Skip to content

Commit

Permalink
Merge branch '3.4' into 4.3
Browse files Browse the repository at this point in the history
* 3.4:
  Add missing use statement
  [Profiler] wording
  X-Accel Nginx URL updated
  ticket-30197 [Validator] Add the missing translations for the Chinese (Taiwan) ("zh_TW") locale
  Fixed test added in #35022
  Use locale_parse for computing fallback locales
  [Console] Fix filtering out identical alternatives when there is a command loader
  • Loading branch information
nicolas-grekas committed Dec 28, 2019
2 parents 1a2b79e + a8b92ed commit 9fe102b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions DataCollector/TranslationDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\VarDumper\Cloner\Data;

/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
Expand Down
31 changes: 27 additions & 4 deletions Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,38 @@ public function testTransWithIcuRootFallbackLocale()
$this->assertSame('bar', $translator->trans('bar'));
}

public function testTransWithFallbackLocaleBis()
/**
* @dataProvider getFallbackLocales
*/
public function testTransWithFallbackLocaleBis($expectedLocale, $locale)
{
$translator = new Translator('en_US');
$translator = new Translator($locale);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['foo' => 'foofoo'], 'en_US');
$translator->addResource('array', ['bar' => 'foobar'], 'en');
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
$translator->addResource('array', ['bar' => 'foobar'], $expectedLocale);
$this->assertEquals('foobar', $translator->trans('bar'));
}

public function getFallbackLocales()
{
$locales = [
['en', 'en_US'],
['en', 'en-US'],
['sl_Latn_IT', 'sl_Latn_IT_nedis'],
['sl_Latn', 'sl_Latn_IT'],
];

if (\function_exists('locale_parse')) {
$locales[] = ['sl_Latn_IT', 'sl-Latn-IT-nedis'];
$locales[] = ['sl_Latn', 'sl-Latn-IT'];
} else {
$locales[] = ['sl-Latn-IT', 'sl-Latn-IT-nedis'];
$locales[] = ['sl-Latn', 'sl-Latn-IT'];
}

return $locales;
}

public function testTransWithFallbackLocaleTer()
{
$translator = new Translator('fr_FR');
Expand Down
15 changes: 11 additions & 4 deletions Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,17 @@ protected function computeFallbackLocales($locale)
while ($locale) {
$parent = $parentLocales[$locale] ?? null;

if (!$parent && false !== strrchr($locale, '_')) {
$locale = substr($locale, 0, -\strlen(strrchr($locale, '_')));
} elseif ('root' !== $parent) {
$locale = $parent;
if ($parent) {
$locale = 'root' !== $parent ? $parent : null;
} elseif (\function_exists('locale_parse')) {
$localeSubTags = locale_parse($locale);
$locale = null;
if (1 < \count($localeSubTags)) {
array_pop($localeSubTags);
$locale = locale_compose($localeSubTags) ?: null;
}
} elseif ($i = strrpos($locale, '_') ?: strrpos($locale, '-')) {
$locale = substr($locale, 0, $i);
} else {
$locale = null;
}
Expand Down

0 comments on commit 9fe102b

Please sign in to comment.