Skip to content

Commit cacc880

Browse files
[Bugfix][Locale] Fixed incomplete Locale data loading
Sublocales like de_CH returned only incomplete results for getDisplayCountries(), getDisplayLanguages() and getDisplayLocales(), consisting only of results specific for this sublocale, but without the results of their respective parent locale
1 parent 33f68fe commit cacc880

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/Symfony/Component/Locale/Locale.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ static public function getDisplayCountries($locale)
6161
}
6262
}
6363

64+
$fallbackLocale = self::getFallbackLocale($locale);
65+
if (null !== $fallbackLocale) {
66+
$countries = array_merge(self::getDisplayCountries($fallbackLocale), $countries);
67+
}
68+
6469
$collator->asort($countries);
6570

6671
self::$countries[$locale] = $countries;
@@ -108,6 +113,11 @@ static public function getDisplayLanguages($locale)
108113
}
109114
}
110115

116+
$fallbackLocale = self::getFallbackLocale($locale);
117+
if (null !== $fallbackLocale) {
118+
$languages = array_merge(self::getDisplayLanguages($fallbackLocale), $languages);
119+
}
120+
111121
$collator->asort($languages);
112122

113123
self::$languages[$locale] = $languages;
@@ -150,6 +160,11 @@ static public function getDisplayLocales($locale)
150160
$locales[$code] = $name;
151161
}
152162

163+
$fallbackLocale = self::getFallbackLocale($locale);
164+
if (null !== $fallbackLocale) {
165+
$locales = array_merge(self::getDisplayLocales($fallbackLocale), $locales);
166+
}
167+
153168
$collator->asort($locales);
154169

155170
self::$locales[$locale] = $locales;
@@ -168,4 +183,23 @@ static public function getLocales()
168183
{
169184
return array_keys(self::getDisplayLocales(self::getDefault()));
170185
}
186+
187+
/**
188+
* Returns the fallback locale for a given locale, if any
189+
*
190+
* @param $locale The locale to find the fallback for
191+
* @return string|null The fallback locale, or null if no parent exists
192+
*/
193+
static protected function getFallbackLocale($locale)
194+
{
195+
if ($locale === self::getDefault()) {
196+
return null;
197+
}
198+
199+
if (false === $pos = strrpos($locale, '_')) {
200+
return self::getDefault();
201+
};
202+
203+
return substr($locale, 0, $pos);
204+
}
171205
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Locale;
13+
14+
use Symfony\Component\Locale\Locale;
15+
16+
class LocaleTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testGetDisplayCountriesReturnsFullListForSubLocale()
19+
{
20+
$countriesDe = Locale::getDisplayCountries('de');
21+
$countriesDeCh = Locale::getDisplayCountries('de_CH');
22+
23+
$this->assertEquals(count($countriesDe), count($countriesDeCh));
24+
$this->assertEquals($countriesDe['BD'], 'Bangladesch');
25+
$this->assertEquals($countriesDeCh['BD'], 'Bangladesh');
26+
}
27+
28+
public function testGetDisplayLanguagesReturnsFullListForSubLocale()
29+
{
30+
$languagesDe = Locale::getDisplayLanguages('de');
31+
$languagesDeCh = Locale::getDisplayLanguages('de_CH');
32+
33+
$this->assertEquals(count($languagesDe), count($languagesDeCh));
34+
$this->assertEquals($languagesDe['be'], 'Weißrussisch');
35+
$this->assertEquals($languagesDeCh['be'], 'Weissrussisch');
36+
}
37+
38+
public function testGetDisplayLocalesReturnsFullListForSubLocale()
39+
{
40+
$localesDe = Locale::getDisplayLocales('de');
41+
$localesDeCh = Locale::getDisplayLocales('de_CH');
42+
43+
$this->assertEquals(count($localesDe), count($localesDeCh));
44+
$this->assertEquals($localesDe['be'], 'Weißrussisch');
45+
$this->assertEquals($localesDeCh['be'], 'Weissrussisch');
46+
}
47+
}

0 commit comments

Comments
 (0)