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

Commit

Permalink
The app.fallback_locale setting can be overridden in each model sep…
Browse files Browse the repository at this point in the history
…arately.

Fallback translation is not returned if it is not defined.
  • Loading branch information
dimsav committed Aug 21, 2014
1 parent 914c33c commit fa9bfdd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ public function translateOrDefault($locale)
public function getTranslation($locale = null, $withFallback = false)
{
$locale = $locale ?: App::getLocale();
$withFallback = isset($this->useTranslationFallback) ? $this->useTranslationFallback : $withFallback;

if ($this->getTranslationByLocaleKey($locale))
{
$translation = $this->getTranslationByLocaleKey($locale);
}
elseif ($withFallback && App::make('config')->has('app.fallback_locale'))
elseif ($withFallback
&& App::make('config')->has('app.fallback_locale')
&& $this->getTranslationByLocaleKey(App::make('config')->get('app.fallback_locale'))
)
{
$translation = $this->getTranslationByLocaleKey(App::make('config')->get('app.fallback_locale'));
}
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ Translatable is fully compatible with all kinds of Eloquent extensions, includin

### v. 4.3

* Added option to override translation class suffix. See [#29](https://github.com/dimsav/laravel-translatable/pull/29)
* The `Translation` class suffix default can be overridden in the app config. See [7ecc0a75d](https://github.com/dimsav/laravel-translatable/commit/7ecc0a75dfcec58ebf694e0a7feb686294b49847)
* The `app.fallback_locale` setting can be overridden in each model separately. See [#33](https://github.com/dimsav/laravel-translatable/pull/33)
* Fallback translation is not returned if it is not defined.

### v. 4.2

Expand Down
28 changes: 28 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,32 @@ public function it_returns_default_translation()
$this->assertEquals($country->getTranslation('ch', false)->name, null);
}

/**
* @test
*/
public function models_fallback_option_overrides_fallback_option_in_config()
{
$country = Country::find(1);
$this->assertEquals($country->getTranslation('ch', true)->locale, 'de');

$country = Country::find(1);
$country->useTranslationFallback = false;
$this->assertEquals($country->getTranslation('ch', true)->locale, 'ch');

$country = Country::find(1);
$country->useTranslationFallback = true;
$this->assertEquals($country->getTranslation('ch', true)->locale, 'de');
}

/**
* @test
*/
public function it_skips_fallback_if_fallback_is_not_defined()
{
App::make('config')->set('app.fallback_locale', 'ch');

$country = Country::find(1);
$this->assertEquals($country->getTranslation('pl', true)->locale, 'pl');
}

}

0 comments on commit fa9bfdd

Please sign in to comment.