Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cc0c36

Browse files
ro0NLfabpot
authored andcommitted
[Translation] Reverse fallback locales
1 parent b2a24b1 commit 7cc0c36

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/Symfony/Component/Translation/Tests/TranslatorTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,17 @@ public function testTransWithIcuVariantFallbackLocale()
304304
$translator->addResource('array', ['foo' => 'foofoo'], 'en_GB_scouse');
305305
$translator->addResource('array', ['bar' => 'foobar'], 'en_GB');
306306
$translator->addResource('array', ['baz' => 'foobaz'], 'en_001');
307-
$translator->addResource('array', ['qux' => 'fooqux'], 'en');
307+
$translator->addResource('array', ['bar' => 'en', 'qux' => 'fooqux'], 'en');
308+
$translator->addResource('array', ['bar' => 'nl_NL', 'fallback' => 'nl_NL'], 'nl_NL');
309+
$translator->addResource('array', ['bar' => 'nl', 'fallback' => 'nl'], 'nl');
310+
311+
$translator->setFallbackLocales(['nl_NL', 'nl']);
312+
308313
$this->assertSame('foofoo', $translator->trans('foo'));
309314
$this->assertSame('foobar', $translator->trans('bar'));
310315
$this->assertSame('foobaz', $translator->trans('baz'));
311316
$this->assertSame('fooqux', $translator->trans('qux'));
317+
$this->assertSame('nl_NL', $translator->trans('fallback'));
312318
}
313319

314320
public function testTransWithIcuRootFallbackLocale()
@@ -358,7 +364,7 @@ public function testTransWithFallbackLocaleTer()
358364
$translator = new Translator('fr_FR');
359365
$translator->addLoader('array', new ArrayLoader());
360366
$translator->addResource('array', ['foo' => 'foo (en_US)'], 'en_US');
361-
$translator->addResource('array', ['bar' => 'bar (en)'], 'en');
367+
$translator->addResource('array', ['foo' => 'foo (en)', 'bar' => 'bar (en)'], 'en');
362368

363369
$translator->setFallbackLocales(['en_US', 'en']);
364370

src/Symfony/Component/Translation/Translator.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,8 @@ protected function computeFallbackLocales($locale)
462462
$this->parentLocales = json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
463463
}
464464

465+
$originLocale = $locale;
465466
$locales = [];
466-
foreach ($this->fallbackLocales as $fallback) {
467-
if ($fallback === $locale) {
468-
continue;
469-
}
470-
471-
$locales[] = $fallback;
472-
}
473467

474468
while ($locale) {
475469
$parent = $this->parentLocales[$locale] ?? null;
@@ -490,10 +484,18 @@ protected function computeFallbackLocales($locale)
490484
}
491485

492486
if (null !== $locale) {
493-
array_unshift($locales, $locale);
487+
$locales[] = $locale;
494488
}
495489
}
496490

491+
foreach ($this->fallbackLocales as $fallback) {
492+
if ($fallback === $originLocale) {
493+
continue;
494+
}
495+
496+
$locales[] = $fallback;
497+
}
498+
497499
return array_unique($locales);
498500
}
499501

0 commit comments

Comments
 (0)