PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mysites/framework/i18n/CLocale.php

https://gitlab.com/muthuvel.ns/imp-file
PHP | 470 lines | 241 code | 34 blank | 195 comment | 28 complexity | 519713c94e076bc54bdb1ab13ff5b1ce MD5 | raw file
  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. * @package system.i18n
  34. * @since 1.0
  35. */
  36. class CLocale extends CComponent
  37. {
  38. /**
  39. * @var string the directory that contains the locale data. If this property is not set,
  40. * the locale data will be loaded from 'framework/i18n/data'.
  41. * @since 1.1.0
  42. */
  43. public static $dataPath;
  44. private $_id;
  45. private $_data;
  46. private $_dateFormatter;
  47. private $_numberFormatter;
  48. /**
  49. * Returns the instance of the specified locale.
  50. * Since the constructor of CLocale is protected, you can only use
  51. * this method to obtain an instance of the specified locale.
  52. * @param string $id the locale ID (e.g. en_US)
  53. * @return CLocale the locale instance
  54. */
  55. public static function getInstance($id)
  56. {
  57. static $locales=array();
  58. if(isset($locales[$id]))
  59. return $locales[$id];
  60. else
  61. return $locales[$id]=new CLocale($id);
  62. }
  63. /**
  64. * @return array IDs of the locales which the framework can recognize
  65. */
  66. public static function getLocaleIDs()
  67. {
  68. static $locales;
  69. if($locales===null)
  70. {
  71. $locales=array();
  72. $dataPath=self::$dataPath===null ? dirname(__FILE__).DIRECTORY_SEPARATOR.'data' : self::$dataPath;
  73. $folder=@opendir($dataPath);
  74. while(($file=@readdir($folder))!==false)
  75. {
  76. $fullPath=$dataPath.DIRECTORY_SEPARATOR.$file;
  77. if(substr($file,-4)==='.php' && is_file($fullPath))
  78. $locales[]=substr($file,0,-4);
  79. }
  80. closedir($folder);
  81. sort($locales);
  82. }
  83. return $locales;
  84. }
  85. /**
  86. * Constructor.
  87. * Since the constructor is protected, please use {@link getInstance}
  88. * to obtain an instance of the specified locale.
  89. * @param string $id the locale ID (e.g. en_US)
  90. */
  91. protected function __construct($id)
  92. {
  93. $this->_id=self::getCanonicalID($id);
  94. $dataPath=self::$dataPath===null ? dirname(__FILE__).DIRECTORY_SEPARATOR.'data' : self::$dataPath;
  95. $dataFile=$dataPath.DIRECTORY_SEPARATOR.$this->_id.'.php';
  96. if(is_file($dataFile))
  97. $this->_data=require($dataFile);
  98. else
  99. throw new CException(Yii::t('yii','Unrecognized locale "{locale}".',array('{locale}'=>$id)));
  100. }
  101. /**
  102. * Converts a locale ID to its canonical form.
  103. * In canonical form, a locale ID consists of only underscores and lower-case letters.
  104. * @param string $id the locale ID to be converted
  105. * @return string the locale ID in canonical form
  106. */
  107. public static function getCanonicalID($id)
  108. {
  109. return strtolower(str_replace('-','_',$id));
  110. }
  111. /**
  112. * @return string the locale ID (in canonical form)
  113. */
  114. public function getId()
  115. {
  116. return $this->_id;
  117. }
  118. /**
  119. * @return CNumberFormatter the number formatter for this locale
  120. */
  121. public function getNumberFormatter()
  122. {
  123. if($this->_numberFormatter===null)
  124. $this->_numberFormatter=new CNumberFormatter($this);
  125. return $this->_numberFormatter;
  126. }
  127. /**
  128. * @return CDateFormatter the date formatter for this locale
  129. */
  130. public function getDateFormatter()
  131. {
  132. if($this->_dateFormatter===null)
  133. $this->_dateFormatter=new CDateFormatter($this);
  134. return $this->_dateFormatter;
  135. }
  136. /**
  137. * @param string $currency 3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency.
  138. * @return string the localized currency symbol. Null if the symbol does not exist.
  139. */
  140. public function getCurrencySymbol($currency)
  141. {
  142. return isset($this->_data['currencySymbols'][$currency]) ? $this->_data['currencySymbols'][$currency] : null;
  143. }
  144. /**
  145. * @param string $name symbol name
  146. * @return string symbol
  147. */
  148. public function getNumberSymbol($name)
  149. {
  150. return isset($this->_data['numberSymbols'][$name]) ? $this->_data['numberSymbols'][$name] : null;
  151. }
  152. /**
  153. * @return string the decimal format
  154. */
  155. public function getDecimalFormat()
  156. {
  157. return $this->_data['decimalFormat'];
  158. }
  159. /**
  160. * @return string the currency format
  161. */
  162. public function getCurrencyFormat()
  163. {
  164. return $this->_data['currencyFormat'];
  165. }
  166. /**
  167. * @return string the percent format
  168. */
  169. public function getPercentFormat()
  170. {
  171. return $this->_data['percentFormat'];
  172. }
  173. /**
  174. * @return string the scientific format
  175. */
  176. public function getScientificFormat()
  177. {
  178. return $this->_data['scientificFormat'];
  179. }
  180. /**
  181. * @param integer $month month (1-12)
  182. * @param string $width month name width. It can be 'wide', 'abbreviated' or 'narrow'.
  183. * @param boolean $standAlone whether the month name should be returned in stand-alone format
  184. * @return string the month name
  185. */
  186. public function getMonthName($month,$width='wide',$standAlone=false)
  187. {
  188. if($standAlone)
  189. return isset($this->_data['monthNamesSA'][$width][$month]) ? $this->_data['monthNamesSA'][$width][$month] : $this->_data['monthNames'][$width][$month];
  190. else
  191. return isset($this->_data['monthNames'][$width][$month]) ? $this->_data['monthNames'][$width][$month] : $this->_data['monthNamesSA'][$width][$month];
  192. }
  193. /**
  194. * Returns the month names in the specified width.
  195. * @param string $width month name width. It can be 'wide', 'abbreviated' or 'narrow'.
  196. * @param boolean $standAlone whether the month names should be returned in stand-alone format
  197. * @return array month names indexed by month values (1-12)
  198. */
  199. public function getMonthNames($width='wide',$standAlone=false)
  200. {
  201. if($standAlone)
  202. return isset($this->_data['monthNamesSA'][$width]) ? $this->_data['monthNamesSA'][$width] : $this->_data['monthNames'][$width];
  203. else
  204. return isset($this->_data['monthNames'][$width]) ? $this->_data['monthNames'][$width] : $this->_data['monthNamesSA'][$width];
  205. }
  206. /**
  207. * @param integer $day weekday (0-7, 0 and 7 means Sunday)
  208. * @param string $width weekday name width. It can be 'wide', 'abbreviated' or 'narrow'.
  209. * @param boolean $standAlone whether the week day name should be returned in stand-alone format
  210. * @return string the weekday name
  211. */
  212. public function getWeekDayName($day,$width='wide',$standAlone=false)
  213. {
  214. $day=$day%7;
  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 (isset($subTag[2]) && strlen($subTag[2])<4)
  360. {
  361. $id = $subTag[2];
  362. }
  363. elseif (strlen($subTag[1])<4)
  364. {
  365. $id = $subTag[1];
  366. }
  367. else
  368. {
  369. $id = null;
  370. }
  371. }
  372. else
  373. {
  374. $id = null;
  375. }
  376. return $id;
  377. }
  378. /**
  379. * Gets a localized name from i18n data file (one of framework/i18n/data/ files).
  380. *
  381. * @param string $id array key from an array named by $category.
  382. * @param string $category data category. One of 'languages', 'scripts' or 'territories'.
  383. * @return string the localized name for the id specified. Null if data does not exist.
  384. * @since 1.1.9
  385. */
  386. public function getLocaleDisplayName($id=null, $category='languages')
  387. {
  388. $id = $this->getCanonicalID($id);
  389. if (($category == 'languages') && ($id=$this->getLanguageID($id)) && (isset($this->_data[$category][$id])))
  390. {
  391. return $this->_data[$category][$id];
  392. }
  393. elseif (($category == 'scripts') && ($id=$this->getScriptID($id)) && (isset($this->_data[$category][$id])))
  394. {
  395. return $this->_data[$category][$id];
  396. }
  397. elseif (($category == 'territories') && ($id=$this->getTerritoryID($id)) && (isset($this->_data[$category][$id])))
  398. {
  399. return $this->_data[$category][$id];
  400. }
  401. elseif (isset($this->_data[$category][$id]))
  402. {
  403. return $this->_data[$category][$id];
  404. }
  405. else {
  406. return null;
  407. }
  408. }
  409. /**
  410. * @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.
  411. * @return string the local display name for the language. Null if the language code does not exist.
  412. * @since 1.1.9
  413. */
  414. public function getLanguage($id)
  415. {
  416. return $this->getLocaleDisplayName($id, 'languages');
  417. }
  418. /**
  419. * @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.
  420. * @return string the local display name for the script. Null if the script code does not exist.
  421. * @since 1.1.9
  422. */
  423. public function getScript($id)
  424. {
  425. return $this->getLocaleDisplayName($id, 'scripts');
  426. }
  427. /**
  428. * @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.
  429. * @return string the local display name for the territory. Null if the territory code does not exist.
  430. * @since 1.1.9
  431. */
  432. public function getTerritory($id)
  433. {
  434. return $this->getLocaleDisplayName($id, 'territories');
  435. }
  436. }