Skip to content

Commit

Permalink
fallback to fallback_locale translation when requested translation li…
Browse files Browse the repository at this point in the history
…ne is empty (#34136)
  • Loading branch information
ahmedsayedabdelsalam committed Sep 4, 2020
1 parent d2e513a commit 02e983e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Translation/Translator.php
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions tests/Translation/TranslationTranslatorTest.php
Expand Up @@ -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');
Expand Down

2 comments on commit 02e983e

@brendt
Copy link
Contributor

@brendt brendt commented on 02e983e Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit causes a significant problem where empty translations strings ("") aren't possible anymore: #34218

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reverted this btw. Will tag soon.

Please sign in to comment.