8000 [Translation] Use `locale_parse` for computing fallback locales by alanpoulain · Pull Request #35103 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Use locale_parse for computing fallback locales #35103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to 8000 load files.
Loading
Diff view
Diff view
Use locale_parse for computing fallback locales
  • Loading branch information
alanpoulain committed Dec 24, 2019
commit 3657c0e664bfb1a62f9fea9a898406909a1e130b
31 changes: 27 additions & 4 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
8000
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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