Skip to content
This repository was archived by the owner on May 16, 2018. It is now read-only.
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
77 changes: 77 additions & 0 deletions library/Zend/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,43 @@
*/
class Zend_Locale
{
/**
* List of locales that are no longer part of CLDR along with a
* mapping to an appropriate alternative.
*
* @var array
*/
private static $_localeAliases = array(
'az_AZ' => 'az_Latn_AZ',
'bs_BA' => 'bs_Latn_BA',
'ha_GH' => 'ha_Latn_GH',
'ha_NE' => 'ha_Latn_NE',
'ha_NG' => 'ha_Latn_NG',
'kk_KZ' => 'kk_Cyrl_KZ',
'ks_IN' => 'ks_Arab_IN',
'mn_MN' => 'mn_Cyrl_MN',
'ms_BN' => 'ms_Latn_BN',
'ms_MY' => 'ms_Latn_MY',
'ms_SG' => 'ms_Latn_SG',
'pa_IN' => 'pa_Guru_IN',
'pa_PK' => 'pa_Arab_PK',
'shi_MA' => 'shi_Latn_MA',
'sr_BA' => 'sr_Latn_BA',
'sr_ME' => 'sr_Latn_ME',
'sr_RS' => 'sr_Latn_RS',
'sr_XK' => 'sr_Latn_XK',
'tg_TJ' => 'tg_Cyrl_TJ',
'tzm_MA' => 'tzm_Latn_MA',
'uz_AF' => 'uz_Arab_AF',
'uz_UZ' => 'uz_Latn_UZ',
'vai_LR' => 'vai_Latn_LR',
'zh_CN' => 'zh_Hans_CN',
'zh_HK' => 'zh_Hans_HK',
'zh_MO' => 'zh_Hans_MO',
'zh_SG' => 'zh_Hans_SG',
'zh_TW' => 'zh_Hant_TW',
);

/**
* Class wide Locale Constants
*
Expand Down Expand Up @@ -1266,6 +1303,12 @@ public function setLocale($locale = null)
$locale = self::_prepareLocale($locale);

if (isset(self::$_localeData[(string) $locale]) === false) {
// Is it an alias? If so, we can use this locale
if (isset(self::$_localeAliases[$locale]) === true) {
$this->_locale = $locale;
return;
}

$region = substr((string) $locale, 0, 3);
if (isset($region[2]) === true) {
if (($region[2] === '_') or ($region[2] === '-')) {
Expand Down Expand Up @@ -1878,4 +1921,38 @@ public static function getOrder($order = null)

return $languages;
}

/**
* Is the given locale in the list of aliases?
*
* @param string|Zend_Locale $locale Locale to work on
* @return boolean
*/
public static function isAlias($locale)
{
if ($locale instanceof Zend_Locale) {
$locale = $locale->toString();
}

return isset(self::$_localeAliases[$locale]);
}

/**
* Return an alias' actual locale.
*
* @param string|Zend_Locale $locale Locale to work on
* @return string
*/
public static function getAlias($locale)
{
if ($locale instanceof Zend_Locale) {
$locale = $locale->toString();
}

if (isset(self::$_localeAliases[$locale]) === true) {
return self::$_localeAliases[$locale];
}

return (string) $locale;
}
}
4 changes: 4 additions & 0 deletions library/Zend/Locale/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ private static function _checkLocale($locale)
throw new Zend_Locale_Exception("Locale (" . (string) $locale . ") is a unknown locale");
}

if (Zend_Locale::isAlias($locale)) {
// Return a valid CLDR locale so that the XML file can be loaded.
return Zend_Locale::getAlias($locale);
}
return (string) $locale;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Zend/Locale/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public function tearDown()
$this->_cache->clean(Zend_Cache::CLEANING_MODE_ALL);
}

/**
* test for reading the scriptlist from a locale that is an alias
*/
public function testAliases()
{
$data = Zend_Locale_Data::getList('zh_CN', 'script');
$this->assertEquals('阿拉伯文', $data['Arab']);
}

/**
* test for reading with standard locale
* expected array
Expand Down
22 changes: 22 additions & 0 deletions tests/Zend/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ public function tearDown()
setlocale(LC_ALL, $this->_locale);
}

/**
* Test that locale names that have been dropped from CLDR continue to
* work.
*/
public function testAliases()
{
$locale = new Zend_Locale('zh_CN');
$this->assertEquals(true, $locale->isLocale('zh_CN'));
$this->assertEquals('zh', $locale->getLanguage());
$this->assertEquals('CN', $locale->getRegion());
$this->assertEquals(true, Zend_Locale::isAlias($locale));
$this->assertEquals(true, Zend_Locale::isAlias('zh_CN'));
$this->assertEquals('zh_Hans_CN', Zend_Locale::getAlias('zh_CN'));

$locale = new Zend_Locale('zh_Hans_CN');
$this->assertEquals(true, $locale->isLocale('zh_Hans_CN'));
$this->assertEquals('zh', $locale->getLanguage());
$this->assertEquals('CN', $locale->getRegion());
$this->assertEquals(false, Zend_Locale::isAlias('zh_Hans_CN'));
$this->assertEquals('zh_Hans_CN', Zend_Locale::getAlias('zh_Hans_CN'));
}

/**
* test for object creation
* expected object instance
Expand Down