Skip to content

Commit

Permalink
[Bugfix][Locale] Fixed incomplete Locale data loading
Browse files Browse the repository at this point in the history
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
  • Loading branch information
manuelkiessling committed Jan 16, 2012
1 parent 33f68fe commit cacc880
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Symfony/Component/Locale/Locale.php
Expand Up @@ -61,6 +61,11 @@ static public function getDisplayCountries($locale)
}
}

$fallbackLocale = self::getFallbackLocale($locale);
if (null !== $fallbackLocale) {
$countries = array_merge(self::getDisplayCountries($fallbackLocale), $countries);
}

$collator->asort($countries);

self::$countries[$locale] = $countries;
Expand Down Expand Up @@ -108,6 +113,11 @@ static public function getDisplayLanguages($locale)
}
}

$fallbackLocale = self::getFallbackLocale($locale);
if (null !== $fallbackLocale) {
$languages = array_merge(self::getDisplayLanguages($fallbackLocale), $languages);
}

$collator->asort($languages);

self::$languages[$locale] = $languages;
Expand Down Expand Up @@ -150,6 +160,11 @@ static public function getDisplayLocales($locale)
$locales[$code] = $name;
}

$fallbackLocale = self::getFallbackLocale($locale);
if (null !== $fallbackLocale) {
$locales = array_merge(self::getDisplayLocales($fallbackLocale), $locales);
}

$collator->asort($locales);

self::$locales[$locale] = $locales;
Expand All @@ -168,4 +183,23 @@ static public function getLocales()
{
return array_keys(self::getDisplayLocales(self::getDefault()));
}

/**
* Returns the fallback locale for a given locale, if any
*
* @param $locale The locale to find the fallback for
* @return string|null The fallback locale, or null if no parent exists
*/
static protected function getFallbackLocale($locale)
{
if ($locale === self::getDefault()) {
return null;
}

if (false === $pos = strrpos($locale, '_')) {
return self::getDefault();
};

return substr($locale, 0, $pos);
}
}
47 changes: 47 additions & 0 deletions tests/Symfony/Tests/Component/Locale/LocaleTest.php
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Locale;

use Symfony\Component\Locale\Locale;

class LocaleTest extends \PHPUnit_Framework_TestCase
{
public function testGetDisplayCountriesReturnsFullListForSubLocale()
{
$countriesDe = Locale::getDisplayCountries('de');
$countriesDeCh = Locale::getDisplayCountries('de_CH');

$this->assertEquals(count($countriesDe), count($countriesDeCh));
$this->assertEquals($countriesDe['BD'], 'Bangladesch');
$this->assertEquals($countriesDeCh['BD'], 'Bangladesh');
}

public function testGetDisplayLanguagesReturnsFullListForSubLocale()
{
$languagesDe = Locale::getDisplayLanguages('de');
$languagesDeCh = Locale::getDisplayLanguages('de_CH');

$this->assertEquals(count($languagesDe), count($languagesDeCh));
$this->assertEquals($languagesDe['be'], 'Weißrussisch');
$this->assertEquals($languagesDeCh['be'], 'Weissrussisch');
}

public function testGetDisplayLocalesReturnsFullListForSubLocale()
{
$localesDe = Locale::getDisplayLocales('de');
$localesDeCh = Locale::getDisplayLocales('de_CH');

$this->assertEquals(count($localesDe), count($localesDeCh));
$this->assertEquals($localesDe['be'], 'Weißrussisch');
$this->assertEquals($localesDeCh['be'], 'Weissrussisch');
}
}

0 comments on commit cacc880

Please sign in to comment.