PageRenderTime 111ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/a10/lib/yii-1.1.10/i18n/CLocale.php

http://chenjin.googlecode.com/
PHP | 466 lines | 236 code | 34 blank | 196 comment | 33 complexity | b9bdebda3abc46ea6b8955f9c231b55d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * CLocale class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CLocale represents the data relevant to a locale.
  12. *
  13. * The data includes the number formatting information and date formatting information.
  14. *
  15. * @property string $id The locale ID (in canonical form).
  16. * @property CNumberFormatter $numberFormatter The number formatter for this locale.
  17. * @property CDateFormatter $dateFormatter The date formatter for this locale.
  18. * @property string $decimalFormat The decimal format.
  19. * @property string $currencyFormat The currency format.
  20. * @property string $percentFormat The percent format.
  21. * @property string $scientificFormat The scientific format.
  22. * @property array $monthNames Month names indexed by month values (1-12).
  23. * @property array $weekDayNames The weekday names indexed by weekday values (0-6, 0 means Sunday, 1 Monday, etc.).
  24. * @property string $aMName The AM name.
  25. * @property string $pMName The PM name.
  26. * @property string $dateFormat Date format.
  27. * @property string $timeFormat Date format.
  28. * @property string $dateTimeFormat Datetime format, i.e., the order of date and time.
  29. * @property string $orientation The character orientation, which is either 'ltr' (left-to-right) or 'rtl' (right-to-left).
  30. * @property array $pluralRules Plural forms expressions.
  31. *
  32. * @author Qiang Xue <qiang.xue@gmail.com>
  33. * @version $Id: CLocale.php 242 2012-03-29 15:18:01Z mole1230 $
  34. * @package system.i18n
  35. * @since 1.0
  36. */
  37. class CLocale extends CComponent
  38. {
  39. /**
  40. * @var string the directory that contains the locale data. If this property is not set,
  41. * the locale data will be loaded from 'framework/i18n/data'.
  42. * @since 1.1.0
  43. */
  44. public static $dataPath;
  45. private $_id;
  46. private $_data;
  47. private $_dateFormatter;
  48. private $_numberFormatter;
  49. /**
  50. * Returns the instance of the specified locale.
  51. * Since the constructor of CLocale is protected, you can only use
  52. * this method to obtain an instance of the specified locale.
  53. * @param string $id the locale ID (e.g. en_US)
  54. * @return CLocale the locale instance
  55. */
  56. public static function getInstance($id)
  57. {
  58. static $locales=array();
  59. if(isset($locales[$id]))
  60. return $locales[$id];
  61. else
  62. return $locales[$id]=new CLocale($id);
  63. }
  64. /**
  65. * @return array IDs of the locales which the framework can recognize
  66. */
  67. public static function getLocaleIDs()
  68. {
  69. static $locales;
  70. if($locales===null)
  71. {
  72. $locales=array();
  73. $dataPath=self::$dataPath===null ? dirname(__FILE__).DIRECTORY_SEPARATOR.'data' : self::$dataPath;
  74. $folder=@opendir($dataPath);
  75. while(($file=@readdir($folder))!==false)
  76. {
  77. $fullPath=$dataPath.DIRECTORY_SEPARATOR.$file;
  78. if(substr($file,-4)==='.php' && is_file($fullPath))
  79. $locales[]=substr($file,0,-4);
  80. }
  81. closedir($folder);
  82. sort($locales);
  83. }
  84. return $locales;
  85. }
  86. /**
  87. * Constructor.
  88. * Since the constructor is protected, please use {@link getInstance}
  89. * to obtain an instance of the specified locale.
  90. * @param string $id the locale ID (e.g. en_US)
  91. */
  92. protected function __construct($id)
  93. {
  94. $this->_id=self::getCanonicalID($id);
  95. $dataPath=self::$dataPath===null ? dirname(__FILE__).DIRECTORY_SEPARATOR.'data' : self::$dataPath;
  96. $dataFile=$dataPath.DIRECTORY_SEPARATOR.$this->_id.'.php';
  97. if(is_file($dataFile))
  98. $this->_data=require($dataFile);
  99. else
  100. throw new CException(Yii::t('yii','Unrecognized locale "{locale}".',array('{locale}'=>$id)));
  101. }
  102. /**
  103. * Converts a locale ID to its canonical form.
  104. * In canonical form, a locale ID consists of only underscores and lower-case letters.
  105. * @param string $id the locale ID to be converted
  106. * @return string the locale ID in canonical form
  107. */
  108. public static function getCanonicalID($id)
  109. {
  110. return strtolower(str_replace('-','_',$id));
  111. }
  112. /**
  113. * @return string the locale ID (in canonical form)
  114. */
  115. public function getId()
  116. {
  117. return $this->_id;
  118. }
  119. /**
  120. * @return CNumberFormatter the number formatter for this locale
  121. */
  122. public function getNumberFormatter()
  123. {
  124. if($this->_numberFormatter===null)
  125. $this->_numberFormatter=new CNumberFormatter($this);
  126. return $this->_numberFormatter;
  127. }
  128. /**
  129. * @return CDateFormatter the date formatter for this locale
  130. */
  131. public function getDateFormatter()
  132. {
  133. if($this->_dateFormatter===null)
  134. $this->_dateFormatter=new CDateFormatter($this);
  135. return $this->_dateFormatter;
  136. }
  137. /**
  138. * @param string $currency 3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency.
  139. * @return string the localized currency symbol. Null if the symbol does not exist.
  140. */
  141. public function getCurrencySymbol($currency)
  142. {
  143. return isset($this->_data['currencySymbols'][$currency]) ? $this->_data['currencySymbols'][$currency] : null;
  144. }
  145. /**
  146. * @param string $name symbol name
  147. * @return string symbol
  148. */
  149. public function getNumberSymbol($name)
  150. {
  151. return isset($this->_data['numberSymbols'][$name]) ? $this->_data['numberSymbols'][$name] : null;
  152. }
  153. /**
  154. * @return string the decimal format
  155. */
  156. public function getDecimalFormat()
  157. {
  158. return $this->_data['decimalFormat'];
  159. }
  160. /**
  161. * @return string the currency format
  162. */
  163. public function getCurrencyFormat()
  164. {
  165. return $this->_data['currencyFormat'];
  166. }
  167. /**
  168. * @return string the percent format
  169. */
  170. public function getPercentFormat()
  171. {
  172. return $this->_data['percentFormat'];
  173. }
  174. /**
  175. * @return string the scientific format
  176. */
  177. public function getScientificFormat()
  178. {
  179. return $this->_data['scientificFormat'];
  180. }
  181. /**
  182. * @param integer $month month (1-12)
  183. * @param string $width month name width. It can be 'wide', 'abbreviated' or 'narrow'.
  184. * @param boolean $standAlone whether the month name should be returned in stand-alone format
  185. * @return string the month name
  186. */
  187. public function getMonthName($month,$width='wide',$standAlone=false)
  188. {
  189. if($standAlone)
  190. return isset($this->_data['monthNamesSA'][$width][$month]) ? $this->_data['monthNamesSA'][$width][$month] : $this->_data['monthNames'][$width][$month];
  191. else
  192. return isset($this->_data['monthNames'][$width][$month]) ? $this->_data['monthNames'][$width][$month] : $this->_data['monthNamesSA'][$width][$month];
  193. }
  194. /**
  195. * Returns the month names in the specified width.
  196. * @param string $width month name width. It can be 'wide', 'abbreviated' or 'narrow'.
  197. * @param boolean $standAlone whether the month names should be returned in stand-alone format
  198. * @return array month names indexed by month values (1-12)
  199. */
  200. public function getMonthNames($width='wide',$standAlone=false)
  201. {
  202. if($standAlone)
  203. return isset($this->_data['monthNamesSA'][$width]) ? $this->_data['monthNamesSA'][$width] : $this->_data['monthNames'][$width];
  204. else
  205. return isset($this->_data['monthNames'][$width]) ? $this->_data['monthNames'][$width] : $this->_data['monthNamesSA'][$width];
  206. }
  207. /**
  208. * @param integer $day weekday (0-6, 0 means Sunday)
  209. * @param string $width weekday name width. It can be 'wide', 'abbreviated' or 'narrow'.
  210. * @param boolean $standAlone whether the week day name should be returned in stand-alone format
  211. * @return string the weekday name
  212. */
  213. public function getWeekDayName($day,$width='wide',$standAlone=false)
  214. {
  215. if($standAlone)
  216. return isset($this->_data['weekDayNamesSA'][$width][$day]) ? $this->_data['weekDayNamesSA'][$width][$day] : $this->_data['weekDayNames'][$width][$day];
  217. else
  218. return isset($this->_data['weekDayNames'][$width][$day]) ? $this->_data['weekDayNames'][$width][$day] : $this->_data['weekDayNamesSA'][$width][$day];
  219. }
  220. /**
  221. * Returns the week day names in the specified width.
  222. * @param string $width weekday name width. It can be 'wide', 'abbreviated' or 'narrow'.
  223. * @param boolean $standAlone whether the week day name should be returned in stand-alone format
  224. * @return array the weekday names indexed by weekday values (0-6, 0 means Sunday, 1 Monday, etc.)
  225. */
  226. public function getWeekDayNames($width='wide',$standAlone=false)
  227. {
  228. if($standAlone)
  229. return isset($this->_data['weekDayNamesSA'][$width]) ? $this->_data['weekDayNamesSA'][$width] : $this->_data['weekDayNames'][$width];
  230. else
  231. return isset($this->_data['weekDayNames'][$width]) ? $this->_data['weekDayNames'][$width] : $this->_data['weekDayNamesSA'][$width];
  232. }
  233. /**
  234. * @param integer $era era (0,1)
  235. * @param string $width era name width. It can be 'wide', 'abbreviated' or 'narrow'.
  236. * @return string the era name
  237. */
  238. public function getEraName($era,$width='wide')
  239. {
  240. return $this->_data['eraNames'][$width][$era];
  241. }
  242. /**
  243. * @return string the AM name
  244. */
  245. public function getAMName()
  246. {
  247. return $this->_data['amName'];
  248. }
  249. /**
  250. * @return string the PM name
  251. */
  252. public function getPMName()
  253. {
  254. return $this->_data['pmName'];
  255. }
  256. /**
  257. * @param string $width date format width. It can be 'full', 'long', 'medium' or 'short'.
  258. * @return string date format
  259. */
  260. public function getDateFormat($width='medium')
  261. {
  262. return $this->_data['dateFormats'][$width];
  263. }
  264. /**
  265. * @param string $width time format width. It can be 'full', 'long', 'medium' or 'short'.
  266. * @return string date format
  267. */
  268. public function getTimeFormat($width='medium')
  269. {
  270. return $this->_data['timeFormats'][$width];
  271. }
  272. /**
  273. * @return string datetime format, i.e., the order of date and time.
  274. */
  275. public function getDateTimeFormat()
  276. {
  277. return $this->_data['dateTimeFormat'];
  278. }
  279. /**
  280. * @return string the character orientation, which is either 'ltr' (left-to-right) or 'rtl' (right-to-left)
  281. * @since 1.1.2
  282. */
  283. public function getOrientation()
  284. {
  285. return isset($this->_data['orientation']) ? $this->_data['orientation'] : 'ltr';
  286. }
  287. /**
  288. * @return array plural forms expressions
  289. */
  290. public function getPluralRules()
  291. {
  292. return isset($this->_data['pluralRules']) ? $this->_data['pluralRules'] : array();
  293. }
  294. /**
  295. * Converts a locale ID to a language ID.
  296. * A language ID consists of only the first group of letters before an underscore or dash.
  297. * @param string $id the locale ID to be converted
  298. * @return string the language ID
  299. * @since 1.1.9
  300. */
  301. public function getLanguageID($id)
  302. {
  303. // normalize id
  304. $id = $this->getCanonicalID($id);
  305. // remove sub tags
  306. if(($underscorePosition=strpos($id, '_'))!== false)
  307. {
  308. $id = substr($id, 0, $underscorePosition);
  309. }
  310. return $id;
  311. }
  312. /**
  313. * Converts a locale ID to a script ID.
  314. * A script ID consists of only the last four characters after an underscore or dash.
  315. * @param string $id the locale ID to be converted
  316. * @return string the script ID
  317. * @since 1.1.9
  318. */
  319. public function getScriptID($id)
  320. {
  321. // normalize id
  322. $id = $this->getCanonicalID($id);
  323. // find sub tags
  324. if(($underscorePosition=strpos($id, '_'))!==false)
  325. {
  326. $subTag = explode('_', $id);
  327. // script sub tags can be distigused from territory sub tags by length
  328. if (strlen($subTag[1])===4)
  329. {
  330. $id = $subTag[1];
  331. }
  332. else
  333. {
  334. $id = null;
  335. }
  336. }
  337. else
  338. {
  339. $id = null;
  340. }
  341. return $id;
  342. }
  343. /**
  344. * Converts a locale ID to a territory ID.
  345. * A territory ID consists of only the last two to three letter or digits after an underscore or dash.
  346. * @param string $id the locale ID to be converted
  347. * @return string the territory ID
  348. * @since 1.1.9
  349. */
  350. public function getTerritoryID($id)
  351. {
  352. // normalize id
  353. $id = $this->getCanonicalID($id);
  354. // find sub tags
  355. if (($underscorePosition=strpos($id, '_'))!== false)
  356. {
  357. $subTag = explode('_', $id);
  358. // territory sub tags can be distigused from script sub tags by length
  359. if (strlen($subTag[1])<4)
  360. {
  361. $id = $subTag[1];
  362. }
  363. else
  364. {
  365. $id = null;
  366. }
  367. }
  368. else
  369. {
  370. $id = null;
  371. }
  372. return $id;
  373. }
  374. /**
  375. * Gets a localized name from i18n data file (one of framework/i18n/data/ files).
  376. *
  377. * @param string $id array key from an array named by $category.
  378. * @param string $category data category. One of 'languages', 'scripts' or 'territories'.
  379. * @return string the localized name for the id specified. Null if data does not exist.
  380. * @since 1.1.9
  381. */
  382. public function getLocaleDisplayName($id=null, $category='languages')
  383. {
  384. $id = $this->getCanonicalID($id);
  385. if (isset($this->_data[$category][$id]))
  386. {
  387. return $this->_data[$category][$id];
  388. }
  389. else if (($category == 'languages') && ($id=$this->getLanguageID($id)) && (isset($this->_data[$category][$id])))
  390. {
  391. return $this->_data[$category][$id];
  392. }
  393. else if (($category == 'scripts') && ($id=$this->getScriptID($id)) && (isset($this->_data[$category][$id])))
  394. {
  395. return $this->_data[$category][$id];
  396. }
  397. else if (($category == 'territories') && ($id=$this->getTerritoryID($id)) && (isset($this->_data[$category][$id])))
  398. {
  399. return $this->_data[$category][$id];
  400. }
  401. else {
  402. return null;
  403. }
  404. }
  405. /**
  406. * @param string $id Unicode language identifier from IETF BCP 47. For example, the code "en_US" represents U.S. English and "en_GB" represents British English.
  407. * @return string the local display name for the language. Null if the language code does not exist.
  408. * @since 1.1.9
  409. */
  410. public function getLanguage($id)
  411. {
  412. return $this->getLocaleDisplayName($id, 'languages');
  413. }
  414. /**
  415. * @param string $id Unicode script identifier from IETF BCP 47. For example, the code "en_US" represents U.S. English and "en_GB" represents British English.
  416. * @return string the local display name for the script. Null if the script code does not exist.
  417. * @since 1.1.9
  418. */
  419. public function getScript($id)
  420. {
  421. return $this->getLocaleDisplayName($id, 'scripts');
  422. }
  423. /**
  424. * @param string $id Unicode territory identifier from IETF BCP 47. For example, the code "en_US" represents U.S. English and "en_GB" represents British English.
  425. * @return string the local display name for the territory. Null if the territory code does not exist.
  426. * @since 1.1.9
  427. */
  428. public function getTerritory($id)
  429. {
  430. return $this->getLocaleDisplayName($id, 'territories');
  431. }
  432. }