Skip to content

Commit

Permalink
Merge pull request zendframework#307 from akrabat/locale-bc
Browse files Browse the repository at this point in the history
Aliases in Zend_Locale for locales that no longer exist in CLDR
  • Loading branch information
froschdesign committed Mar 24, 2014
2 parents d70cdf9 + 8391f08 commit f97293b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
77 changes: 77 additions & 0 deletions 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 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

0 comments on commit f97293b

Please sign in to comment.