diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 61197e86d3d5..9894a0f294fe 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -1573,15 +1573,24 @@ public function getFallbackLocale() * Set the current application locale. * * @param string $locale + * @param bool $useDerivedFallback * @return void */ - public function setLocale($locale) + public function setLocale($locale, $useDerivedFallback = false) { $this['config']->set('app.locale', $locale); $this['translator']->setLocale($locale); $this['events']->dispatch(new LocaleUpdated($locale)); + + if ($useDerivedFallback) { + $primaryLanguage = substr($locale, 0, 2); + + if ($primaryLanguage !== $locale) { + $this->setFallbackLocale($primaryLanguage); + } + } } /** diff --git a/tests/Integration/Translation/TranslatorTest.php b/tests/Integration/Translation/TranslatorTest.php index 2756d75ad8e6..9986baee9dd1 100644 --- a/tests/Integration/Translation/TranslatorTest.php +++ b/tests/Integration/Translation/TranslatorTest.php @@ -136,4 +136,28 @@ public static function greetingChoiceDataProvider() yield [0, 'Bonjour :name', 'fr']; yield [3, 'Bonjour :name, vous avez :count messages non lus', 'fr']; } + + public function test_it_can_use_derived_fallback_locale() + { + $this->app->setLocale('de_CH', true); + + $this->assertSame('de', $this->app['translator']->getFallback()); + + $this->assertSame('Grüezi', $this->app['translator']->get('Greeting')); + + $this->assertSame('Welcome', $this->app['translator']->get('Welcome')); + + $this->assertSame('Goodbye', $this->app['translator']->get('Goodbye')); + } + + public function test_has_for_locale_respects_derived_fallback() + { + $this->app->setLocale('de_CH', true); + + $this->assertTrue($this->app['translator']->hasForLocale('Greeting')); + + $this->assertFalse($this->app['translator']->hasForLocale('Welcome')); + + $this->assertFalse($this->app['translator']->hasForLocale('Nonexistent')); + } } diff --git a/tests/Integration/Translation/lang/de.json b/tests/Integration/Translation/lang/de.json new file mode 100644 index 000000000000..3d203b9092ef --- /dev/null +++ b/tests/Integration/Translation/lang/de.json @@ -0,0 +1,4 @@ +{ + "Greeting": "Hallo", + "Welcome": "Willkommen" +} diff --git a/tests/Integration/Translation/lang/de_CH.json b/tests/Integration/Translation/lang/de_CH.json new file mode 100644 index 000000000000..cf6b864dfa27 --- /dev/null +++ b/tests/Integration/Translation/lang/de_CH.json @@ -0,0 +1,4 @@ +{ + "Greeting": "Grüezi", + "Hello": "Grüezi mitenand" +} diff --git a/tests/Integration/Translation/lang/en.json b/tests/Integration/Translation/lang/en.json index 5fe21d7ec1c6..05c7fb310834 100644 --- a/tests/Integration/Translation/lang/en.json +++ b/tests/Integration/Translation/lang/en.json @@ -3,5 +3,7 @@ "30 Days": "30 Days", "365 Days": "365 Days", "60 Days": "60 Days", - "90 Days": "90 Days" + "90 Days": "90 Days", + "Greeting": "Hello", + "Welcome": "Welcome" }