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

Commit

Permalink
Added configuration option for returning fallback translations
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsav committed Feb 13, 2015
1 parent 8f41827 commit 1e57cb2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"illuminate/support": "~5.0"
},
"require-dev": {
"orchestra/testbench": "3.0.*@dev",
"orchestra/testbench": "~3.0",
"phpunit/phpunit": "~4.0"
},
"autoload": {
Expand Down
9 changes: 8 additions & 1 deletion src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,16 @@ private function getFallbackLocale()
return App::make('config')->get('translatable.fallback_locale');
}

/**
* @return bool|null
*/
private function useFallback()
{
return isset($this->useTranslationFallback) ? $this->useTranslationFallback : false;
if (isset($this->useTranslationFallback) and $this->useTranslationFallback !== null)
{
return $this->useTranslationFallback;
}
return App::make('config')->get('translatable.use_fallback');
}

protected function isTranslationAttribute($key)
Expand Down
12 changes: 12 additions & 0 deletions src/config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
*/
'locales' => ['en', 'es'],

/*
|--------------------------------------------------------------------------
| Use fallback
|--------------------------------------------------------------------------
|
| Determine if fallback locales are returned by default or not. To add
| more flexibility and configure this option per "translatable"
| instance, this value will be overridden by the property
| $useTranslationFallback when defined
*/
'use_fallback' => false,

/*
|--------------------------------------------------------------------------
| Fallback Locale
Expand Down
24 changes: 24 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ public function fallback_option_in_config_overrides_models_fallback_option()
$this->assertSame($country->getTranslation('ch'), null);
}

/**
* @test
*/
public function configuration_defines_if_fallback_is_used()
{
App::make('config')->set('translatable.fallback_locale', 'de');
App::make('config')->set('translatable.use_fallback', true);

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

/**
* @test
*/
public function useTranslationFallback_overrides_configuration()
{
App::make('config')->set('translatable.fallback_locale', 'de');
App::make('config')->set('translatable.use_fallback', true);
$country = Country::find(1);
$country->useTranslationFallback = false;
$this->assertSame($country->getTranslation('ch'), null);
}

/**
* @test
*/
Expand Down
1 change: 1 addition & 0 deletions versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### v. 5

* Laravel 5 ready
* Added configuration option for returning fallback translations

### v. 4.5

Expand Down

0 comments on commit 1e57cb2

Please sign in to comment.