2
0

Locale.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Locale
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Base class for localization
  23. *
  24. * @category Zend
  25. * @package Zend_Locale
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Locale
  30. {
  31. /**
  32. * Class wide Locale Constants
  33. *
  34. * @var array $_localeData
  35. */
  36. private static $_localeData = array(
  37. 'root' => true, 'aa_DJ' => true, 'aa_ER' => true, 'aa_ET' => true, 'aa' => true,
  38. 'af_NA' => true, 'af_ZA' => true, 'af' => true, 'ak_GH' => true, 'ak' => true,
  39. 'am_ET' => true, 'am' => true, 'ar_AE' => true, 'ar_BH' => true, 'ar_DZ' => true,
  40. 'ar_EG' => true, 'ar_IQ' => true, 'ar_JO' => true, 'ar_KW' => true, 'ar_LB' => true,
  41. 'ar_LY' => true, 'ar_MA' => true, 'ar_OM' => true, 'ar_QA' => true, 'ar_SA' => true,
  42. 'ar_SD' => true, 'ar_SY' => true, 'ar_TN' => true, 'ar_YE' => true, 'ar' => true,
  43. 'as_IN' => true, 'as' => true, 'az_AZ' => true, 'az' => true, 'be_BY' => true,
  44. 'be' => true, 'bg_BG' => true, 'bg' => true, 'bn_BD' => true, 'bn_IN' => true,
  45. 'bn' => true, 'bo_CN' => true, 'bo_IN' => true, 'bo' => true, 'bs_BA' => true,
  46. 'bs' => true, 'byn_ER'=> true, 'byn' => true, 'ca_ES' => true, 'ca' => true,
  47. 'cch_NG'=> true, 'cch' => true, 'cop' => true, 'cs_CZ' => true, 'cs' => true,
  48. 'cy_GB' => true, 'cy' => true, 'da_DK' => true, 'da' => true, 'de_AT' => true,
  49. 'de_BE' => true, 'de_CH' => true, 'de_DE' => true, 'de_LI' => true, 'de_LU' => true,
  50. 'de' => true, 'dv_MV' => true, 'dv' => true, 'dz_BT' => true, 'dz' => true,
  51. 'ee_GH' => true, 'ee_TG' => true, 'ee' => true, 'el_CY' => true, 'el_GR' => true,
  52. 'el' => true, 'en_AS' => true, 'en_AU' => true, 'en_BE' => true, 'en_BW' => true,
  53. 'en_BZ' => true, 'en_CA' => true, 'en_GB' => true, 'en_GU' => true, 'en_HK' => true,
  54. 'en_IE' => true, 'en_IN' => true, 'en_JM' => true, 'en_MH' => true, 'en_MP' => true,
  55. 'en_MT' => true, 'en_NA' => true, 'en_NZ' => true, 'en_PH' => true, 'en_PK' => true,
  56. 'en_SG' => true, 'en_TT' => true, 'en_UM' => true, 'en_US' => true, 'en_VI' => true,
  57. 'en_ZA' => true, 'en_ZW' => true, 'en' => true, 'eo' => true, 'es_AR' => true,
  58. 'es_BO' => true, 'es_CL' => true, 'es_CO' => true, 'es_CR' => true, 'es_DO' => true,
  59. 'es_EC' => true, 'es_ES' => true, 'es_GT' => true, 'es_HN' => true, 'es_MX' => true,
  60. 'es_NI' => true, 'es_PA' => true, 'es_PE' => true, 'es_PR' => true, 'es_PY' => true,
  61. 'es_SV' => true, 'es_US' => true, 'es_UY' => true, 'es_VE' => true, 'es' => true,
  62. 'et_EE' => true, 'et' => true, 'eu_ES' => true, 'eu' => true, 'fa_AF' => true,
  63. 'fa_IR' => true, 'fa' => true, 'fi_FI' => true, 'fi' => true, 'fil_PH'=> true,
  64. 'fil' => true, 'fo_FO' => true, 'fo' => true, 'fr_BE' => true, 'fr_CA' => true,
  65. 'fr_CH' => true, 'fr_FR' => true, 'fr_LU' => true, 'fr_MC' => true, 'fr_SN' => true,
  66. 'fr' => true, 'fur_IT'=> true, 'fur' => true, 'ga_IE' => true, 'ga' => true,
  67. 'gaa_GH'=> true, 'gaa' => true, 'gez_ER'=> true, 'gez_ET'=> true, 'gez' => true,
  68. 'gl_ES' => true, 'gl' => true, 'gsw_CH'=> true, 'gsw' => true, 'gu_IN' => true,
  69. 'gu' => true, 'gv_GB' => true, 'gv' => true, 'ha_GH' => true, 'ha_NE' => true,
  70. 'ha_NG' => true, 'ha_SD' => true, 'ha' => true, 'haw_US'=> true, 'haw' => true,
  71. 'he_IL' => true, 'he' => true, 'hi_IN' => true, 'hi' => true, 'hr_HR' => true,
  72. 'hr' => true, 'hu_HU' => true, 'hu' => true, 'hy_AM' => true, 'hy' => true,
  73. 'ia' => true, 'id_ID' => true, 'id' => true, 'ig_NG' => true, 'ig' => true,
  74. 'ii_CN' => true, 'ii' => true, 'in' => true, 'is_IS' => true, 'is' => true,
  75. 'it_CH' => true, 'it_IT' => true, 'it' => true, 'iu' => true, 'iw' => true,
  76. 'ja_JP' => true, 'ja' => true, 'ka_GE' => true, 'ka' => true, 'kaj_NG'=> true,
  77. 'kaj' => true, 'kam_KE'=> true, 'kam' => true, 'kcg_NG'=> true, 'kcg' => true,
  78. 'kfo_CI'=> true, 'kfo' => true, 'kk_KZ' => true, 'kk' => true, 'kl_GL' => true,
  79. 'kl' => true, 'km_KH' => true, 'km' => true, 'kn_IN' => true, 'kn' => true,
  80. 'ko_KR' => true, 'ko' => true, 'kok_IN'=> true, 'kok' => true, 'kpe_GN'=> true,
  81. 'kpe_LR'=> true, 'kpe' => true, 'ku_IQ' => true, 'ku_IR' => true, 'ku_SY' => true,
  82. 'ku_TR' => true, 'ku' => true, 'kw_GB' => true, 'kw' => true, 'ky_KG' => true,
  83. 'ky' => true, 'ln_CD' => true, 'ln_CG' => true, 'ln' => true, 'lo_LA' => true,
  84. 'lo' => true, 'lt_LT' => true, 'lt' => true, 'lv_LV' => true, 'lv' => true,
  85. 'mk_MK' => true, 'mk' => true, 'ml_IN' => true, 'ml' => true, 'mn_CN' => true,
  86. 'mn_MN' => true, 'mn' => true, 'mo' => true, 'mr_IN' => true, 'mr' => true,
  87. 'ms_BN' => true, 'ms_MY' => true, 'ms' => true, 'mt_MT' => true, 'mt' => true,
  88. 'my_MM' => true, 'my' => true, 'nb_NO' => true, 'nb' => true, 'nds_DE'=> true,
  89. 'nds' => true, 'ne_IN' => true, 'ne_NP' => true, 'ne' => true, 'nl_BE' => true,
  90. 'nl_NL' => true, 'nl' => true, 'nn_NO' => true, 'nn' => true, 'no' => true,
  91. 'nr_ZA' => true, 'nr' => true, 'nso_ZA'=> true, 'nso' => true, 'ny_MW' => true,
  92. 'ny' => true, 'oc_FR' => true, 'oc' => true, 'om_ET' => true, 'om_KE' => true,
  93. 'om' => true, 'or_IN' => true, 'or' => true, 'pa_IN' => true, 'pa_PK' => true,
  94. 'pa' => true, 'pl_PL' => true, 'pl' => true, 'ps_AF' => true, 'ps' => true,
  95. 'pt_BR' => true, 'pt_PT' => true, 'pt' => true, 'ro_MD' => true, 'ro_RO' => true,
  96. 'ro' => true, 'ru_RU' => true, 'ru_UA' => true, 'ru' => true, 'rw_RW' => true,
  97. 'rw' => true, 'sa_IN' => true, 'sa' => true, 'se_FI' => true, 'se_NO' => true,
  98. 'se' => true, 'sh_BA' => true, 'sh_CS' => true, 'sh_YU' => true, 'sh' => true,
  99. 'si_LK' => true, 'si' => true, 'sid_ET'=> true, 'sid' => true, 'sk_SK' => true,
  100. 'sk' => true, 'sl_SI' => true, 'sl' => true, 'so_DJ' => true, 'so_ET' => true,
  101. 'so_KE' => true, 'so_SO' => true, 'so' => true, 'sq_AL' => true, 'sq' => true,
  102. 'sr_BA' => true, 'sr_CS' => true, 'sr_ME' => true, 'sr_RS' => true, 'sr_YU' => true,
  103. 'sr' => true, 'ss_SZ' => true, 'ss_ZA' => true, 'ss' => true, 'st_LS' => true,
  104. 'st_ZA' => true, 'st' => true, 'sv_FI' => true, 'sv_SE' => true, 'sv' => true,
  105. 'sw_KE' => true, 'sw_TZ' => true, 'sw' => true, 'syr_SY'=> true, 'syr' => true,
  106. 'ta_IN' => true, 'ta' => true, 'te_IN' => true, 'te' => true, 'tg_TJ' => true,
  107. 'tg' => true, 'th_TH' => true, 'th' => true, 'ti_ER' => true, 'ti_ET' => true,
  108. 'ti' => true, 'tig_ER'=> true, 'tig' => true, 'tl' => true, 'tn_ZA' => true,
  109. 'tn' => true, 'to_TO' => true, 'to' => true, 'tr_TR' => true, 'tr' => true,
  110. 'trv_TW'=> true, 'trv' => true, 'ts_ZA' => true, 'ts' => true, 'tt_RU' => true,
  111. 'tt' => true, 'ug_CN' => true, 'ug' => true, 'uk_UA' => true, 'uk' => true,
  112. 'ur_IN' => true, 'ur_PK' => true, 'ur' => true, 'uz_AF' => true, 'uz_UZ' => true,
  113. 'uz' => true, 've_ZA' => true, 've' => true, 'vi_VN' => true, 'vi' => true,
  114. 'wal_ET'=> true, 'wal' => true, 'wo_SN' => true, 'wo' => true, 'xh_ZA' => true,
  115. 'xh' => true, 'yo_NG' => true, 'yo' => true, 'zh_CN' => true, 'zh_HK' => true,
  116. 'zh_MO' => true, 'zh_SG' => true, 'zh_TW' => true, 'zh' => true, 'zu_ZA' => true,
  117. 'zu' => true
  118. );
  119. /**
  120. * Autosearch constants
  121. */
  122. const BROWSER = 'browser';
  123. const ENVIRONMENT = 'environment';
  124. const ZFDEFAULT = 'default';
  125. /**
  126. * Defines if old behaviour should be supported
  127. * Old behaviour throws notices and will be deleted in future releases
  128. *
  129. * @var boolean
  130. */
  131. public static $compatibilityMode = false;
  132. /**
  133. * Internal variable
  134. *
  135. * @var boolean
  136. */
  137. private static $_breakChain = false;
  138. /**
  139. * Actual set locale
  140. *
  141. * @var string Locale
  142. */
  143. protected $_locale;
  144. /**
  145. * Automatic detected locale
  146. *
  147. * @var string Locales
  148. */
  149. protected static $_auto;
  150. /**
  151. * Browser detected locale
  152. *
  153. * @var string Locales
  154. */
  155. protected static $_browser;
  156. /**
  157. * Environment detected locale
  158. *
  159. * @var string Locales
  160. */
  161. protected static $_environment;
  162. /**
  163. * Default locale
  164. *
  165. * @var string Locales
  166. */
  167. protected static $_default = array('en' => true);
  168. /**
  169. * Generates a locale object
  170. * If no locale is given a automatic search is done
  171. * Then the most probable locale will be automatically set
  172. * Search order is
  173. * 1. Given Locale
  174. * 2. HTTP Client
  175. * 3. Server Environment
  176. * 4. Framework Standard
  177. *
  178. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  179. * @throws Zend_Locale_Exception When autodetection has been failed
  180. */
  181. public function __construct($locale = null)
  182. {
  183. $locale = self::_prepareLocale($locale);
  184. $this->setLocale((string) $locale);
  185. }
  186. /**
  187. * Serialization Interface
  188. *
  189. * @return string
  190. */
  191. public function serialize()
  192. {
  193. return serialize($this);
  194. }
  195. /**
  196. * Returns a string representation of the object
  197. *
  198. * @return string
  199. */
  200. public function toString()
  201. {
  202. return (string) $this->_locale;
  203. }
  204. /**
  205. * Returns a string representation of the object
  206. * Alias for toString
  207. *
  208. * @return string
  209. */
  210. public function __toString()
  211. {
  212. return $this->toString();
  213. }
  214. /**
  215. * Return the default locale
  216. *
  217. * @return array Returns an array of all locale string
  218. */
  219. public static function getDefault()
  220. {
  221. if ((self::$compatibilityMode === true) or (func_num_args() > 0)) {
  222. if (!self::$_breakChain) {
  223. self::$_breakChain = true;
  224. trigger_error('You are running Zend_Locale in compatibility mode... please migrate your scripts', E_USER_NOTICE);
  225. $params = func_get_args();
  226. $param = null;
  227. if (isset($params[0])) {
  228. $param = $params[0];
  229. }
  230. return self::getOrder($param);
  231. }
  232. self::$_breakChain = false;
  233. }
  234. return self::$_default;
  235. }
  236. /**
  237. * Sets a new default locale
  238. * If provided you can set a quality between 0 and 1 (or 2 and 100)
  239. * which represents the percent of quality the browser
  240. * requested within HTTP
  241. *
  242. * @param string|Zend_Locale $locale Locale to set
  243. * @param float $quality The quality to set from 0 to 1
  244. * @throws Zend_Locale_Exception When a autolocale was given
  245. * @throws Zend_Locale_Exception When a unknown locale was given
  246. * @return void
  247. */
  248. public static function setDefault($locale, $quality = 1)
  249. {
  250. if (($locale === 'auto') or ($locale === 'root') or ($locale === 'default') or
  251. ($locale === 'environment') or ($locale === 'browser')) {
  252. require_once 'Zend/Locale/Exception.php';
  253. throw new Zend_Locale_Exception('Only full qualified locales can be used as default!');
  254. }
  255. if (($quality < 0.1) or ($quality > 100)) {
  256. require_once 'Zend/Locale/Exception.php';
  257. throw new Zend_Locale_Exception("Quality must be between 0.1 and 100");
  258. }
  259. if ($quality > 1) {
  260. $quality /= 100;
  261. }
  262. if (isset(self::$_localeData[(string) $locale]) === true) {
  263. self::$_default = array((string) $locale => $quality);
  264. } else {
  265. $locale = explode('_', (string) $locale);
  266. if (isset(self::$_localeData[$locale[0]]) === true) {
  267. self::$_default = array($locale[0] => $quality);
  268. } else {
  269. require_once 'Zend/Locale/Exception.php';
  270. throw new Zend_Locale_Exception("Unknown locale '" . (string) $locale . "' can not be set as default!");
  271. }
  272. }
  273. }
  274. /**
  275. * Expects the Systems standard locale
  276. *
  277. * For Windows:
  278. * f.e.: LC_COLLATE=C;LC_CTYPE=German_Austria.1252;LC_MONETARY=C
  279. * would be recognised as de_AT
  280. *
  281. * @return array
  282. */
  283. public static function getEnvironment()
  284. {
  285. if (self::$_environment !== null) {
  286. return self::$_environment;
  287. }
  288. require_once 'Zend/Locale/Data/Translation.php';
  289. $language = setlocale(LC_ALL, 0);
  290. $languages = explode(';', $language);
  291. $languagearray = array();
  292. foreach ($languages as $locale) {
  293. if (strpos($locale, '=') !== false) {
  294. $language = substr($locale, strpos($locale, '='));
  295. $language = substr($language, 1);
  296. }
  297. if ($language !== 'C') {
  298. if (strpos($language, '.') !== false) {
  299. $language = substr($language, 0, (strpos($language, '.') - 1));
  300. } else if (strpos($language, '@') !== false) {
  301. $language = substr($language, 0, (strpos($language, '@') - 1));
  302. }
  303. $splitted = explode('_', $language);
  304. $language = (string) $language;
  305. if (isset(self::$_localeData[$language]) === true) {
  306. $languagearray[$language] = 1;
  307. if (strlen($language) > 4) {
  308. $languagearray[substr($language, 0, 2)] = 1;
  309. }
  310. continue;
  311. }
  312. if (empty(Zend_Locale_Data_Translation::$localeTranslation[$splitted[0]]) === false) {
  313. if (empty(Zend_Locale_Data_Translation::$localeTranslation[$splitted[1]]) === false) {
  314. $languagearray[Zend_Locale_Data_Translation::$localeTranslation[$splitted[0]] . '_' .
  315. Zend_Locale_Data_Translation::$localeTranslation[$splitted[1]]] = 1;
  316. }
  317. $languagearray[Zend_Locale_Data_Translation::$localeTranslation[$splitted[0]]] = 1;
  318. }
  319. }
  320. }
  321. self::$_environment = $languagearray;
  322. return $languagearray;
  323. }
  324. /**
  325. * Return an array of all accepted languages of the client
  326. * Expects RFC compilant Header !!
  327. *
  328. * The notation can be :
  329. * de,en-UK-US;q=0.5,fr-FR;q=0.2
  330. *
  331. * @return array - list of accepted languages including quality
  332. */
  333. public static function getBrowser()
  334. {
  335. if (self::$_browser !== null) {
  336. return self::$_browser;
  337. }
  338. $httplanguages = getenv('HTTP_ACCEPT_LANGUAGE');
  339. $languages = array();
  340. if (empty($httplanguages) === true) {
  341. return $languages;
  342. }
  343. $accepted = preg_split('/,\s*/', $httplanguages);
  344. foreach ($accepted as $accept) {
  345. $match = null;
  346. $result = preg_match('/^([a-z]{1,8}(?:[-_][a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i',
  347. $accept, $match);
  348. if ($result < 1) {
  349. continue;
  350. }
  351. if (isset($match[2]) === true) {
  352. $quality = (float) $match[2];
  353. } else {
  354. $quality = 1.0;
  355. }
  356. $countrys = explode('-', $match[1]);
  357. $region = array_shift($countrys);
  358. $country2 = explode('_', $region);
  359. $region = array_shift($country2);
  360. foreach ($countrys as $country) {
  361. $languages[$region . '_' . strtoupper($country)] = $quality;
  362. }
  363. foreach ($country2 as $country) {
  364. $languages[$region . '_' . strtoupper($country)] = $quality;
  365. }
  366. if ((isset($languages[$region]) === false) || ($languages[$region] < $quality)) {
  367. $languages[$region] = $quality;
  368. }
  369. }
  370. self::$_browser = $languages;
  371. return $languages;
  372. }
  373. /**
  374. * Sets a new locale
  375. *
  376. * @param string|Zend_Locale $locale (Optional) New locale to set
  377. * @return void
  378. */
  379. public function setLocale($locale = null)
  380. {
  381. $locale = self::_prepareLocale($locale);
  382. if (isset(self::$_localeData[(string) $locale]) === false) {
  383. $region = substr((string) $locale, 0, 3);
  384. if (isset($region[2]) === true) {
  385. if (($region[2] === '_') or ($region[2] === '-')) {
  386. $region = substr($region, 0, 2);
  387. }
  388. }
  389. if (isset(self::$_localeData[(string) $region]) === true) {
  390. $this->_locale = $region;
  391. } else {
  392. $this->_locale = 'root';
  393. }
  394. } else {
  395. $this->_locale = $locale;
  396. }
  397. }
  398. /**
  399. * Returns the language part of the locale
  400. *
  401. * @return string
  402. */
  403. public function getLanguage()
  404. {
  405. $locale = explode('_', $this->_locale);
  406. return $locale[0];
  407. }
  408. /**
  409. * Returns the region part of the locale if available
  410. *
  411. * @return string|false - Regionstring
  412. */
  413. public function getRegion()
  414. {
  415. $locale = explode('_', $this->_locale);
  416. if (isset($locale[1]) === true) {
  417. return $locale[1];
  418. }
  419. return false;
  420. }
  421. /**
  422. * Return the accepted charset of the client
  423. *
  424. * @return string
  425. */
  426. public static function getHttpCharset()
  427. {
  428. $httpcharsets = getenv('HTTP_ACCEPT_CHARSET');
  429. $charsets = array();
  430. if ($httpcharsets === false) {
  431. return $charsets;
  432. }
  433. $accepted = preg_split('/,\s*/', $httpcharsets);
  434. foreach ($accepted as $accept) {
  435. if (empty($accept) === true) {
  436. continue;
  437. }
  438. if (strpos($accept, ';') !== false) {
  439. $quality = (float) substr($accept, (strpos($accept, '=') + 1));
  440. $pos = substr($accept, 0, strpos($accept, ';'));
  441. $charsets[$pos] = $quality;
  442. } else {
  443. $quality = 1.0;
  444. $charsets[$accept] = $quality;
  445. }
  446. }
  447. return $charsets;
  448. }
  449. /**
  450. * Returns true if both locales are equal
  451. *
  452. * @param Zend_Locale $object Locale to check for equality
  453. * @return boolean
  454. */
  455. public function equals(Zend_Locale $object)
  456. {
  457. if ($object->toString() === $this->toString()) {
  458. return true;
  459. }
  460. return false;
  461. }
  462. /**
  463. * Returns localized informations as array, supported are several
  464. * types of informations.
  465. * For detailed information about the types look into the documentation
  466. *
  467. * @param string $path (Optional) Type of information to return
  468. * @param string|Zend_Locale $locale (Optional) Locale|Language for which this informations should be returned
  469. * @param string $value (Optional) Value for detail list
  470. * @return array Array with the wished information in the given language
  471. */
  472. public static function getTranslationList($path = null, $locale = null, $value = null)
  473. {
  474. require_once 'Zend/Locale/Data.php';
  475. $locale = self::_prepareLocale($locale);
  476. $result = Zend_Locale_Data::getList($locale, $path, $value);
  477. if (empty($result) === true) {
  478. return false;
  479. }
  480. return $result;
  481. }
  482. /**
  483. * Returns an array with the name of all languages translated to the given language
  484. *
  485. * @param string|Zend_Locale $locale (Optional) Locale for language translation
  486. * @return array
  487. * @deprecated
  488. */
  489. public static function getLanguageTranslationList($locale = null)
  490. {
  491. trigger_error("The method getLanguageTranslationList is deprecated. Use getTranslationList('language', $locale) instead", E_USER_NOTICE);
  492. return self::getTranslationList('language', $locale);
  493. }
  494. /**
  495. * Returns an array with the name of all scripts translated to the given language
  496. *
  497. * @param string|Zend_Locale $locale (Optional) Locale for script translation
  498. * @return array
  499. * @deprecated
  500. */
  501. public static function getScriptTranslationList($locale = null)
  502. {
  503. trigger_error("The method getScriptTranslationList is deprecated. Use getTranslationList('script', $locale) instead", E_USER_NOTICE);
  504. return self::getTranslationList('script', $locale);
  505. }
  506. /**
  507. * Returns an array with the name of all countries translated to the given language
  508. *
  509. * @param string|Zend_Locale $locale (Optional) Locale for country translation
  510. * @return array
  511. * @deprecated
  512. */
  513. public static function getCountryTranslationList($locale = null)
  514. {
  515. trigger_error("The method getCountryTranslationList is deprecated. Use getTranslationList('territory', $locale, 2) instead", E_USER_NOTICE);
  516. return self::getTranslationList('territory', $locale, 2);
  517. }
  518. /**
  519. * Returns an array with the name of all territories translated to the given language
  520. * All territories contains other countries.
  521. *
  522. * @param string|Zend_Locale $locale (Optional) Locale for territory translation
  523. * @return array
  524. * @deprecated
  525. */
  526. public static function getTerritoryTranslationList($locale = null)
  527. {
  528. trigger_error("The method getTerritoryTranslationList is deprecated. Use getTranslationList('territory', $locale, 1) instead", E_USER_NOTICE);
  529. return self::getTranslationList('territory', $locale, 1);
  530. }
  531. /**
  532. * Returns a localized information string, supported are several types of informations.
  533. * For detailed information about the types look into the documentation
  534. *
  535. * @param string $value Name to get detailed information about
  536. * @param string $path (Optional) Type of information to return
  537. * @param string|Zend_Locale $locale (Optional) Locale|Language for which this informations should be returned
  538. * @return string|false The wished information in the given language
  539. */
  540. public static function getTranslation($value = null, $path = null, $locale = null)
  541. {
  542. require_once 'Zend/Locale/Data.php';
  543. $locale = self::_prepareLocale($locale);
  544. $result = Zend_Locale_Data::getContent($locale, $path, $value);
  545. if (empty($result) === true) {
  546. return false;
  547. }
  548. return $result;
  549. }
  550. /**
  551. * Returns the localized language name
  552. *
  553. * @param string $value Name to get detailed information about
  554. * @param string $locale (Optional) Locale for language translation
  555. * @return array
  556. * @deprecated
  557. */
  558. public static function getLanguageTranslation($value, $locale = null)
  559. {
  560. trigger_error("The method getLanguageTranslation is deprecated. Use getTranslation($value, 'language', $locale) instead", E_USER_NOTICE);
  561. return self::getTranslation($value, 'language', $locale);
  562. }
  563. /**
  564. * Returns the localized script name
  565. *
  566. * @param string $value Name to get detailed information about
  567. * @param string $locale (Optional) locale for script translation
  568. * @return array
  569. * @deprecated
  570. */
  571. public static function getScriptTranslation($value, $locale = null)
  572. {
  573. trigger_error("The method getScriptTranslation is deprecated. Use getTranslation($value, 'script', $locale) instead", E_USER_NOTICE);
  574. return self::getTranslation($value, 'script', $locale);
  575. }
  576. /**
  577. * Returns the localized country name
  578. *
  579. * @param string $value Name to get detailed information about
  580. * @param string|Zend_Locale $locale (Optional) Locale for country translation
  581. * @return array
  582. * @deprecated
  583. */
  584. public static function getCountryTranslation($value, $locale = null)
  585. {
  586. trigger_error("The method getCountryTranslation is deprecated. Use getTranslation($value, 'country', $locale) instead", E_USER_NOTICE);
  587. return self::getTranslation($value, 'country', $locale);
  588. }
  589. /**
  590. * Returns the localized territory name
  591. * All territories contains other countries.
  592. *
  593. * @param string $value Name to get detailed information about
  594. * @param string|Zend_Locale $locale (Optional) Locale for territory translation
  595. * @return array
  596. * @deprecated
  597. */
  598. public static function getTerritoryTranslation($value, $locale = null)
  599. {
  600. trigger_error("The method getTerritoryTranslation is deprecated. Use getTranslation($value, 'territory', $locale) instead", E_USER_NOTICE);
  601. return self::getTranslation($value, 'territory', $locale);
  602. }
  603. /**
  604. * Returns an array with translated yes strings
  605. *
  606. * @param string|Zend_Locale $locale (Optional) Locale for language translation (defaults to $this locale)
  607. * @return array
  608. */
  609. public static function getQuestion($locale = null)
  610. {
  611. require_once 'Zend/Locale/Data.php';
  612. $locale = self::_prepareLocale($locale);
  613. $quest = Zend_Locale_Data::getList($locale, 'question');
  614. $yes = explode(':', $quest['yes']);
  615. $no = explode(':', $quest['no']);
  616. $quest['yes'] = $yes[0];
  617. $quest['yesarray'] = $yes;
  618. $quest['no'] = $no[0];
  619. $quest['noarray'] = $no;
  620. $quest['yesexpr'] = self::_prepareQuestionString($yes);
  621. $quest['noexpr'] = self::_prepareQuestionString($no);
  622. return $quest;
  623. }
  624. /**
  625. * Internal function for preparing the returned question regex string
  626. *
  627. * @param string $input Regex to parse
  628. * @return string
  629. */
  630. private static function _prepareQuestionString($input)
  631. {
  632. $regex = '';
  633. if (is_array($input) === true) {
  634. $regex = '^';
  635. $start = true;
  636. foreach ($input as $row) {
  637. if ($start === false) {
  638. $regex .= '|';
  639. }
  640. $start = false;
  641. $regex .= '(';
  642. $one = null;
  643. if (strlen($row) > 2) {
  644. $one = true;
  645. }
  646. foreach (str_split($row, 1) as $char) {
  647. $regex .= '[' . $char;
  648. $regex .= strtoupper($char) . ']';
  649. if ($one === true) {
  650. $one = false;
  651. $regex .= '(';
  652. }
  653. }
  654. if ($one === false) {
  655. $regex .= ')';
  656. }
  657. $regex .= '?)';
  658. }
  659. }
  660. return $regex;
  661. }
  662. /**
  663. * Checks if a locale identifier is a real locale or not
  664. * Examples:
  665. * "en_XX" refers to "en", which returns true
  666. * "XX_yy" refers to "root", which returns false
  667. *
  668. * @param string|Zend_Locale $locale Locale to check for
  669. * @param boolean $strict (Optional) If true, no rerouting will be done when checking
  670. * @param boolean $compatible (DEPRECIATED) Only for internal usage, brakes compatibility mode
  671. * @return boolean If the locale is known dependend on the settings
  672. */
  673. public static function isLocale($locale, $strict = false, $compatible = true)
  674. {
  675. try {
  676. $locale = self::_prepareLocale($locale, $strict);
  677. } catch (Zend_Locale_Exception $e) {
  678. return false;
  679. }
  680. if (($compatible === true) and (self::$compatibilityMode === true)) {
  681. trigger_error('You are running Zend_Locale in compatibility mode... please migrate your scripts', E_USER_NOTICE);
  682. if (isset(self::$_localeData[$locale]) === true) {
  683. return $locale;
  684. } else if (!$strict) {
  685. $locale = explode('_', $locale);
  686. if (isset(self::$_localeData[$locale[0]]) === true) {
  687. return $locale[0];
  688. }
  689. }
  690. } else {
  691. if (isset(self::$_localeData[$locale]) === true) {
  692. return true;
  693. } else if (!$strict) {
  694. $locale = explode('_', $locale);
  695. if (isset(self::$_localeData[$locale[0]]) === true) {
  696. return true;
  697. }
  698. }
  699. }
  700. return false;
  701. }
  702. /**
  703. * Finds the proper locale based on the input
  704. * Checks if it exists, degrades it when necessary
  705. * Detects registry locale and when all fails tries to detect a automatic locale
  706. * Returns the found locale as string
  707. *
  708. * @param string $locale
  709. * @throws Zend_Locale_Exception When the given locale is no locale or the autodetection fails
  710. * @return string
  711. */
  712. public static function findLocale($locale = null)
  713. {
  714. if ($locale === null) {
  715. require_once 'Zend/Registry.php';
  716. if (Zend_Registry::isRegistered('Zend_Locale')) {
  717. $locale = Zend_Registry::get('Zend_Locale');
  718. }
  719. }
  720. require_once 'Zend/Locale.php';
  721. if ($locale === null) {
  722. $locale = new Zend_Locale();
  723. }
  724. if (!Zend_Locale::isLocale($locale, true, false)) {
  725. if (!Zend_Locale::isLocale($locale, false, false)) {
  726. require_once 'Zend/Locale/Exception.php';
  727. throw new Zend_Locale_Exception("The locale '$locale' is no known locale");
  728. }
  729. $locale = new Zend_Locale($locale);
  730. }
  731. if ($locale instanceof Zend_Locale) {
  732. $locale = $locale->toString();
  733. }
  734. return $locale;
  735. }
  736. /**
  737. * Returns a list of all known locales where the locale is the key
  738. * Only real locales are returned, the internal locales 'root', 'auto', 'browser'
  739. * and 'environment' are suppressed
  740. *
  741. * @return array List of all Locales
  742. */
  743. public static function getLocaleList()
  744. {
  745. $list = self::$_localeData;
  746. unset($list['root']);
  747. unset($list['auto']);
  748. unset($list['browser']);
  749. unset($list['environment']);
  750. return $list;
  751. }
  752. /**
  753. * Returns the set cache
  754. *
  755. * @return Zend_Cache_Core The set cache
  756. */
  757. public static function getCache()
  758. {
  759. require_once 'Zend/Locale/Data.php';
  760. $cache = Zend_Locale_Data::getCache();
  761. return $cache;
  762. }
  763. /**
  764. * Sets a cache
  765. *
  766. * @param Zend_Cache_Core $cache Cache to set
  767. * @return void
  768. */
  769. public static function setCache(Zend_Cache_Core $cache)
  770. {
  771. require_once 'Zend/Locale/Data.php';
  772. Zend_Locale_Data::setCache($cache);
  773. }
  774. /**
  775. * Returns true when a cache is set
  776. *
  777. * @return boolean
  778. */
  779. public static function hasCache()
  780. {
  781. require_once 'Zend/Locale/Data.php';
  782. return Zend_Locale_Data::hasCache();
  783. }
  784. /**
  785. * Removes any set cache
  786. *
  787. * @return void
  788. */
  789. public static function removeCache()
  790. {
  791. require_once 'Zend/Locale/Data.php';
  792. Zend_Locale_Data::removeCache();
  793. }
  794. /**
  795. * Clears all set cache data
  796. *
  797. * @return void
  798. */
  799. public static function clearCache()
  800. {
  801. require_once 'Zend/Locale/Data.php';
  802. Zend_Locale_Data::clearCache();
  803. }
  804. /**
  805. * Disables the set cache
  806. *
  807. * @param boolean $flag True disables any set cache, default is false
  808. * @return void
  809. */
  810. public static function disableCache($flag)
  811. {
  812. require_once 'Zend/Locale/Data.php';
  813. Zend_Locale_Data::disableCache($flag);
  814. }
  815. /**
  816. * Internal function, returns a single locale on detection
  817. *
  818. * @param string|Zend_Locale $locale (Optional) Locale to work on
  819. * @param boolean $strict (Optional) Strict preparation
  820. * @throws Zend_Locale_Exception When no locale is set which is only possible when the class was wrong extended
  821. * @return string
  822. */
  823. private static function _prepareLocale($locale, $strict = false)
  824. {
  825. if ($locale instanceof Zend_Locale) {
  826. $locale = $locale->toString();
  827. }
  828. if (is_array($locale)) {
  829. return '';
  830. }
  831. if (empty(self::$_auto) === true) {
  832. self::$_browser = self::getBrowser();
  833. self::$_environment = self::getEnvironment();
  834. self::$_breakChain = true;
  835. self::$_auto = self::getBrowser() + self::getEnvironment() + self::getDefault();
  836. }
  837. if (!$strict) {
  838. if ($locale === 'browser') {
  839. $locale = self::$_browser;
  840. }
  841. if ($locale === 'environment') {
  842. $locale = self::$_environment;
  843. }
  844. if ($locale === 'default') {
  845. $locale = self::$_default;
  846. }
  847. if (($locale === 'auto') or ($locale === null)) {
  848. $locale = self::$_auto;
  849. }
  850. if (is_array($locale) === true) {
  851. $locale = key($locale);
  852. }
  853. }
  854. // This can only happen when someone extends Zend_Locale and erases the default
  855. if ($locale === null) {
  856. require_once 'Zend/Locale/Exception.php';
  857. throw new Zend_Locale_Exception('Autodetection of Locale has been failed!');
  858. }
  859. if (strpos($locale, '-') !== false) {
  860. $locale = strtr($locale, '-', '_');
  861. }
  862. $parts = explode('_', $locale);
  863. foreach($parts as $key => $value) {
  864. if ((strlen($value) < 2) || (strlen($value) > 3)) {
  865. unset($parts[$key]);
  866. }
  867. }
  868. $locale = implode('_', $parts);
  869. return (string) $locale;
  870. }
  871. /**
  872. * Search the locale automatically and return all used locales
  873. * ordered by quality
  874. *
  875. * Standard Searchorder is Browser, Environment, Default
  876. *
  877. * @param string $searchorder (Optional) Searchorder
  878. * @return array Returns an array of all detected locales
  879. */
  880. public static function getOrder($order = null)
  881. {
  882. switch ($order) {
  883. case self::ENVIRONMENT:
  884. self::$_breakChain = true;
  885. $languages = self::getEnvironment() + self::getBrowser() + self::getDefault();
  886. break;
  887. case self::ZFDEFAULT:
  888. self::$_breakChain = true;
  889. $languages = self::getDefault() + self::getEnvironment() + self::getBrowser();
  890. break;
  891. default:
  892. self::$_breakChain = true;
  893. $languages = self::getBrowser() + self::getEnvironment() + self::getDefault();
  894. break;
  895. }
  896. return $languages;
  897. }
  898. }