From 1e57cb252e9752b118c4f2b6fb4bba0719864d5e Mon Sep 17 00:00:00 2001 From: Dimitrios Savvopoulos Date: Fri, 13 Feb 2015 14:31:53 +0100 Subject: [PATCH] Added configuration option for returning fallback translations --- composer.json | 2 +- src/Translatable/Translatable.php | 9 ++++++++- src/config/translatable.php | 12 ++++++++++++ tests/TranslatableTest.php | 24 ++++++++++++++++++++++++ versions.md | 1 + 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c18f3fa..c9ba0f9 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "illuminate/support": "~5.0" }, "require-dev": { - "orchestra/testbench": "3.0.*@dev", + "orchestra/testbench": "~3.0", "phpunit/phpunit": "~4.0" }, "autoload": { diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 2b6742e..5d2aee3 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -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) diff --git a/src/config/translatable.php b/src/config/translatable.php index b989509..e315914 100644 --- a/src/config/translatable.php +++ b/src/config/translatable.php @@ -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 diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index cf1cd4d..2f03798 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -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 */ diff --git a/versions.md b/versions.md index 8924b06..6409c6b 100644 --- a/versions.md +++ b/versions.md @@ -3,6 +3,7 @@ ### v. 5 * Laravel 5 ready + * Added configuration option for returning fallback translations ### v. 4.5