Skip to content

Commit a36f542

Browse files
Merge branch '4.3' into 4.4
* 4.3: Fixed #35084 Add missing use statement [HttpClient] fix scheduling pending NativeResponse do not overwrite variable value [Profiler] wording Use spaces correctly to display options in DebugCommand 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
2 parents f7669f4 + 9fe102b commit a36f542

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

DataCollector/TranslationDataCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
1717
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
1818
use Symfony\Component\Translation\DataCollectorTranslator;
19+
use Symfony\Component\VarDumper\Cloner\Data;
1920

2021
/**
2122
* @author Abdellatif Ait boudad <[email protected]>

Tests/TranslatorTest.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,38 @@ public function testTransWithIcuRootFallbackLocale()
305305
$this->assertSame('bar', $translator->trans('bar'));
306306
}
307307

308-
public function testTransWithFallbackLocaleBis()
308+
/**
309+
* @dataProvider getFallbackLocales
310+
*/
311+
public function testTransWithFallbackLocaleBis($expectedLocale, $locale)
309312
{
310-
$translator = new Translator('en_US');
313+
$translator = new Translator($locale);
311314
$translator->addLoader('array', new ArrayLoader());
312-
$translator->addResource('array', ['foo' => 'foofoo'], 'en_US');
313-
$translator->addResource('array', ['bar' => 'foobar'], 'en');
315+
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
316+
$translator->addResource('array', ['bar' => 'foobar'], $expectedLocale);
314317
$this->assertEquals('foobar', $translator->trans('bar'));
315318
}
316319

320+
public function getFallbackLocales()
321+
{
322+
$locales = [
323+
['en', 'en_US'],
324+
['en', 'en-US'],
325+
['sl_Latn_IT', 'sl_Latn_IT_nedis'],
326+
['sl_Latn', 'sl_Latn_IT'],
327+
];
328+
329+
if (\function_exists('locale_parse')) {
330+
$locales[] = ['sl_Latn_IT', 'sl-Latn-IT-nedis'];
331+
$locales[] = ['sl_Latn', 'sl-Latn-IT'];
332+
} else {
333+
$locales[] = ['sl-Latn-IT', 'sl-Latn-IT-nedis'];
334+
$locales[] = ['sl-Latn', 'sl-Latn-IT'];
335+
}
336+
337+
return $locales;
338+
}
339+
317340
public function testTransWithFallbackLocaleTer()
318341
{
319342
$translator = new Translator('fr_FR');

Translator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,17 @@ protected function computeFallbackLocales($locale)
471471
while ($locale) {
472472
$parent = $parentLocales[$locale] ?? null;
473473

474-
if (!$parent && false !== strrchr($locale, '_')) {
475-
$locale = substr($locale, 0, -\strlen(strrchr($locale, '_')));
476-
} elseif ('root' !== $parent) {
477-
$locale = $parent;
474+
if ($parent) {
475+
$locale = 'root' !== $parent ? $parent : null;
476+
} elseif (\function_exists('locale_parse')) {
477+
$localeSubTags = locale_parse($locale);
478+
$locale = null;
479+
if (1 < \count($localeSubTags)) {
480+
array_pop($localeSubTags);
481+
$locale = locale_compose($localeSubTags) ?: null;
482+
}
483+
} elseif ($i = strrpos($locale, '_') ?: strrpos($locale, '-')) {
484+
$locale = substr($locale, 0, $i);
478485
} else {
479486
$locale = null;
480487
}

0 commit comments

Comments
 (0)