Skip to content

Commit cee47ce

Browse files
committed
bug #35103 [Translation] Use locale_parse for computing fallback locales (alanpoulain)
This PR was merged into the 3.4 branch. Discussion ---------- [Translation] Use `locale_parse` for computing fallback locales | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A As done in this PR #24157 for the `Intl` component, the `Translation` component should use `locale_parse` as well when available. It will allow to manage [BCP 47](https://tools.ietf.org/html/bcp47) locales, which is why it is considered a bugfix ([locale_set_default](https://www.php.net/manual/en/locale.setdefault.php) is using BCP 47 compliant locale). As done with the forementioned PR, there is also a fallback to make it work with `-`. Sadly, I think it will create some conflicts when merging it upstream since the modified code has changed little by little. Commits ------- 3657c0e Use locale_parse for computing fallback locales
2 parents be40f32 + 3657c0e commit cee47ce

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

+27-4
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,38 @@ public function testTransWithFallbackLocaleFile($format, $loader)
234234
$this->assertEquals('bar', $translator->trans('foo', [], 'resources'));
235235
}
236236

237-
public function testTransWithFallbackLocaleBis()
237+
/**
238+
* @dataProvider getFallbackLocales
239+
*/
240+
public function testTransWithFallbackLocaleBis($expectedLocale, $locale)
238241
{
239-
$translator = new Translator('en_US');
242+
$translator = new Translator($locale);
240243
$translator->addLoader('array', new ArrayLoader());
241-
$translator->addResource('array', ['foo' => 'foofoo'], 'en_US');
242-
$translator->addResource('array', ['bar' => 'foobar'], 'en');
244+
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
245+
$translator->addResource('array', ['bar' => 'foobar'], $expectedLocale);
243246
$this->assertEquals('foobar', $translator->trans('bar'));
244247
}
245248

249+
public function getFallbackLocales()
250+
{
251+
$locales = [
252+
['en', 'en_US'],
253+
['en', 'en-US'],
254+
['sl_Latn_IT', 'sl_Latn_IT_nedis'],
255+
['sl_Latn', 'sl_Latn_IT'],
256+
];
257+
258+
if (\function_exists('locale_parse')) {
259+
$locales[] = ['sl_Latn_IT', 'sl-Latn-IT-nedis'];
260+
$locales[] = ['sl_Latn', 'sl-Latn-IT'];
261+
} else {
262+
$locales[] = ['sl-Latn-IT', 'sl-Latn-IT-nedis'];
263+
$locales[] = ['sl-Latn', 'sl-Latn-IT'];
264+
}
265+
266+
return $locales;
267+
}
268+
246269
public function testTransWithFallbackLocaleTer()
247270
{
248271
$translator = new Translator('fr_FR');

src/Symfony/Component/Translation/Translator.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,19 @@ protected function computeFallbackLocales($locale)
412412
$locales[] = $fallback;
413413
}
414414

415-
if (false !== strrchr($locale, '_')) {
415+
if (\function_exists('locale_parse')) {
416+
$localeSubTags = locale_parse($locale);
417+
if (1 < \count($localeSubTags)) {
418+
array_pop($localeSubTags);
419+
$fallback = locale_compose($localeSubTags);
420+
if (false !== $fallback) {
421+
array_unshift($locales, $fallback);
422+
}
423+
}
424+
} elseif (false !== strrchr($locale, '_')) {
416425
array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '_'))));
426+
} elseif (false !== strrchr($locale, '-')) {
427+
array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '-'))));
417428
}
418429

419430
return array_unique($locales);

0 commit comments

Comments
 (0)