PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/i18n/src/locale/lmbLocale.class.php

https://github.com/syfisher/limb
PHP | 371 lines | 285 code | 57 blank | 29 comment | 28 complexity | cbe92d50decb265a0d72aaa220d6bf59 MD5 | raw file
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. lmb_require('limb/config/src/lmbIni.class.php');
  10. lmb_require('limb/i18n/src/locale/lmbLocaleSpec.class.php');
  11. /**
  12. * Handles locale information and can format time, date, numbers and currency
  13. * for correct display for a given locale. The locale conversion uses plain numerical values for
  14. * dates, times, numbers and currency, if you want more elaborate conversions consider using the
  15. * date, time, date_time and currency classes.
  16. *
  17. * Countries are specified by the ISO 3166 country Code
  18. * http://www.iso.ch/iso/en/prods-services/iso3166ma/index.html
  19. * User-assigned code elements
  20. * http://www.iso.ch/iso/en/prods-services/iso3166ma/04background-on-iso-3166/reserved-and-user-assigned-codes.html#userassigned
  21. *
  22. * language is specified by the ISO 639 language Code
  23. * http://www.w3.org/WAI/ER/IG/ert/iso639.htm
  24. *
  25. * currency/funds are specified by the ISO 4217
  26. * http://www.bsi-global.com/Technical+Information/Publications/_Publications/tig90.xalter
  27. * @package i18n
  28. * @version $Id: lmbLocale.class.php 7486 2009-01-26 19:13:20Z pachanga $
  29. */
  30. class lmbLocale
  31. {
  32. protected $is_valid = false;
  33. public $date_format = ''; // format of dates
  34. public $short_date_format = ''; // format of short dates
  35. public $time_format = ''; // format of times
  36. public $date_time_format = '';
  37. public $short_date_time_format = '';
  38. public $short_date_short_time_format = '';
  39. public $short_time_format = ''; // format of short times
  40. public $is_monday_first = false; // true if monday is the first day of the week
  41. public $am_name = 'am';
  42. public $pm_name = 'pm';
  43. public $charset = '';
  44. public $LC_ALL = '';
  45. // numbers
  46. public $decimal_symbol = '';
  47. public $thousand_separator = '';
  48. public $fract_digits = '';
  49. public $negative_symbol = '';
  50. public $positive_symbol = '';
  51. // currency
  52. public $currency_name = '';
  53. public $currency_short_name = '';
  54. public $currency_decimal_symbol = '';
  55. public $currency_thousand_separator = '';
  56. public $currency_fract_digits = '';
  57. public $currency_negative_symbol = '';
  58. public $currency_positive_symbol = '';
  59. public $currency_symbol = '';
  60. public $currency_positive_format = '';
  61. public $currency_negative_format = '';
  62. // help arrays
  63. public $short_month_names = array();
  64. public $long_month_names = array();
  65. public $short_day_names = array();
  66. public $long_day_names = array();
  67. public $week_days = array(0, 1, 2, 3, 4, 5, 6);
  68. public $months = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  69. public $country = '';
  70. public $country_comment = '';
  71. public $language_comment = '';
  72. public $language_name = ''; // name of the language
  73. public $intl_language_name = ''; // internationalized name of the language
  74. public $language_direction = 'ltr';
  75. protected $locale_spec;
  76. function __construct($name, $config = null)
  77. {
  78. $this->locale_spec = new lmbLocaleSpec($name);
  79. if($config)
  80. $this->initLocaleSettings($config);
  81. }
  82. function initLocaleSettings($config)
  83. {
  84. if(!$config instanceof lmbIni)
  85. throw new lmbException('Config object must be an lmbIni instance', array('config' => $config));
  86. $this->_initCountrySettings($config);
  87. $this->_initLanguageSettings($config);
  88. }
  89. function isValid()
  90. {
  91. return $this->is_valid;
  92. }
  93. protected function _initCountrySettings($config)
  94. {
  95. $config->assignOption($this->time_format, 'time_format', 'date_time');
  96. $config->assignOption($this->short_time_format, 'short_time_format', 'date_time');
  97. $config->assignOption($this->date_format, 'date_format', 'date_time');
  98. $config->assignOption($this->short_date_format, 'short_date_format', 'date_time');
  99. $config->assignOption($this->date_time_format, 'date_time_format', 'date_time');
  100. $config->assignOption($this->short_date_time_format, 'short_date_time_format', 'date_time');
  101. $config->assignOption($this->short_date_short_time_format, 'short_date_short_time_format', 'date_time');
  102. if($config->hasOption('is_monday_first', 'date_time'))
  103. $this->is_monday_first = strtolower($config->getOption('is_monday_first', 'date_time')) == 'yes';
  104. if($this->is_monday_first)
  105. $this->week_days = array(1, 2, 3, 4, 5, 6, 0);
  106. else
  107. $this->week_days = array(0, 1, 2, 3, 4, 5, 6);
  108. $config->assignOption($this->country, 'country', 'regional_settings');
  109. $config->assignOption($this->country_comment, 'country_comment', 'regional_settings');
  110. $config->assignOption($this->decimal_symbol, 'decimal_symbol', 'numbers');
  111. $config->assignOption($this->thousand_separator, 'thousands_separator', 'numbers');
  112. $config->assignOption($this->fract_digits, 'fract_digits', 'numbers');
  113. $config->assignOption($this->negative_symbol, 'negative_symbol', 'numbers');
  114. $config->assignOption($this->positive_symbol, 'positive_symbol', 'numbers');
  115. $config->assignOption($this->currency_decimal_symbol, 'decimal_symbol', 'currency');
  116. $config->assignOption($this->currency_name, 'name', 'currency');
  117. $config->assignOption($this->currency_short_name, 'short_name', 'currency');
  118. $config->assignOption($this->currency_thousand_separator, 'thousands_separator', 'currency');
  119. $config->assignOption($this->currency_fract_digits, 'fract_digits', 'currency');
  120. $config->assignOption($this->currency_negative_symbol, 'negative_symbol', 'currency');
  121. $config->assignOption($this->currency_positive_symbol, 'positive_symbol', 'currency');
  122. $config->assignOption($this->currency_symbol, 'symbol', 'currency');
  123. $config->assignOption($this->currency_positive_format, 'positive_format', 'currency');
  124. $config->assignOption($this->currency_negative_format, 'negative_format', 'currency');
  125. }
  126. protected function _initLanguageSettings($config)
  127. {
  128. $config->assignOption($this->language_name, 'language_name', 'regional_settings');
  129. $config->assignOption($this->intl_language_name, 'international_language_name', 'regional_settings');
  130. $config->assignOption($this->language_comment, 'language_comment', 'regional_settings');
  131. $config->assignOption($this->language_direction, 'language_direction', 'regional_settings');
  132. $config->assignOption($this->LC_ALL, 'LC_ALL', 'regional_settings');
  133. $charset = false;
  134. if($config->hasOption('preferred', 'charset'))
  135. {
  136. $charset = $config->getOption('preferred', 'charset');
  137. if($charset != '')
  138. $this->charset = $charset;
  139. }
  140. if(!is_array($this->short_day_names))
  141. $this->short_day_names = array();
  142. if(!is_array($this->long_day_names))
  143. $this->long_day_names = array();
  144. foreach ($this->week_days as $day)
  145. {
  146. if($config->hasOption($day, 'short_day_names'))
  147. $this->short_day_names[$day] = $config->getOption($day, 'short_day_names');
  148. if($config->hasOption($day, 'long_day_names'))
  149. $this->long_day_names[$day] = $config->getOption($day, 'long_day_names');
  150. }
  151. if(!is_array($this->short_month_names))
  152. $this->short_month_names = array();
  153. if(!is_array($this->long_month_names))
  154. $this->long_month_names = array();
  155. foreach ($this->months as $month)
  156. {
  157. if($config->hasOption($month, 'short_month_names'))
  158. $this->short_month_names[$month] = $config->getOption($month, 'short_month_names');
  159. if($config->hasOption($month, 'long_month_names'))
  160. $this->long_month_names[$month] = $config->getOption($month, 'long_month_names');
  161. }
  162. if(!is_array($this->short_day_names))
  163. $this->short_day_names = array();
  164. if(!is_array($this->long_day_names))
  165. $this->long_day_names = array();
  166. foreach($this->week_days as $wday)
  167. {
  168. if($config->hasOption($wday, 'short_day_names'))
  169. $this->short_day_names[$wday] = $config->getOption($wday, 'short_day_names');
  170. if($config->hasOption($wday, 'long_day_names'))
  171. $this->long_day_names[$wday] = $config->getOption($wday, 'long_day_names');
  172. }
  173. }
  174. function getLocaleSpec()
  175. {
  176. return $this->locale_spec;
  177. }
  178. function getLocaleString()
  179. {
  180. return $this->locale_spec->getLocaleString();
  181. }
  182. function getLanguage()
  183. {
  184. return $this->locale_spec->getLanguage();
  185. }
  186. function setPHPLocale()
  187. {
  188. setlocale(LC_ALL, $this->LC_ALL);
  189. }
  190. function getCharset()
  191. {
  192. return $this->charset;
  193. }
  194. function getLanguageDirection()
  195. {
  196. return $this->language_direction;
  197. }
  198. function getCountryName()
  199. {
  200. return $this->country;
  201. }
  202. function getCountryComment()
  203. {
  204. return $this->country_comment;
  205. }
  206. function getLanguageComment()
  207. {
  208. return $this->language_comment;
  209. }
  210. function getLanguageName()
  211. {
  212. return $this->language_name;
  213. }
  214. function getIntlLanguageName()
  215. {
  216. return $this->intl_language_name;
  217. }
  218. function getCurrencySymbol()
  219. {
  220. return $this->currency_symbol;
  221. }
  222. function getCurrencyName()
  223. {
  224. return $this->currency_name;
  225. }
  226. function getCurrencyShortName()
  227. {
  228. return $this->currency_short_name;
  229. }
  230. function getTimeFormat()
  231. {
  232. return $this->time_format;
  233. }
  234. function getShortTimeFormat()
  235. {
  236. return $this->short_time_format;
  237. }
  238. function getDateFormat()
  239. {
  240. return $this->date_format;
  241. }
  242. function getShortDateFormat()
  243. {
  244. return $this->short_date_format;
  245. }
  246. function getShortDateTimeFormat()
  247. {
  248. return $this->short_date_time_format;
  249. }
  250. function getShortDateShortTimeFormat()
  251. {
  252. return $this->short_date_short_time_format;
  253. }
  254. function getDateTimeFormat()
  255. {
  256. return $this->date_time_format;
  257. }
  258. function isMondayFirst()
  259. {
  260. return $this->is_monday_first;
  261. }
  262. function getWeekDays()
  263. {
  264. return $this->week_days;
  265. }
  266. function getMonths()
  267. {
  268. return $this->months;
  269. }
  270. function getWeekDayNames($short = false)
  271. {
  272. if($short)
  273. return $this->short_day_names;
  274. else
  275. return $this->long_day_names;
  276. }
  277. function getMonthNames($short = false)
  278. {
  279. if($short)
  280. return $this->short_month_names;
  281. else
  282. return $this->long_month_names;
  283. }
  284. function getMeridiemName($hour)
  285. {
  286. return ($hour < 12) ? $this->am_name : $this->pm_name;
  287. }
  288. function getPmName()
  289. {
  290. return $this->pm_name;
  291. }
  292. function getAmName()
  293. {
  294. return $this->am_name;
  295. }
  296. function getDayName($num, $short = false)
  297. {
  298. if($num < 0 || $num > 6)
  299. return null;
  300. if($short)
  301. return $this->short_day_names[$num];
  302. else
  303. return $this->long_day_names[$num];
  304. }
  305. function getMonthName($num, $short = false)
  306. {
  307. if($num < 0 || $num > 11)
  308. return null;
  309. if($short)
  310. return $this->short_month_names[$num];
  311. else
  312. return $this->long_month_names[$num];
  313. }
  314. }