[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Locale.php
blob478a5e8db1797df550122a62476e80ef666c9d84
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Locale
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
22 /**
23 * Base class for localization
25 * @category Zend
26 * @package Zend_Locale
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
30 class Zend_Locale
32 /**
33 * Class wide Locale Constants
35 * @var array $_localeData
37 private static $_localeData = array(
38 'root' => true, 'aa_DJ' => true, 'aa_ER' => true, 'aa_ET' => true, 'aa' => true,
39 'af_NA' => true, 'af_ZA' => true, 'af' => true, 'ak_GH' => true, 'ak' => true,
40 'am_ET' => true, 'am' => true, 'ar_AE' => true, 'ar_BH' => true, 'ar_DZ' => true,
41 'ar_EG' => true, 'ar_IQ' => true, 'ar_JO' => true, 'ar_KW' => true, 'ar_LB' => true,
42 'ar_LY' => true, 'ar_MA' => true, 'ar_OM' => true, 'ar_QA' => true, 'ar_SA' => true,
43 'ar_SD' => true, 'ar_SY' => true, 'ar_TN' => true, 'ar_YE' => true, 'ar' => true,
44 'as_IN' => true, 'as' => true, 'az_AZ' => true, 'az' => true, 'be_BY' => true,
45 'be' => true, 'bg_BG' => true, 'bg' => true, 'bn_BD' => true, 'bn_IN' => true,
46 'bn' => true, 'bo_CN' => true, 'bo_IN' => true, 'bo' => true, 'bs_BA' => true,
47 'bs' => true, 'byn_ER'=> true, 'byn' => true, 'ca_ES' => true, 'ca' => true,
48 'cch_NG'=> true, 'cch' => true, 'cop' => true, 'cs_CZ' => true, 'cs' => true,
49 'cy_GB' => true, 'cy' => true, 'da_DK' => true, 'da' => true, 'de_AT' => true,
50 'de_BE' => true, 'de_CH' => true, 'de_DE' => true, 'de_LI' => true, 'de_LU' => true,
51 'de' => true, 'dv_MV' => true, 'dv' => true, 'dz_BT' => true, 'dz' => true,
52 'ee_GH' => true, 'ee_TG' => true, 'ee' => true, 'el_CY' => true, 'el_GR' => true,
53 'el' => true, 'en_AS' => true, 'en_AU' => true, 'en_BE' => true, 'en_BW' => true,
54 'en_BZ' => true, 'en_CA' => true, 'en_GB' => true, 'en_GU' => true, 'en_HK' => true,
55 'en_IE' => true, 'en_IN' => true, 'en_JM' => true, 'en_MH' => true, 'en_MP' => true,
56 'en_MT' => true, 'en_NA' => true, 'en_NZ' => true, 'en_PH' => true, 'en_PK' => true,
57 'en_SG' => true, 'en_TT' => true, 'en_UM' => true, 'en_US' => true, 'en_VI' => true,
58 'en_ZA' => true, 'en_ZW' => true, 'en' => true, 'eo' => true, 'es_AR' => true,
59 'es_BO' => true, 'es_CL' => true, 'es_CO' => true, 'es_CR' => true, 'es_DO' => true,
60 'es_EC' => true, 'es_ES' => true, 'es_GT' => true, 'es_HN' => true, 'es_MX' => true,
61 'es_NI' => true, 'es_PA' => true, 'es_PE' => true, 'es_PR' => true, 'es_PY' => true,
62 'es_SV' => true, 'es_US' => true, 'es_UY' => true, 'es_VE' => true, 'es' => true,
63 'et_EE' => true, 'et' => true, 'eu_ES' => true, 'eu' => true, 'fa_AF' => true,
64 'fa_IR' => true, 'fa' => true, 'fi_FI' => true, 'fi' => true, 'fil_PH'=> true,
65 'fil' => true, 'fo_FO' => true, 'fo' => true, 'fr_BE' => true, 'fr_CA' => true,
66 'fr_CH' => true, 'fr_FR' => true, 'fr_LU' => true, 'fr_MC' => true, 'fr_SN' => true,
67 'fr' => true, 'fur_IT'=> true, 'fur' => true, 'ga_IE' => true, 'ga' => true,
68 'gaa_GH'=> true, 'gaa' => true, 'gez_ER'=> true, 'gez_ET'=> true, 'gez' => true,
69 'gl_ES' => true, 'gl' => true, 'gsw_CH'=> true, 'gsw' => true, 'gu_IN' => true,
70 'gu' => true, 'gv_GB' => true, 'gv' => true, 'ha_GH' => true, 'ha_NE' => true,
71 'ha_NG' => true, 'ha_SD' => true, 'ha' => true, 'haw_US'=> true, 'haw' => true,
72 'he_IL' => true, 'he' => true, 'hi_IN' => true, 'hi' => true, 'hr_HR' => true,
73 'hr' => true, 'hu_HU' => true, 'hu' => true, 'hy_AM' => true, 'hy' => true,
74 'ia' => true, 'id_ID' => true, 'id' => true, 'ig_NG' => true, 'ig' => true,
75 'ii_CN' => true, 'ii' => true, 'in' => true, 'is_IS' => true, 'is' => true,
76 'it_CH' => true, 'it_IT' => true, 'it' => true, 'iu' => true, 'iw' => true,
77 'ja_JP' => true, 'ja' => true, 'ka_GE' => true, 'ka' => true, 'kaj_NG'=> true,
78 'kaj' => true, 'kam_KE'=> true, 'kam' => true, 'kcg_NG'=> true, 'kcg' => true,
79 'kfo_CI'=> true, 'kfo' => true, 'kk_KZ' => true, 'kk' => true, 'kl_GL' => true,
80 'kl' => true, 'km_KH' => true, 'km' => true, 'kn_IN' => true, 'kn' => true,
81 'ko_KR' => true, 'ko' => true, 'kok_IN'=> true, 'kok' => true, 'kpe_GN'=> true,
82 'kpe_LR'=> true, 'kpe' => true, 'ku_IQ' => true, 'ku_IR' => true, 'ku_SY' => true,
83 'ku_TR' => true, 'ku' => true, 'kw_GB' => true, 'kw' => true, 'ky_KG' => true,
84 'ky' => true, 'ln_CD' => true, 'ln_CG' => true, 'ln' => true, 'lo_LA' => true,
85 'lo' => true, 'lt_LT' => true, 'lt' => true, 'lv_LV' => true, 'lv' => true,
86 'mk_MK' => true, 'mk' => true, 'ml_IN' => true, 'ml' => true, 'mn_CN' => true,
87 'mn_MN' => true, 'mn' => true, 'mo' => true, 'mr_IN' => true, 'mr' => true,
88 'ms_BN' => true, 'ms_MY' => true, 'ms' => true, 'mt_MT' => true, 'mt' => true,
89 'my_MM' => true, 'my' => true, 'nb_NO' => true, 'nb' => true, 'nds_DE'=> true,
90 'nds' => true, 'ne_IN' => true, 'ne_NP' => true, 'ne' => true, 'nl_BE' => true,
91 'nl_NL' => true, 'nl' => true, 'nn_NO' => true, 'nn' => true, 'no' => true,
92 'nr_ZA' => true, 'nr' => true, 'nso_ZA'=> true, 'nso' => true, 'ny_MW' => true,
93 'ny' => true, 'oc_FR' => true, 'oc' => true, 'om_ET' => true, 'om_KE' => true,
94 'om' => true, 'or_IN' => true, 'or' => true, 'pa_IN' => true, 'pa_PK' => true,
95 'pa' => true, 'pl_PL' => true, 'pl' => true, 'ps_AF' => true, 'ps' => true,
96 'pt_BR' => true, 'pt_PT' => true, 'pt' => true, 'ro_MD' => true, 'ro_RO' => true,
97 'ro' => true, 'ru_RU' => true, 'ru_UA' => true, 'ru' => true, 'rw_RW' => true,
98 'rw' => true, 'sa_IN' => true, 'sa' => true, 'se_FI' => true, 'se_NO' => true,
99 'se' => true, 'sh_BA' => true, 'sh_CS' => true, 'sh_YU' => true, 'sh' => true,
100 'si_LK' => true, 'si' => true, 'sid_ET'=> true, 'sid' => true, 'sk_SK' => true,
101 'sk' => true, 'sl_SI' => true, 'sl' => true, 'so_DJ' => true, 'so_ET' => true,
102 'so_KE' => true, 'so_SO' => true, 'so' => true, 'sq_AL' => true, 'sq' => true,
103 'sr_BA' => true, 'sr_CS' => true, 'sr_ME' => true, 'sr_RS' => true, 'sr_YU' => true,
104 'sr' => true, 'ss_SZ' => true, 'ss_ZA' => true, 'ss' => true, 'st_LS' => true,
105 'st_ZA' => true, 'st' => true, 'sv_FI' => true, 'sv_SE' => true, 'sv' => true,
106 'sw_KE' => true, 'sw_TZ' => true, 'sw' => true, 'syr_SY'=> true, 'syr' => true,
107 'ta_IN' => true, 'ta' => true, 'te_IN' => true, 'te' => true, 'tg_TJ' => true,
108 'tg' => true, 'th_TH' => true, 'th' => true, 'ti_ER' => true, 'ti_ET' => true,
109 'ti' => true, 'tig_ER'=> true, 'tig' => true, 'tl' => true, 'tn_ZA' => true,
110 'tn' => true, 'to_TO' => true, 'to' => true, 'tr_TR' => true, 'tr' => true,
111 'trv_TW'=> true, 'trv' => true, 'ts_ZA' => true, 'ts' => true, 'tt_RU' => true,
112 'tt' => true, 'ug_CN' => true, 'ug' => true, 'uk_UA' => true, 'uk' => true,
113 'ur_IN' => true, 'ur_PK' => true, 'ur' => true, 'uz_AF' => true, 'uz_UZ' => true,
114 'uz' => true, 've_ZA' => true, 've' => true, 'vi_VN' => true, 'vi' => true,
115 'wal_ET'=> true, 'wal' => true, 'wo_SN' => true, 'wo' => true, 'xh_ZA' => true,
116 'xh' => true, 'yo_NG' => true, 'yo' => true, 'zh_CN' => true, 'zh_HK' => true,
117 'zh_MO' => true, 'zh_SG' => true, 'zh_TW' => true, 'zh' => true, 'zu_ZA' => true,
118 'zu' => true
122 * Class wide Locale Constants
124 * @var array $_territoryData
126 private static $_territoryData = array(
127 'AD' => 'ca_AD', 'AE' => 'ar_AE', 'AF' => 'fa_AF', 'AG' => 'en_AG', 'AI' => 'en_AI',
128 'AL' => 'sq_AL', 'AM' => 'hy_AM', 'AN' => 'pap_AN', 'AO' => 'pt_AO', 'AQ' => 'und_AQ',
129 'AR' => 'es_AR', 'AS' => 'sm_AS', 'AT' => 'de_AT', 'AU' => 'en_AU', 'AW' => 'nl_AW',
130 'AX' => 'sv_AX', 'AZ' => 'az_Latn_AZ', 'BA' => 'bs_BA', 'BB' => 'en_BB', 'BD' => 'bn_BD',
131 'BE' => 'nl_BE', 'BF' => 'mos_BF', 'BG' => 'bg_BG', 'BH' => 'ar_BH', 'BI' => 'rn_BI',
132 'BJ' => 'fr_BJ', 'BL' => 'fr_BL', 'BM' => 'en_BM', 'BN' => 'ms_BN', 'BO' => 'es_BO',
133 'BR' => 'pt_BR', 'BS' => 'en_BS', 'BT' => 'dz_BT', 'BV' => 'und_BV', 'BW' => 'en_BW',
134 'BY' => 'be_BY', 'BZ' => 'en_BZ', 'CA' => 'en_CA', 'CC' => 'ms_CC', 'CD' => 'sw_CD',
135 'CF' => 'fr_CF', 'CG' => 'fr_CG', 'CH' => 'de_CH', 'CI' => 'fr_CI', 'CK' => 'en_CK',
136 'CL' => 'es_CL', 'CM' => 'fr_CM', 'CN' => 'zh_Hans_CN', 'CO' => 'es_CO', 'CR' => 'es_CR',
137 'CU' => 'es_CU', 'CV' => 'kea_CV', 'CX' => 'en_CX', 'CY' => 'el_CY', 'CZ' => 'cs_CZ',
138 'DE' => 'de_DE', 'DJ' => 'aa_DJ', 'DK' => 'da_DK', 'DM' => 'en_DM', 'DO' => 'es_DO',
139 'DZ' => 'ar_DZ', 'EC' => 'es_EC', 'EE' => 'et_EE', 'EG' => 'ar_EG', 'EH' => 'ar_EH',
140 'ER' => 'ti_ER', 'ES' => 'es_ES', 'ET' => 'en_ET', 'FI' => 'fi_FI', 'FJ' => 'hi_FJ',
141 'FK' => 'en_FK', 'FM' => 'chk_FM', 'FO' => 'fo_FO', 'FR' => 'fr_FR', 'GA' => 'fr_GA',
142 'GB' => 'en_GB', 'GD' => 'en_GD', 'GE' => 'ka_GE', 'GF' => 'fr_GF', 'GG' => 'en_GG',
143 'GH' => 'ak_GH', 'GI' => 'en_GI', 'GL' => 'iu_GL', 'GM' => 'en_GM', 'GN' => 'fr_GN',
144 'GP' => 'fr_GP', 'GQ' => 'fan_GQ', 'GR' => 'el_GR', 'GS' => 'und_GS', 'GT' => 'es_GT',
145 'GU' => 'en_GU', 'GW' => 'pt_GW', 'GY' => 'en_GY', 'HK' => 'zh_Hant_HK', 'HM' => 'und_HM',
146 'HN' => 'es_HN', 'HR' => 'hr_HR', 'HT' => 'ht_HT', 'HU' => 'hu_HU', 'ID' => 'id_ID',
147 'IE' => 'en_IE', 'IL' => 'he_IL', 'IM' => 'en_IM', 'IN' => 'hi_IN', 'IO' => 'und_IO',
148 'IQ' => 'ar_IQ', 'IR' => 'fa_IR', 'IS' => 'is_IS', 'IT' => 'it_IT', 'JE' => 'en_JE',
149 'JM' => 'en_JM', 'JO' => 'ar_JO', 'JP' => 'ja_JP', 'KE' => 'en_KE', 'KG' => 'ky_Cyrl_KG',
150 'KH' => 'km_KH', 'KI' => 'en_KI', 'KM' => 'ar_KM', 'KN' => 'en_KN', 'KP' => 'ko_KP',
151 'KR' => 'ko_KR', 'KW' => 'ar_KW', 'KY' => 'en_KY', 'KZ' => 'ru_KZ', 'LA' => 'lo_LA',
152 'LB' => 'ar_LB', 'LC' => 'en_LC', 'LI' => 'de_LI', 'LK' => 'si_LK', 'LR' => 'en_LR',
153 'LS' => 'st_LS', 'LT' => 'lt_LT', 'LU' => 'fr_LU', 'LV' => 'lv_LV', 'LY' => 'ar_LY',
154 'MA' => 'ar_MA', 'MC' => 'fr_MC', 'MD' => 'ro_MD', 'ME' => 'sr_Latn_ME', 'MF' => 'fr_MF',
155 'MG' => 'mg_MG', 'MH' => 'mh_MH', 'MK' => 'mk_MK', 'ML' => 'bm_ML', 'MM' => 'my_MM',
156 'MN' => 'mn_Cyrl_MN', 'MO' => 'zh_Hant_MO', 'MP' => 'en_MP', 'MQ' => 'fr_MQ', 'MR' => 'ar_MR',
157 'MS' => 'en_MS', 'MT' => 'mt_MT', 'MU' => 'mfe_MU', 'MV' => 'dv_MV', 'MW' => 'ny_MW',
158 'MX' => 'es_MX', 'MY' => 'ms_MY', 'MZ' => 'pt_MZ', 'NA' => 'kj_NA', 'NC' => 'fr_NC',
159 'NE' => 'ha_Latn_NE', 'NF' => 'en_NF', 'NG' => 'en_NG', 'NI' => 'es_NI', 'NL' => 'nl_NL',
160 'NO' => 'nb_NO', 'NP' => 'ne_NP', 'NR' => 'en_NR', 'NU' => 'niu_NU', 'NZ' => 'en_NZ',
161 'OM' => 'ar_OM', 'PA' => 'es_PA', 'PE' => 'es_PE', 'PF' => 'fr_PF', 'PG' => 'tpi_PG',
162 'PH' => 'fil_PH', 'PK' => 'ur_PK', 'PL' => 'pl_PL', 'PM' => 'fr_PM', 'PN' => 'en_PN',
163 'PR' => 'es_PR', 'PS' => 'ar_PS', 'PT' => 'pt_PT', 'PW' => 'pau_PW', 'PY' => 'gn_PY',
164 'QA' => 'ar_QA', 'RE' => 'fr_RE', 'RO' => 'ro_RO', 'RS' => 'sr_Cyrl_RS', 'RU' => 'ru_RU',
165 'RW' => 'rw_RW', 'SA' => 'ar_SA', 'SB' => 'en_SB', 'SC' => 'crs_SC', 'SD' => 'ar_SD',
166 'SE' => 'sv_SE', 'SG' => 'en_SG', 'SH' => 'en_SH', 'SI' => 'sl_SI', 'SJ' => 'nb_SJ',
167 'SK' => 'sk_SK', 'SL' => 'kri_SL', 'SM' => 'it_SM', 'SN' => 'fr_SN', 'SO' => 'sw_SO',
168 'SR' => 'srn_SR', 'ST' => 'pt_ST', 'SV' => 'es_SV', 'SY' => 'ar_SY', 'SZ' => 'en_SZ',
169 'TC' => 'en_TC', 'TD' => 'fr_TD', 'TF' => 'und_TF', 'TG' => 'fr_TG', 'TH' => 'th_TH',
170 'TJ' => 'tg_Cyrl_TJ', 'TK' => 'tkl_TK', 'TL' => 'pt_TL', 'TM' => 'tk_TM', 'TN' => 'ar_TN',
171 'TO' => 'to_TO', 'TR' => 'tr_TR', 'TT' => 'en_TT', 'TV' => 'tvl_TV', 'TW' => 'zh_Hant_TW',
172 'TZ' => 'sw_TZ', 'UA' => 'uk_UA', 'UG' => 'sw_UG', 'UM' => 'en_UM', 'US' => 'en_US',
173 'UY' => 'es_UY', 'UZ' => 'uz_Cyrl_UZ', 'VA' => 'it_VA', 'VC' => 'en_VC', 'VE' => 'es_VE',
174 'VG' => 'en_VG', 'VI' => 'en_VI', 'VU' => 'bi_VU', 'WF' => 'wls_WF', 'WS' => 'sm_WS',
175 'YE' => 'ar_YE', 'YT' => 'swb_YT', 'ZA' => 'en_ZA', 'ZM' => 'en_ZM', 'ZW' => 'sn_ZW'
179 * Autosearch constants
181 const BROWSER = 'browser';
182 const ENVIRONMENT = 'environment';
183 const ZFDEFAULT = 'default';
186 * Defines if old behaviour should be supported
187 * Old behaviour throws notices and will be deleted in future releases
189 * @var boolean
191 public static $compatibilityMode = false;
194 * Internal variable
196 * @var boolean
198 private static $_breakChain = false;
201 * Actual set locale
203 * @var string Locale
205 protected $_locale;
208 * Automatic detected locale
210 * @var string Locales
212 protected static $_auto;
215 * Browser detected locale
217 * @var string Locales
219 protected static $_browser;
222 * Environment detected locale
224 * @var string Locales
226 protected static $_environment;
229 * Default locale
231 * @var string Locales
233 protected static $_default = array('en' => true);
236 * Generates a locale object
237 * If no locale is given a automatic search is done
238 * Then the most probable locale will be automatically set
239 * Search order is
240 * 1. Given Locale
241 * 2. HTTP Client
242 * 3. Server Environment
243 * 4. Framework Standard
245 * @param string|Zend_Locale $locale (Optional) Locale for parsing input
246 * @throws Zend_Locale_Exception When autodetection has been failed
248 public function __construct($locale = null)
250 $locale = self::_prepareLocale($locale);
251 $this->setLocale((string) $locale);
255 * Serialization Interface
257 * @return string
259 public function serialize()
261 return serialize($this);
265 * Returns a string representation of the object
267 * @return string
269 public function toString()
271 return (string) $this->_locale;
275 * Returns a string representation of the object
276 * Alias for toString
278 * @return string
280 public function __toString()
282 return $this->toString();
286 * Return the default locale
288 * @return array Returns an array of all locale string
290 public static function getDefault()
292 if ((self::$compatibilityMode === true) or (func_num_args() > 0)) {
293 if (!self::$_breakChain) {
294 self::$_breakChain = true;
295 trigger_error('You are running Zend_Locale in compatibility mode... please migrate your scripts', E_USER_NOTICE);
296 $params = func_get_args();
297 $param = null;
298 if (isset($params[0])) {
299 $param = $params[0];
301 return self::getOrder($param);
304 self::$_breakChain = false;
307 return self::$_default;
311 * Sets a new default locale which will be used when no locale can be detected
312 * If provided you can set a quality between 0 and 1 (or 2 and 100)
313 * which represents the percent of quality the browser
314 * requested within HTTP
316 * @param string|Zend_Locale $locale Locale to set
317 * @param float $quality The quality to set from 0 to 1
318 * @throws Zend_Locale_Exception When a autolocale was given
319 * @throws Zend_Locale_Exception When a unknown locale was given
320 * @return void
322 public static function setDefault($locale, $quality = 1)
324 if (($locale === 'auto') or ($locale === 'root') or ($locale === 'default') or
325 ($locale === 'environment') or ($locale === 'browser')) {
326 require_once 'Zend/Locale/Exception.php';
327 throw new Zend_Locale_Exception('Only full qualified locales can be used as default!');
330 if (($quality < 0.1) or ($quality > 100)) {
331 require_once 'Zend/Locale/Exception.php';
332 throw new Zend_Locale_Exception("Quality must be between 0.1 and 100");
335 if ($quality > 1) {
336 $quality /= 100;
339 $locale = self::_prepareLocale($locale);
340 if (isset(self::$_localeData[(string) $locale]) === true) {
341 self::$_default = array((string) $locale => $quality);
342 } else {
343 $elocale = explode('_', (string) $locale);
344 if (isset(self::$_localeData[$elocale[0]]) === true) {
345 self::$_default = array($elocale[0] => $quality);
346 } else {
347 require_once 'Zend/Locale/Exception.php';
348 throw new Zend_Locale_Exception("Unknown locale '" . (string) $locale . "' can not be set as default!");
352 self::$_auto = self::getBrowser() + self::getEnvironment() + self::getDefault();
356 * Expects the Systems standard locale
358 * For Windows:
359 * f.e.: LC_COLLATE=C;LC_CTYPE=German_Austria.1252;LC_MONETARY=C
360 * would be recognised as de_AT
362 * @return array
364 public static function getEnvironment()
366 if (self::$_environment !== null) {
367 return self::$_environment;
370 require_once 'Zend/Locale/Data/Translation.php';
372 $language = setlocale(LC_ALL, 0);
373 $languages = explode(';', $language);
374 $languagearray = array();
376 foreach ($languages as $locale) {
377 if (strpos($locale, '=') !== false) {
378 $language = substr($locale, strpos($locale, '='));
379 $language = substr($language, 1);
382 if ($language !== 'C') {
383 if (strpos($language, '.') !== false) {
384 $language = substr($language, 0, strpos($language, '.'));
385 } else if (strpos($language, '@') !== false) {
386 $language = substr($language, 0, strpos($language, '@'));
389 $language = str_ireplace(
390 array_keys(Zend_Locale_Data_Translation::$languageTranslation),
391 array_values(Zend_Locale_Data_Translation::$languageTranslation),
392 (string) $language
395 $language = str_ireplace(
396 array_keys(Zend_Locale_Data_Translation::$regionTranslation),
397 array_values(Zend_Locale_Data_Translation::$regionTranslation),
398 $language
401 if (isset(self::$_localeData[$language]) === true) {
402 $languagearray[$language] = 1;
403 if (strpos($language, '_') !== false) {
404 $languagearray[substr($language, 0, strpos($language, '_'))] = 1;
410 self::$_environment = $languagearray;
411 return $languagearray;
415 * Return an array of all accepted languages of the client
416 * Expects RFC compilant Header !!
418 * The notation can be :
419 * de,en-UK-US;q=0.5,fr-FR;q=0.2
421 * @return array - list of accepted languages including quality
423 public static function getBrowser()
425 if (self::$_browser !== null) {
426 return self::$_browser;
429 $httplanguages = getenv('HTTP_ACCEPT_LANGUAGE');
430 $languages = array();
431 if (empty($httplanguages) === true) {
432 return $languages;
435 $accepted = preg_split('/,\s*/', $httplanguages);
437 foreach ($accepted as $accept) {
438 $match = null;
439 $result = preg_match('/^([a-z]{1,8}(?:[-_][a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i',
440 $accept, $match);
442 if ($result < 1) {
443 continue;
446 if (isset($match[2]) === true) {
447 $quality = (float) $match[2];
448 } else {
449 $quality = 1.0;
452 $countrys = explode('-', $match[1]);
453 $region = array_shift($countrys);
455 $country2 = explode('_', $region);
456 $region = array_shift($country2);
458 foreach ($countrys as $country) {
459 $languages[$region . '_' . strtoupper($country)] = $quality;
462 foreach ($country2 as $country) {
463 $languages[$region . '_' . strtoupper($country)] = $quality;
466 if ((isset($languages[$region]) === false) || ($languages[$region] < $quality)) {
467 $languages[$region] = $quality;
471 self::$_browser = $languages;
472 return $languages;
476 * Sets a new locale
478 * @param string|Zend_Locale $locale (Optional) New locale to set
479 * @return void
481 public function setLocale($locale = null)
483 $locale = self::_prepareLocale($locale);
485 if (isset(self::$_localeData[(string) $locale]) === false) {
486 $region = substr((string) $locale, 0, 3);
487 if (isset($region[2]) === true) {
488 if (($region[2] === '_') or ($region[2] === '-')) {
489 $region = substr($region, 0, 2);
493 if (isset(self::$_localeData[(string) $region]) === true) {
494 $this->_locale = $region;
495 } else {
496 $this->_locale = 'root';
498 } else {
499 $this->_locale = $locale;
504 * Returns the language part of the locale
506 * @return string
508 public function getLanguage()
510 $locale = explode('_', $this->_locale);
511 return $locale[0];
515 * Returns the region part of the locale if available
517 * @return string|false - Regionstring
519 public function getRegion()
521 $locale = explode('_', $this->_locale);
522 if (isset($locale[1]) === true) {
523 return $locale[1];
526 return false;
530 * Return the accepted charset of the client
532 * @return string
534 public static function getHttpCharset()
536 $httpcharsets = getenv('HTTP_ACCEPT_CHARSET');
538 $charsets = array();
539 if ($httpcharsets === false) {
540 return $charsets;
543 $accepted = preg_split('/,\s*/', $httpcharsets);
544 foreach ($accepted as $accept) {
545 if (empty($accept) === true) {
546 continue;
549 if (strpos($accept, ';') !== false) {
550 $quality = (float) substr($accept, (strpos($accept, '=') + 1));
551 $pos = substr($accept, 0, strpos($accept, ';'));
552 $charsets[$pos] = $quality;
553 } else {
554 $quality = 1.0;
555 $charsets[$accept] = $quality;
559 return $charsets;
563 * Returns true if both locales are equal
565 * @param Zend_Locale $object Locale to check for equality
566 * @return boolean
568 public function equals(Zend_Locale $object)
570 if ($object->toString() === $this->toString()) {
571 return true;
574 return false;
578 * Returns localized informations as array, supported are several
579 * types of informations.
580 * For detailed information about the types look into the documentation
582 * @param string $path (Optional) Type of information to return
583 * @param string|Zend_Locale $locale (Optional) Locale|Language for which this informations should be returned
584 * @param string $value (Optional) Value for detail list
585 * @return array Array with the wished information in the given language
587 public static function getTranslationList($path = null, $locale = null, $value = null)
589 require_once 'Zend/Locale/Data.php';
590 $locale = self::findLocale($locale);
591 $result = Zend_Locale_Data::getList($locale, $path, $value);
592 if (empty($result) === true) {
593 return false;
596 return $result;
600 * Returns an array with the name of all languages translated to the given language
602 * @param string|Zend_Locale $locale (Optional) Locale for language translation
603 * @return array
604 * @deprecated
606 public static function getLanguageTranslationList($locale = null)
608 trigger_error("The method getLanguageTranslationList is deprecated. Use getTranslationList('language', $locale) instead", E_USER_NOTICE);
609 return self::getTranslationList('language', $locale);
613 * Returns an array with the name of all scripts translated to the given language
615 * @param string|Zend_Locale $locale (Optional) Locale for script translation
616 * @return array
617 * @deprecated
619 public static function getScriptTranslationList($locale = null)
621 trigger_error("The method getScriptTranslationList is deprecated. Use getTranslationList('script', $locale) instead", E_USER_NOTICE);
622 return self::getTranslationList('script', $locale);
626 * Returns an array with the name of all countries translated to the given language
628 * @param string|Zend_Locale $locale (Optional) Locale for country translation
629 * @return array
630 * @deprecated
632 public static function getCountryTranslationList($locale = null)
634 trigger_error("The method getCountryTranslationList is deprecated. Use getTranslationList('territory', $locale, 2) instead", E_USER_NOTICE);
635 return self::getTranslationList('territory', $locale, 2);
639 * Returns an array with the name of all territories translated to the given language
640 * All territories contains other countries.
642 * @param string|Zend_Locale $locale (Optional) Locale for territory translation
643 * @return array
644 * @deprecated
646 public static function getTerritoryTranslationList($locale = null)
648 trigger_error("The method getTerritoryTranslationList is deprecated. Use getTranslationList('territory', $locale, 1) instead", E_USER_NOTICE);
649 return self::getTranslationList('territory', $locale, 1);
653 * Returns a localized information string, supported are several types of informations.
654 * For detailed information about the types look into the documentation
656 * @param string $value Name to get detailed information about
657 * @param string $path (Optional) Type of information to return
658 * @param string|Zend_Locale $locale (Optional) Locale|Language for which this informations should be returned
659 * @return string|false The wished information in the given language
661 public static function getTranslation($value = null, $path = null, $locale = null)
663 require_once 'Zend/Locale/Data.php';
664 $locale = self::findLocale($locale);
665 $result = Zend_Locale_Data::getContent($locale, $path, $value);
666 if (empty($result) === true) {
667 return false;
670 return $result;
674 * Returns the localized language name
676 * @param string $value Name to get detailed information about
677 * @param string $locale (Optional) Locale for language translation
678 * @return array
679 * @deprecated
681 public static function getLanguageTranslation($value, $locale = null)
683 trigger_error("The method getLanguageTranslation is deprecated. Use getTranslation($value, 'language', $locale) instead", E_USER_NOTICE);
684 return self::getTranslation($value, 'language', $locale);
688 * Returns the localized script name
690 * @param string $value Name to get detailed information about
691 * @param string $locale (Optional) locale for script translation
692 * @return array
693 * @deprecated
695 public static function getScriptTranslation($value, $locale = null)
697 trigger_error("The method getScriptTranslation is deprecated. Use getTranslation($value, 'script', $locale) instead", E_USER_NOTICE);
698 return self::getTranslation($value, 'script', $locale);
702 * Returns the localized country name
704 * @param string $value Name to get detailed information about
705 * @param string|Zend_Locale $locale (Optional) Locale for country translation
706 * @return array
707 * @deprecated
709 public static function getCountryTranslation($value, $locale = null)
711 trigger_error("The method getCountryTranslation is deprecated. Use getTranslation($value, 'country', $locale) instead", E_USER_NOTICE);
712 return self::getTranslation($value, 'country', $locale);
716 * Returns the localized territory name
717 * All territories contains other countries.
719 * @param string $value Name to get detailed information about
720 * @param string|Zend_Locale $locale (Optional) Locale for territory translation
721 * @return array
722 * @deprecated
724 public static function getTerritoryTranslation($value, $locale = null)
726 trigger_error("The method getTerritoryTranslation is deprecated. Use getTranslation($value, 'territory', $locale) instead", E_USER_NOTICE);
727 return self::getTranslation($value, 'territory', $locale);
731 * Returns an array with translated yes strings
733 * @param string|Zend_Locale $locale (Optional) Locale for language translation (defaults to $this locale)
734 * @return array
736 public static function getQuestion($locale = null)
738 require_once 'Zend/Locale/Data.php';
739 $locale = self::findLocale($locale);
740 $quest = Zend_Locale_Data::getList($locale, 'question');
741 $yes = explode(':', $quest['yes']);
742 $no = explode(':', $quest['no']);
743 $quest['yes'] = $yes[0];
744 $quest['yesarray'] = $yes;
745 $quest['no'] = $no[0];
746 $quest['noarray'] = $no;
747 $quest['yesexpr'] = self::_prepareQuestionString($yes);
748 $quest['noexpr'] = self::_prepareQuestionString($no);
750 return $quest;
754 * Internal function for preparing the returned question regex string
756 * @param string $input Regex to parse
757 * @return string
759 private static function _prepareQuestionString($input)
761 $regex = '';
762 if (is_array($input) === true) {
763 $regex = '^';
764 $start = true;
765 foreach ($input as $row) {
766 if ($start === false) {
767 $regex .= '|';
770 $start = false;
771 $regex .= '(';
772 $one = null;
773 if (strlen($row) > 2) {
774 $one = true;
777 foreach (str_split($row, 1) as $char) {
778 $regex .= '[' . $char;
779 $regex .= strtoupper($char) . ']';
780 if ($one === true) {
781 $one = false;
782 $regex .= '(';
786 if ($one === false) {
787 $regex .= ')';
790 $regex .= '?)';
794 return $regex;
798 * Checks if a locale identifier is a real locale or not
799 * Examples:
800 * "en_XX" refers to "en", which returns true
801 * "XX_yy" refers to "root", which returns false
803 * @param string|Zend_Locale $locale Locale to check for
804 * @param boolean $strict (Optional) If true, no rerouting will be done when checking
805 * @param boolean $compatible (DEPRECATED) Only for internal usage, brakes compatibility mode
806 * @return boolean If the locale is known dependend on the settings
808 public static function isLocale($locale, $strict = false, $compatible = true)
810 if (($locale instanceof Zend_Locale)
811 || (is_string($locale) && array_key_exists($locale, self::$_localeData))
813 return true;
816 if (($locale === null) || (!is_string($locale) and !is_array($locale))) {
817 return false;
820 try {
821 $locale = self::_prepareLocale($locale, $strict);
822 } catch (Zend_Locale_Exception $e) {
823 return false;
826 if (($compatible === true) and (self::$compatibilityMode === true)) {
827 trigger_error('You are running Zend_Locale in compatibility mode... please migrate your scripts', E_USER_NOTICE);
828 if (isset(self::$_localeData[$locale]) === true) {
829 return $locale;
830 } else if (!$strict) {
831 $locale = explode('_', $locale);
832 if (isset(self::$_localeData[$locale[0]]) === true) {
833 return $locale[0];
836 } else {
837 if (isset(self::$_localeData[$locale]) === true) {
838 return true;
839 } else if (!$strict) {
840 $locale = explode('_', $locale);
841 if (isset(self::$_localeData[$locale[0]]) === true) {
842 return true;
847 return false;
851 * Finds the proper locale based on the input
852 * Checks if it exists, degrades it when necessary
853 * Detects registry locale and when all fails tries to detect a automatic locale
854 * Returns the found locale as string
856 * @param string $locale
857 * @throws Zend_Locale_Exception When the given locale is no locale or the autodetection fails
858 * @return string
860 public static function findLocale($locale = null)
862 if ($locale === null) {
863 require_once 'Zend/Registry.php';
864 if (Zend_Registry::isRegistered('Zend_Locale')) {
865 $locale = Zend_Registry::get('Zend_Locale');
869 if ($locale === null) {
870 $locale = new Zend_Locale();
873 if (!Zend_Locale::isLocale($locale, true, false)) {
874 if (!Zend_Locale::isLocale($locale, false, false)) {
875 $locale = Zend_Locale::getLocaleToTerritory($locale);
877 if (empty($locale)) {
878 require_once 'Zend/Locale/Exception.php';
879 throw new Zend_Locale_Exception("The locale '$locale' is no known locale");
881 } else {
882 $locale = new Zend_Locale($locale);
886 $locale = self::_prepareLocale($locale);
887 return $locale;
891 * Returns the expected locale for a given territory
893 * @param string $territory Territory for which the locale is being searched
894 * @return string|null Locale string or null when no locale has been found
896 public static function getLocaleToTerritory($territory)
898 $territory = strtoupper($territory);
899 if (array_key_exists($territory, self::$_territoryData)) {
900 return self::$_territoryData[$territory];
903 return null;
907 * Returns a list of all known locales where the locale is the key
908 * Only real locales are returned, the internal locales 'root', 'auto', 'browser'
909 * and 'environment' are suppressed
911 * @return array List of all Locales
913 public static function getLocaleList()
915 $list = self::$_localeData;
916 unset($list['root']);
917 unset($list['auto']);
918 unset($list['browser']);
919 unset($list['environment']);
920 return $list;
924 * Returns the set cache
926 * @return Zend_Cache_Core The set cache
928 public static function getCache()
930 require_once 'Zend/Locale/Data.php';
931 $cache = Zend_Locale_Data::getCache();
933 return $cache;
937 * Sets a cache
939 * @param Zend_Cache_Core $cache Cache to set
940 * @return void
942 public static function setCache(Zend_Cache_Core $cache)
944 require_once 'Zend/Locale/Data.php';
945 Zend_Locale_Data::setCache($cache);
949 * Returns true when a cache is set
951 * @return boolean
953 public static function hasCache()
955 require_once 'Zend/Locale/Data.php';
956 return Zend_Locale_Data::hasCache();
960 * Removes any set cache
962 * @return void
964 public static function removeCache()
966 require_once 'Zend/Locale/Data.php';
967 Zend_Locale_Data::removeCache();
971 * Clears all set cache data
973 * @return void
975 public static function clearCache()
977 require_once 'Zend/Locale/Data.php';
978 Zend_Locale_Data::clearCache();
982 * Disables the set cache
984 * @param boolean $flag True disables any set cache, default is false
985 * @return void
987 public static function disableCache($flag)
989 require_once 'Zend/Locale/Data.php';
990 Zend_Locale_Data::disableCache($flag);
994 * Internal function, returns a single locale on detection
996 * @param string|Zend_Locale $locale (Optional) Locale to work on
997 * @param boolean $strict (Optional) Strict preparation
998 * @throws Zend_Locale_Exception When no locale is set which is only possible when the class was wrong extended
999 * @return string
1001 private static function _prepareLocale($locale, $strict = false)
1003 if ($locale instanceof Zend_Locale) {
1004 $locale = $locale->toString();
1007 if (is_array($locale)) {
1008 return '';
1011 if (empty(self::$_auto) === true) {
1012 self::$_browser = self::getBrowser();
1013 self::$_environment = self::getEnvironment();
1014 self::$_breakChain = true;
1015 self::$_auto = self::getBrowser() + self::getEnvironment() + self::getDefault();
1018 if (!$strict) {
1019 if ($locale === 'browser') {
1020 $locale = self::$_browser;
1023 if ($locale === 'environment') {
1024 $locale = self::$_environment;
1027 if ($locale === 'default') {
1028 $locale = self::$_default;
1031 if (($locale === 'auto') or ($locale === null)) {
1032 $locale = self::$_auto;
1035 if (is_array($locale) === true) {
1036 $locale = key($locale);
1040 // This can only happen when someone extends Zend_Locale and erases the default
1041 if ($locale === null) {
1042 require_once 'Zend/Locale/Exception.php';
1043 throw new Zend_Locale_Exception('Autodetection of Locale has been failed!');
1046 if (strpos($locale, '-') !== false) {
1047 $locale = strtr($locale, '-', '_');
1050 $parts = explode('_', $locale);
1051 if (!isset(self::$_localeData[$parts[0]])) {
1052 if ((count($parts) == 1) && array_key_exists($parts[0], self::$_territoryData)) {
1053 return self::$_territoryData[$parts[0]];
1056 return '';
1059 foreach($parts as $key => $value) {
1060 if ((strlen($value) < 2) || (strlen($value) > 3)) {
1061 unset($parts[$key]);
1065 $locale = implode('_', $parts);
1066 return (string) $locale;
1070 * Search the locale automatically and return all used locales
1071 * ordered by quality
1073 * Standard Searchorder is Browser, Environment, Default
1075 * @param string $searchorder (Optional) Searchorder
1076 * @return array Returns an array of all detected locales
1078 public static function getOrder($order = null)
1080 switch ($order) {
1081 case self::ENVIRONMENT:
1082 self::$_breakChain = true;
1083 $languages = self::getEnvironment() + self::getBrowser() + self::getDefault();
1084 break;
1086 case self::ZFDEFAULT:
1087 self::$_breakChain = true;
1088 $languages = self::getDefault() + self::getEnvironment() + self::getBrowser();
1089 break;
1091 default:
1092 self::$_breakChain = true;
1093 $languages = self::getBrowser() + self::getEnvironment() + self::getDefault();
1094 break;
1097 return $languages;