Locale.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 8ovkBqaf1epmTeM7o56rGT9bUZA80PQ9hPf93TlApKA=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.6
  11. */
  12. namespace Cube;
  13. class Locale
  14. {
  15. const LANG_TOKEN = 'LangToken';
  16. const DEFAULT_LOCALE = 'en';
  17. private static $_data = array(
  18. 'Albanian (Albania)' => 'sq_AL',
  19. 'Albanian' => 'sq',
  20. 'Arabic (Algeria)' => 'ar_DZ',
  21. 'Arabic (Bahrain)' => 'ar_BH',
  22. 'Arabic (Egypt)' => 'ar_EG',
  23. 'Arabic (Iraq)' => 'ar_IQ',
  24. 'Arabic (Jordan)' => 'ar_JO',
  25. 'Arabic (Kuwait)' => 'ar_KW',
  26. 'Arabic (Lebanon)' => 'ar_LB',
  27. 'Arabic (Libya)' => 'ar_LY',
  28. 'Arabic (Morocco)' => 'ar_MA',
  29. 'Arabic (Oman)' => 'ar_OM',
  30. 'Arabic (Qatar)' => 'ar_QA',
  31. 'Arabic (Saudi Arabia)' => 'ar_SA',
  32. 'Arabic (Sudan)' => 'ar_SD',
  33. 'Arabic (Syria)' => 'ar_SY',
  34. 'Arabic (Tunisia)' => 'ar_TN',
  35. 'Arabic (United Arab Emirates)' => 'ar_AE',
  36. 'Arabic (Yemen)' => 'ar_YE',
  37. 'Arabic' => 'ar',
  38. 'Belarusian (Belarus)' => 'be_BY',
  39. 'Belarusian' => 'be',
  40. 'Bulgarian (Bulgaria)' => 'bg_BG',
  41. 'Bulgarian' => 'bg',
  42. 'Catalan (Spain)' => 'ca_ES',
  43. 'Catalan' => 'ca',
  44. 'Chinese (China)' => 'zh_CN',
  45. 'Chinese (Hong Kong)' => 'zh_HK',
  46. 'Chinese (Singapore)' => 'zh_SG',
  47. 'Chinese (Taiwan)' => 'zh_TW',
  48. 'Chinese' => 'zh',
  49. 'Croatian (Croatia)' => 'hr_HR',
  50. 'Croatian' => 'hr',
  51. 'Czech (Czech Republic)' => 'cs_CZ',
  52. 'Czech' => 'cs',
  53. 'Danish (Denmark)' => 'da_DK',
  54. 'Danish' => 'da',
  55. 'Dutch (Belgium)' => 'nl_BE',
  56. 'Dutch (Netherlands)' => 'nl_NL',
  57. 'Dutch' => 'nl',
  58. 'English (Australia)' => 'en_AU',
  59. 'English (Canada)' => 'en_CA',
  60. 'English (India)' => 'en_IN',
  61. 'English (Ireland)' => 'en_IE',
  62. 'English (Malta)' => 'en_MT',
  63. 'English (New Zealand)' => 'en_NZ',
  64. 'English (Philippines)' => 'en_PH',
  65. 'English (Singapore)' => 'en_SG',
  66. 'English (South Africa)' => 'en_ZA',
  67. 'English (United Kingdom)' => 'en_GB',
  68. 'English (United States)' => 'en_US',
  69. 'English' => 'en',
  70. 'Estonian (Estonia)' => 'et_EE',
  71. 'Estonian' => 'et',
  72. 'Farsi (Iran)' => 'fa_IR',
  73. 'Finnish (Finland)' => 'fi_FI',
  74. 'Finnish' => 'fi',
  75. 'French (Belgium)' => 'fr_BE',
  76. 'French (Canada)' => 'fr_CA',
  77. 'French (France)' => 'fr_FR',
  78. 'French (Luxembourg)' => 'fr_LU',
  79. 'French (Switzerland)' => 'fr_CH',
  80. 'French' => 'fr',
  81. 'German (Austria)' => 'de_AT',
  82. 'German (Germany)' => 'de_DE',
  83. 'German (Luxembourg)' => 'de_LU',
  84. 'German (Switzerland)' => 'de_CH',
  85. 'German' => 'de',
  86. 'Greek (Cyprus)' => 'el_CY',
  87. 'Greek (Greece)' => 'el_GR',
  88. 'Greek' => 'el',
  89. 'Hebrew (Israel)' => 'iw_IL',
  90. 'Hebrew' => 'iw',
  91. 'Hindi (India)' => 'hi_IN',
  92. 'Hungarian (Hungary)' => 'hu_HU',
  93. 'Hungarian' => 'hu',
  94. 'Icelandic (Iceland)' => 'is_IS',
  95. 'Icelandic' => 'is',
  96. 'Indonesian (Indonesia)' => 'id_ID',
  97. 'Indonesian' => 'id',
  98. 'Irish (Ireland)' => 'ga_IE',
  99. 'Irish' => 'ga',
  100. 'Italian (Italy)' => 'it_IT',
  101. 'Italian (Switzerland)' => 'it_CH',
  102. 'Italian' => 'it',
  103. 'Japanese (Japan)' => 'ja_JP',
  104. 'Japanese (Japan, JP)' => 'ja_JP_JP',
  105. 'Japanese' => 'ja',
  106. 'Korean (South Korea)' => 'ko_KR',
  107. 'Korean' => 'ko',
  108. 'Latvian (Latvia)' => 'lv_LV',
  109. 'Latvian' => 'lv',
  110. 'Lithuanian (Lithuania)' => 'lt_LT',
  111. 'Lithuanian' => 'lt',
  112. 'Macedonian (Macedonia)' => 'mk_MK',
  113. 'Macedonian' => 'mk',
  114. 'Malay (Malaysia)' => 'ms_MY',
  115. 'Malay' => 'ms',
  116. 'Maltese (Malta)' => 'mt_MT',
  117. 'Maltese' => 'mt',
  118. 'Norwegian (Norway)' => 'no_NO',
  119. 'Norwegian (Norway, Nynorsk)' => 'no_NO_NY',
  120. 'Norwegian' => 'no',
  121. 'Polish (Poland)' => 'pl_PL',
  122. 'Polish' => 'pl',
  123. 'Portuguese (Brazil)' => 'pt_BR',
  124. 'Portuguese (Portugal)' => 'pt_PT',
  125. 'Portuguese' => 'pt',
  126. 'Romanian (Romania)' => 'ro_RO',
  127. 'Romanian' => 'ro',
  128. 'Russian (Russia)' => 'ru_RU',
  129. 'Russian' => 'ru',
  130. 'Kinyarwanda (Rwanda)' => 'rw_RW',
  131. 'Serbian (Bosnia and Herzegovina)' => 'sr_BA',
  132. 'Serbian (Montenegro)' => 'sr_ME',
  133. 'Serbian (Serbia and Montenegro)' => 'sr_CS',
  134. 'Serbian (Serbia)' => 'sr_RS',
  135. 'Serbian' => 'sr',
  136. 'Slovak (Slovakia)' => 'sk_SK',
  137. 'Slovak' => 'sk',
  138. 'Slovenian (Slovenia)' => 'sl_SI',
  139. 'Slovenian' => 'sl',
  140. 'Spanish (Argentina)' => 'es_AR',
  141. 'Spanish (Bolivia)' => 'es_BO',
  142. 'Spanish (Chile)' => 'es_CL',
  143. 'Spanish (Colombia)' => 'es_CO',
  144. 'Spanish (Costa Rica)' => 'es_CR',
  145. 'Spanish (Dominican Republic)' => 'es_DO',
  146. 'Spanish (Ecuador)' => 'es_EC',
  147. 'Spanish (El Salvador)' => 'es_SV',
  148. 'Spanish (Guatemala)' => 'es_GT',
  149. 'Spanish (Honduras)' => 'es_HN',
  150. 'Spanish (Mexico)' => 'es_MX',
  151. 'Spanish (Nicaragua)' => 'es_NI',
  152. 'Spanish (Panama)' => 'es_PA',
  153. 'Spanish (Paraguay)' => 'es_PY',
  154. 'Spanish (Peru)' => 'es_PE',
  155. 'Spanish (Puerto Rico)' => 'es_PR',
  156. 'Spanish (Spain)' => 'es_ES',
  157. 'Spanish (United States)' => 'es_US',
  158. 'Spanish (Uruguay)' => 'es_UY',
  159. 'Spanish (Venezuela)' => 'es_VE',
  160. 'Spanish' => 'es',
  161. 'Swedish (Sweden)' => 'sv_SE',
  162. 'Swedish' => 'sv',
  163. 'Thai (Thailand)' => 'th_TH',
  164. 'Thai (Thailand, TH)' => 'th_TH_TH',
  165. 'Thai' => 'th',
  166. 'Turkish (Turkey)' => 'tr_TR',
  167. 'Turkish' => 'tr',
  168. 'Ukrainian (Ukraine)' => 'uk_UA',
  169. 'Ukrainian' => 'uk',
  170. 'Vietnamese (Vietnam)' => 'vi_VN',
  171. 'Vietnamese' => 'vi',
  172. );
  173. /**
  174. *
  175. * locale value
  176. *
  177. * @var string
  178. */
  179. protected $_locale;
  180. /**
  181. *
  182. * class constructor
  183. *
  184. * @param string $locale
  185. */
  186. public function __construct($locale = null)
  187. {
  188. $this->setLocale($locale);
  189. }
  190. /**
  191. *
  192. * get active locale
  193. *
  194. * @return string
  195. */
  196. public function getLocale()
  197. {
  198. return $this->_locale;
  199. }
  200. /**
  201. *
  202. * set active locale
  203. *
  204. * @param string $locale
  205. *
  206. * @return \Cube\Locale
  207. */
  208. public function setLocale($locale)
  209. {
  210. if (array_search($locale, self::$_data) !== false) {
  211. $this->_locale = $locale;
  212. }
  213. else {
  214. $this->_locale = self::DEFAULT_LOCALE;
  215. }
  216. setlocale(LC_COLLATE, $this->_locale);
  217. if (!in_array($this->_locale, array('tr', 'tr_TR'))) {
  218. setlocale(LC_CTYPE, $this->_locale); // for some reason this gives error for the turkish locales
  219. }
  220. setlocale(LC_MONETARY, $this->_locale);
  221. setlocale(LC_TIME, $this->_locale);
  222. setlocale(LC_NUMERIC, self::DEFAULT_LOCALE); // numeric values are always displayed in en locale
  223. return $this;
  224. }
  225. /**
  226. *
  227. * get locale array
  228. *
  229. * @return array
  230. */
  231. public static function getData()
  232. {
  233. return self::$_data;
  234. }
  235. /**
  236. *
  237. * get the (name) key of a locale or false if not found
  238. *
  239. * @param string $locale
  240. *
  241. * @return string|false
  242. */
  243. public static function getLocaleKey($locale)
  244. {
  245. return array_search($locale, self::$_data);
  246. }
  247. /**
  248. *
  249. * get the name (key) of a locale, or return the initial value if not found
  250. *
  251. * @param string $locale
  252. *
  253. * @return string
  254. */
  255. public static function getLocaleName($locale)
  256. {
  257. return (($output = self::getLocaleKey($locale)) === false) ? $locale : $output;
  258. }
  259. /**
  260. *
  261. * check if a locale variable exists in the locales array and return true if it is, false otherwise
  262. *
  263. * @param string $locale
  264. *
  265. * @return bool
  266. */
  267. public static function isLocale($locale)
  268. {
  269. if (array_search($locale, self::$_data) !== false) {
  270. return true;
  271. }
  272. return false;
  273. }
  274. }