Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Allow translateOrDefault to user app locale #500

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function translate($locale = null, $withFallback = false)
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function translateOrDefault($locale)
public function translateOrDefault($locale = null)
{
return $this->getTranslation($locale, true);
}
Expand All @@ -44,7 +44,7 @@ public function translateOrDefault($locale)
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function translateOrNew($locale)
public function translateOrNew($locale = null)
{
return $this->getTranslationOrNew($locale);
}
Expand Down Expand Up @@ -265,8 +265,10 @@ public function save(array $options = [])
*
* @return \Illuminate\Database\Eloquent\Model
*/
protected function getTranslationOrNew($locale)
protected function getTranslationOrNew($locale = null)
{
$locale = $locale ?: $this->locale();

if (($translation = $this->getTranslation($locale, false)) === null) {
$translation = $this->getNewTranslation($locale);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public function test_it_returns_default_translation()
$this->assertSame($country->getTranslation('ch', true)->name, 'Griechenland');
$this->assertSame($country->translateOrDefault('ch')->name, 'Griechenland');
$this->assertSame($country->getTranslation('ch', false), null);

$this->app->setLocale('ch');
$this->assertSame($country->translateOrDefault()->name, 'Griechenland');
}

public function test_fallback_option_in_config_overrides_models_fallback_option()
Expand Down Expand Up @@ -341,6 +344,9 @@ public function test_it_has_methods_that_return_always_a_translation()
{
$country = Country::find(1)->first();
$this->assertSame('abc', $country->translateOrNew('abc')->locale);

$this->app->setLocale('xyz');
$this->assertSame('xyz', $country->translateOrNew()->locale);
}

public function test_it_returns_if_attribute_is_translated()
Expand Down