8000 merged branch ManuelKiessling/ticket_3090_bugfix (PR #3127) · lsmith77/symfony@33170ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 33170ae

Browse files
committed
merged branch ManuelKiessling/ticket_3090_bugfix (PR symfony#3127)
Commits ------- 7e14a56 [Locale] Removed unneccesary semi-colon cacc880 [Bugfix][Locale] Fixed incomplete Locale data loading Discussion ---------- [Bugfix][Locale] Fixed incomplete Locale data loading Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: ![Build Status](https://secure.travis-ci.org/ManuelKiessling/symfony.png) Fixes the following tickets: symfony#3090 Todo: - 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 This PR was symfony#3106 befor 10000 e - reopened it as a new PR because the commits were too chaotic.
2 parents 056c16e + 7e14a56 commit 33170ae

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)
0