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

Commit 3657c0e

Browse filesBrowse files
committed
Use locale_parse for computing fallback locales
1 parent 135c6f7 commit 3657c0e
Copy full SHA for 3657c0e

File tree

2 files changed

+39
-5
lines changed
Filter options

2 files changed

+39
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/TranslatorTest.php
+27-4Lines changed: 27 additions & 4 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+12-1Lines changed: 12 additions & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.