Locale.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 language
  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. */
  484. public static function getLanguageTranslationList($locale = null)
  485. {
  486. return self::getTranslationList('language', $locale);
  487. }
  488. /**
  489. * Returns an array with the name of all scripts translated to the given language
  490. *
  491. * @param string|Zend_Locale $locale (Optional) Locale for script translation
  492. * @return array
  493. */
  494. public static function getScriptTranslationList($locale = null)
  495. {
  496. return self::getTranslationList('script', $locale);
  497. }
  498. /**
  499. * Returns an array with the name of all countries translated to the given language
  500. *
  501. * @param string|Zend_Locale $locale (Optional) Locale for country translation
  502. * @return array
  503. */
  504. public static function getCountryTranslationList($locale = null)
  505. {
  506. return self::getTranslationList('territory', $locale, 2);
  507. }
  508. /**
  509. * Returns an array with the name of all territories translated to the given language
  510. * All territories contains other countries.
  511. *
  512. * @param string|Zend_Locale $locale (Optional) Locale for territory translation
  513. * @return array
  514. */
  515. public static function getTerritoryTranslationList($locale = null)
  516. {
  517. return self::getTranslationList('territory', $locale, 1);
  518. }
  519. /**
  520. * Returns a localized information string, supported are several types of informations.
  521. * For detailed information about the types look into the documentation
  522. *
  523. * @param string $value Name to get detailed information about
  524. * @param string $path (Optional) Type of information to return
  525. * @param string|Zend_Locale $locale (Optional) Locale|Language for which this informations should be returned
  526. * @return string|false The wished information in the given language
  527. */
  528. public static function getTranslation($value = null, $path = null, $locale = null)
  529. {
  530. require_once 'Zend/Locale/Data.php';
  531. $locale = self::_prepareLocale($locale);
  532. $result = Zend_Locale_Data::getContent($locale, $path, $value);
  533. if (empty($result) === true) {
  534. return false;
  535. }
  536. return $result;
  537. }
  538. /**
  539. * Returns the localized language name
  540. *
  541. * @param string $value Name to get detailed information about
  542. * @param string $locale (Optional) Locale for language translation
  543. * @return array
  544. */
  545. public static function getLanguageTranslation($value, $locale = null)
  546. {
  547. return self::getTranslation($value, 'language', $locale);
  548. }
  549. /**
  550. * Returns the localized script name
  551. *
  552. * @param string $value Name to get detailed information about
  553. * @param string $locale (Optional) locale for script translation
  554. * @return array
  555. */
  556. public static function getScriptTranslation($value, $locale = null)
  557. {
  558. return self::getTranslation($value, 'script', $locale);
  559. }
  560. /**
  561. * Returns the localized country name
  562. *
  563. * @param string $value Name to get detailed information about
  564. * @param string|Zend_Locale $locale (Optional) Locale for country translation
  565. * @return array
  566. */
  567. public static function getCountryTranslation($value, $locale = null)
  568. {
  569. return self::getTranslation($value, 'country', $locale);
  570. }
  571. /**
  572. * Returns the localized territory name
  573. * All territories contains other countries.
  574. *
  575. * @param string $value Name to get detailed information about
  576. * @param string|Zend_Locale $locale (Optional) Locale for territory translation
  577. * @return array
  578. */
  579. public static function getTerritoryTranslation($value, $locale = null)
  580. {
  581. return self::getTranslation($value, 'territory', $locale);
  582. }
  583. /**
  584. * Returns an array with translated yes strings
  585. *
  586. * @param string|Zend_Locale $locale (Optional) Locale for language translation (defaults to $this locale)
  587. * @return array
  588. */
  589. public static function getQuestion($locale = null)
  590. {
  591. require_once 'Zend/Locale/Data.php';
  592. $locale = self::_prepareLocale($locale);
  593. $quest = Zend_Locale_Data::getList($locale, 'question');
  594. $yes = explode(':', $quest['yes']);
  595. $no = explode(':', $quest['no']);
  596. $quest['yes'] = $yes[0];
  597. $quest['yesarray'] = $yes;
  598. $quest['no'] = $no[0];
  599. $quest['noarray'] = $no;
  600. $quest['yesexpr'] = self::_prepareQuestionString($yes);
  601. $quest['noexpr'] = self::_prepareQuestionString($no);
  602. return $quest;
  603. }
  604. /**
  605. * Internal function for preparing the returned question regex string
  606. *
  607. * @param string $input Regex to parse
  608. * @return string
  609. */
  610. private static function _prepareQuestionString($input)
  611. {
  612. $regex = '';
  613. if (is_array($input) === true) {
  614. $regex = '^';
  615. $start = true;
  616. foreach ($input as $row) {
  617. if ($start === false) {
  618. $regex .= '|';
  619. }
  620. $start = false;
  621. $regex .= '(';
  622. $one = null;
  623. if (strlen($row) > 2) {
  624. $one = true;
  625. }
  626. foreach (str_split($row, 1) as $char) {
  627. $regex .= '[' . $char;
  628. $regex .= strtoupper($char) . ']';
  629. if ($one === true) {
  630. $one = false;
  631. $regex .= '(';
  632. }
  633. }
  634. if ($one === false) {
  635. $regex .= ')';
  636. }
  637. $regex .= '?)';
  638. }
  639. }
  640. return $regex;
  641. }
  642. /**
  643. * Checks if a locale identifier is a real locale or not
  644. * Examples:
  645. * "en_XX" refers to "en", which returns true
  646. * "XX_yy" refers to "root", which returns false
  647. *
  648. * @param string|Zend_Locale $locale Locale to check for
  649. * @param boolean $strict (Optional) If true, no rerouting will be done when checking
  650. * @param boolean $compatible (DEPRECIATED) Only for internal usage, brakes compatibility mode
  651. * @return boolean If the locale is known dependend on the settings
  652. */
  653. public static function isLocale($locale, $strict = false, $compatible = true)
  654. {
  655. try {
  656. $locale = self::_prepareLocale($locale, $strict);
  657. } catch (Zend_Locale_Exception $e) {
  658. return false;
  659. }
  660. if (($compatible === true) and (self::$compatibilityMode === true)) {
  661. trigger_error('You are running Zend_Locale in compatibility mode... please migrate your scripts', E_USER_NOTICE);
  662. if (isset(self::$_localeData[$locale]) === true) {
  663. return $locale;
  664. } else if (!$strict) {
  665. $locale = explode('_', $locale);
  666. if (isset(self::$_localeData[$locale[0]]) === true) {
  667. return $locale[0];
  668. }
  669. }
  670. } else {
  671. if (isset(self::$_localeData[$locale]) === true) {
  672. return true;
  673. } else if (!$strict) {
  674. $locale = explode('_', $locale);
  675. if (isset(self::$_localeData[$locale[0]]) === true) {
  676. return true;
  677. }
  678. }
  679. }
  680. return false;
  681. }
  682. /**
  683. * Finds the proper locale based on the input
  684. * Checks if it exists, degrades it when necessary
  685. * Detects registry locale and when all fails tries to detect a automatic locale
  686. * Returns the found locale as string
  687. *
  688. * @param string $locale
  689. * @throws Zend_Locale_Exception When the given locale is no locale or the autodetection fails
  690. * @return string
  691. */
  692. public static function findLocale($locale = null)
  693. {
  694. if ($locale === null) {
  695. require_once 'Zend/Registry.php';
  696. if (Zend_Registry::isRegistered('Zend_Locale')) {
  697. $locale = Zend_Registry::get('Zend_Locale');
  698. }
  699. }
  700. require_once 'Zend/Locale.php';
  701. if ($locale === null) {
  702. $locale = new Zend_Locale();
  703. }
  704. if (!Zend_Locale::isLocale($locale, true, false)) {
  705. if (!Zend_Locale::isLocale($locale, false, false)) {
  706. require_once 'Zend/Locale/Exception.php';
  707. throw new Zend_Locale_Exception("The locale '$locale' is no known locale");
  708. }
  709. $locale = new Zend_Locale($locale);
  710. }
  711. if ($locale instanceof Zend_Locale) {
  712. $locale = $locale->toString();
  713. }
  714. return $locale;
  715. }
  716. /**
  717. * Returns a list of all known locales where the locale is the key
  718. * Only real locales are returned, the internal locales 'root', 'auto', 'browser'
  719. * and 'environment' are suppressed
  720. *
  721. * @return array List of all Locales
  722. */
  723. public static function getLocaleList()
  724. {
  725. $list = self::$_localeData;
  726. unset($list['root']);
  727. unset($list['auto']);
  728. unset($list['browser']);
  729. unset($list['environment']);
  730. return $list;
  731. }
  732. /**
  733. * Returns the set cache
  734. *
  735. * @return Zend_Cache_Core The set cache
  736. */
  737. public static function getCache()
  738. {
  739. require_once 'Zend/Locale/Data.php';
  740. $cache = Zend_Locale_Data::getCache();
  741. return $cache;
  742. }
  743. /**
  744. * Sets a cache
  745. *
  746. * @param Zend_Cache_Core $cache Cache to set
  747. * @return void
  748. */
  749. public static function setCache(Zend_Cache_Core $cache)
  750. {
  751. require_once 'Zend/Locale/Data.php';
  752. Zend_Locale_Data::setCache($cache);
  753. }
  754. /**
  755. * Returns true when a cache is set
  756. *
  757. * @return boolean
  758. */
  759. public static function hasCache()
  760. {
  761. require_once 'Zend/Locale/Data.php';
  762. return Zend_Locale_Data::hasCache();
  763. }
  764. /**
  765. * Removes any set cache
  766. *
  767. * @return void
  768. */
  769. public static function removeCache()
  770. {
  771. require_once 'Zend/Locale/Data.php';
  772. Zend_Locale_Data::removeCache();
  773. }
  774. /**
  775. * Clears all set cache data
  776. *
  777. * @return void
  778. */
  779. public static function clearCache()
  780. {
  781. require_once 'Zend/Locale/Data.php';
  782. Zend_Locale_Data::clearCache();
  783. }
  784. /**
  785. * Internal function, returns a single locale on detection
  786. *
  787. * @param string|Zend_Locale $locale (Optional) Locale to work on
  788. * @param boolean $strict (Optional) Strict preparation
  789. * @throws Zend_Locale_Exception When no locale is set which is only possible when the class was wrong extended
  790. * @return string
  791. */
  792. private static function _prepareLocale($locale, $strict = false)
  793. {
  794. if ($locale instanceof Zend_Locale) {
  795. $locale = $locale->toString();
  796. }
  797. if (is_array($locale)) {
  798. return '';
  799. }
  800. if (empty(self::$_auto) === true) {
  801. self::$_browser = self::getBrowser();
  802. self::$_environment = self::getEnvironment();
  803. self::$_breakChain = true;
  804. self::$_auto = self::getBrowser() + self::getEnvironment() + self::getDefault();
  805. }
  806. if (!$strict) {
  807. if ($locale === 'browser') {
  808. $locale = self::$_browser;
  809. }
  810. if ($locale === 'environment') {
  811. $locale = self::$_environment;
  812. }
  813. if ($locale === 'default') {
  814. $locale = self::$_default;
  815. }
  816. if (($locale === 'auto') or ($locale === null)) {
  817. $locale = self::$_auto;
  818. }
  819. if (is_array($locale) === true) {
  820. $locale = key($locale);
  821. }
  822. }
  823. // This can only happen when someone extends Zend_Locale and erases the default
  824. if ($locale === null) {
  825. require_once 'Zend/Locale/Exception.php';
  826. throw new Zend_Locale_Exception('Autodetection of Locale has been failed!');
  827. }
  828. if (strpos($locale, '-') !== false) {
  829. $locale = strtr($locale, '-', '_');
  830. }
  831. return (string) $locale;
  832. }
  833. /**
  834. * Search the locale automatically and return all used locales
  835. * ordered by quality
  836. *
  837. * Standard Searchorder is Browser, Environment, Default
  838. *
  839. * @param string $searchorder (Optional) Searchorder
  840. * @return array Returns an array of all detected locales
  841. */
  842. public static function getOrder($order = null)
  843. {
  844. switch ($order) {
  845. case self::ENVIRONMENT:
  846. self::$_breakChain = true;
  847. $languages = self::getEnvironment() + self::getBrowser() + self::getDefault();
  848. break;
  849. case self::ZFDEFAULT:
  850. self::$_breakChain = true;
  851. $languages = self::getDefault() + self::getEnvironment() + self::getBrowser();
  852. break;
  853. default:
  854. self::$_breakChain = true;
  855. $languages = self::getBrowser() + self::getEnvironment() + self::getDefault();
  856. break;
  857. }
  858. return $languages;
  859. }
  860. }