Locale.php 35 KB

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