Skip to content

Commit

Permalink
bug #35103 [Translation] Use locale_parse for computing fallback lo…
Browse files Browse the repository at this point in the history
…cales (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
  • Loading branch information
fabpot committed Dec 27, 2019
2 parents be40f32 + 3657c0e commit cee47ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
31 changes: 27 additions & 4 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Expand Up @@ -234,15 +234,38 @@ public function testTransWithFallbackLocaleFile($format, $loader)
$this->assertEquals('bar', $translator->trans('foo', [], 'resources'));
}

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
13 changes: 12 additions & 1 deletion src/Symfony/Component/Translation/Translator.php
Expand Up @@ -412,8 +412,19 @@ protected function computeFallbackLocales($locale)
$locales[] = $fallback;
}

if (false !== strrchr($locale, '_')) {
if (\function_exists('locale_parse')) {
$localeSubTags = locale_parse($locale);
if (1 < \count($localeSubTags)) {
array_pop($localeSubTags);
$fallback = locale_compose($localeSubTags);
if (false !== $fallback) {
array_unshift($locales, $fallback);
}
}
} elseif (false !== strrchr($locale, '_')) {
array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '_'))));
} elseif (false !== strrchr($locale, '-')) {
array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '-'))));
}

return array_unique($locales);
Expand Down

0 comments on commit cee47ce

Please sign in to comment.