From bfda77105cc6ff0981921e3e348f9c6e1826b9b4 Mon Sep 17 00:00:00 2001 From: ahmed sayed Date: Fri, 4 Sep 2020 11:54:26 +0200 Subject: [PATCH] fallback to fallback_locale translation when requested translation line is empty --- src/Illuminate/Translation/Translator.php | 2 +- tests/Translation/TranslationTranslatorTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index 0f1606e8493e..46611b6b6bde 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -122,7 +122,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true) $locales = $fallback ? $this->localeArray($locale) : [$locale]; foreach ($locales as $locale) { - if (! is_null($line = $this->getLine( + if (! empty($line = $this->getLine( $namespace, $group, $locale, $item, $replace ))) { return $line ?? $key; diff --git a/tests/Translation/TranslationTranslatorTest.php b/tests/Translation/TranslationTranslatorTest.php index a660c8e864aa..021b8b5dcb5d 100755 --- a/tests/Translation/TranslationTranslatorTest.php +++ b/tests/Translation/TranslationTranslatorTest.php @@ -104,6 +104,17 @@ public function testGetMethodProperlyLoadsAndRetrievesItemForFallback() $this->assertSame('foo', $t->get('foo::bar.foo')); } + public function testGetMethodProperlyLoadsAndRetrievesItemForFallbackWhenLineIsEmpty() + { + $t = new Translator($this->getLoader(), 'en'); + $t->setFallback('lv'); + $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]); + $t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn(['baz' => '']); + $t->getLoader()->shouldReceive('load')->once()->with('lv', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :foo']); + $this->assertSame('breeze bar', $t->get('foo::bar.baz', ['foo' => 'bar'], 'en')); + $this->assertSame('foo', $t->get('foo::bar.foo')); + } + public function testGetMethodProperlyLoadsAndRetrievesItemForGlobalNamespace() { $t = new Translator($this->getLoader(), 'en');