Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions 31 src/Symfony/Component/Translation/Tests/TranslatorTest.php
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 13 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.