/bp-forums/bbpress/bb-includes/class.bb-locale.php

  1. <?php 
  2. // Last sync [WP11537] 
  3.  
  4. /** 
  5. * Date and Time Locale object 
  6. * 
  7. * @package bbPress 
  8. * @subpackage i18n 
  9. */ 
  10.  
  11. /** 
  12. * Class that loads the calendar locale. 
  13. */ 
  14. class BB_Locale { 
  15. /** 
  16. * Stores the translated strings for the full weekday names. 
  17. * 
  18. * @var array 
  19. * @access private 
  20. */ 
  21. var $weekday; 
  22.  
  23. /** 
  24. * Stores the translated strings for the one character weekday names. 
  25. * 
  26. * There is a hack to make sure that Tuesday and Thursday, as well 
  27. * as Sunday and Saturday don't conflict. See init() method for more. 
  28. * 
  29. * @see BB_Locale::init() for how to handle the hack. 
  30. * 
  31. * @var array 
  32. * @access private 
  33. */ 
  34. var $weekday_initial; 
  35.  
  36. /** 
  37. * Stores the translated strings for the abbreviated weekday names. 
  38. * 
  39. * @var array 
  40. * @access private 
  41. */ 
  42. var $weekday_abbrev; 
  43.  
  44. /** 
  45. * Stores the translated strings for the full month names. 
  46. * 
  47. * @var array 
  48. * @access private 
  49. */ 
  50. var $month; 
  51.  
  52. /** 
  53. * Stores the translated strings for the abbreviated month names. 
  54. * 
  55. * @var array 
  56. * @access private 
  57. */ 
  58. var $month_abbrev; 
  59.  
  60. /** 
  61. * Stores the translated strings for 'am' and 'pm'. 
  62. * 
  63. * Also the capitalized versions. 
  64. * 
  65. * @var array 
  66. * @access private 
  67. */ 
  68. var $meridiem; 
  69.  
  70. /** 
  71. * Stores number formatting rules. 
  72. * 
  73. * @var array 
  74. * @access public 
  75. */ 
  76. var $number_format; 
  77.  
  78. /** 
  79. * Stores date and time formatting strings. 
  80. * 
  81. * @var array 
  82. * @access public 
  83. */ 
  84. var $datetime_formatstring; 
  85.  
  86. /** 
  87. * The text direction of the locale language. 
  88. * 
  89. * Default is left to right 'ltr'. 
  90. * 
  91. * @var string 
  92. * @access private 
  93. */ 
  94. var $text_direction = ''; 
  95.  
  96. /** 
  97. * Imports the global version to the class property. 
  98. * 
  99. * @var array 
  100. * @access private 
  101. */ 
  102. var $locale_vars = array('text_direction'); 
  103.  
  104. /** 
  105. * Sets up the translated strings and object properties. 
  106. * 
  107. * The method creates the translatable strings for various 
  108. * calendar elements. Which allows for specifying locale 
  109. * specific calendar names and text direction. 
  110. * 
  111. * @access private 
  112. */ 
  113. function init() { 
  114. // The Weekdays 
  115. $this->weekday[0] = __('Sunday'); 
  116. $this->weekday[1] = __('Monday'); 
  117. $this->weekday[2] = __('Tuesday'); 
  118. $this->weekday[3] = __('Wednesday'); 
  119. $this->weekday[4] = __('Thursday'); 
  120. $this->weekday[5] = __('Friday'); 
  121. $this->weekday[6] = __('Saturday'); 
  122.  
  123. // The first letter of each day. The _%day%_initial suffix is a hack to make 
  124. // sure the day initials are unique. 
  125. $this->weekday_initial[__('Sunday')] = __('S_Sunday_initial'); 
  126. $this->weekday_initial[__('Monday')] = __('M_Monday_initial'); 
  127. $this->weekday_initial[__('Tuesday')] = __('T_Tuesday_initial'); 
  128. $this->weekday_initial[__('Wednesday')] = __('W_Wednesday_initial'); 
  129. $this->weekday_initial[__('Thursday')] = __('T_Thursday_initial'); 
  130. $this->weekday_initial[__('Friday')] = __('F_Friday_initial'); 
  131. $this->weekday_initial[__('Saturday')] = __('S_Saturday_initial'); 
  132.  
  133. foreach ($this->weekday_initial as $weekday_ => $weekday_initial_) { 
  134. $this->weekday_initial[$weekday_] = preg_replace('/_.+_initial$/', '', $weekday_initial_); 
  135.  
  136. // Abbreviations for each day. 
  137. $this->weekday_abbrev[__('Sunday')] = __('Sun'); 
  138. $this->weekday_abbrev[__('Monday')] = __('Mon'); 
  139. $this->weekday_abbrev[__('Tuesday')] = __('Tue'); 
  140. $this->weekday_abbrev[__('Wednesday')] = __('Wed'); 
  141. $this->weekday_abbrev[__('Thursday')] = __('Thu'); 
  142. $this->weekday_abbrev[__('Friday')] = __('Fri'); 
  143. $this->weekday_abbrev[__('Saturday')] = __('Sat'); 
  144.  
  145. // The Months 
  146. $this->month['01'] = __('January'); 
  147. $this->month['02'] = __('February'); 
  148. $this->month['03'] = __('March'); 
  149. $this->month['04'] = __('April'); 
  150. $this->month['05'] = __('May'); 
  151. $this->month['06'] = __('June'); 
  152. $this->month['07'] = __('July'); 
  153. $this->month['08'] = __('August'); 
  154. $this->month['09'] = __('September'); 
  155. $this->month['10'] = __('October'); 
  156. $this->month['11'] = __('November'); 
  157. $this->month['12'] = __('December'); 
  158.  
  159. // Abbreviations for each month. Uses the same hack as above to get around the 
  160. // 'May' duplication. 
  161. $this->month_abbrev[__('January')] = __('Jan_January_abbreviation'); 
  162. $this->month_abbrev[__('February')] = __('Feb_February_abbreviation'); 
  163. $this->month_abbrev[__('March')] = __('Mar_March_abbreviation'); 
  164. $this->month_abbrev[__('April')] = __('Apr_April_abbreviation'); 
  165. $this->month_abbrev[__('May')] = __('May_May_abbreviation'); 
  166. $this->month_abbrev[__('June')] = __('Jun_June_abbreviation'); 
  167. $this->month_abbrev[__('July')] = __('Jul_July_abbreviation'); 
  168. $this->month_abbrev[__('August')] = __('Aug_August_abbreviation'); 
  169. $this->month_abbrev[__('September')] = __('Sep_September_abbreviation'); 
  170. $this->month_abbrev[__('October')] = __('Oct_October_abbreviation'); 
  171. $this->month_abbrev[__('November')] = __('Nov_November_abbreviation'); 
  172. $this->month_abbrev[__('December')] = __('Dec_December_abbreviation'); 
  173.  
  174. foreach ($this->month_abbrev as $month_ => $month_abbrev_) { 
  175. $this->month_abbrev[$month_] = preg_replace('/_.+_abbreviation$/', '', $month_abbrev_); 
  176.  
  177. // The Meridiems 
  178. $this->meridiem['am'] = __('am'); 
  179. $this->meridiem['pm'] = __('pm'); 
  180. $this->meridiem['AM'] = __('AM'); 
  181. $this->meridiem['PM'] = __('PM'); 
  182.  
  183. // Numbers formatting 
  184. // See http://php.net/number_format 
  185.  
  186. /** translators: $decimals argument for http://php.net/number_format, default is 0 */ 
  187. $trans = __('number_format_decimals'); 
  188. $this->number_format['decimals'] = ('number_format_decimals' == $trans) ? 0 : $trans; 
  189.  
  190. /** translators: $dec_point argument for http://php.net/number_format, default is . */ 
  191. $trans = __('number_format_decimal_point'); 
  192. $this->number_format['decimal_point'] = ('number_format_decimal_point' == $trans) ? '.' : $trans; 
  193.  
  194. /** translators: $thousands_sep argument for http://php.net/number_format, default is , */ 
  195. $trans = __('number_format_thousands_sep'); 
  196. $this->number_format['thousands_sep'] = ('number_format_thousands_sep' == $trans) ? ', ' : $trans; 
  197.  
  198. // Date/Time formatting 
  199. $this->datetime_formatstring['datetime'] = __('F j, Y - h:i A'); 
  200. $this->datetime_formatstring['date'] = __('F j, Y'); 
  201. $this->datetime_formatstring['time'] = __('h:i A'); 
  202.  
  203. $this->_load_locale_data(); 
  204.  
  205. /** 
  206. * Imports global locale vars set during inclusion of $locale.php. 
  207. * 
  208. * @access private 
  209. */ 
  210. function _load_locale_data() { 
  211. $locale = bb_get_locale(); 
  212. $locale_file = BB_LANG_DIR . $locale . '.php'; 
  213. if ( !is_file( $locale_file ) ) { 
  214. return; 
  215.  
  216. include( $locale_file ); 
  217.  
  218. foreach ( $this->locale_vars as $var ) { 
  219. $this->$var = $$var; 
  220.  
  221. /** 
  222. * Retrieve the full translated weekday word. 
  223. * 
  224. * Week starts on translated Sunday and can be fetched 
  225. * by using 0 (zero). So the week starts with 0 (zero) 
  226. * and ends on Saturday with is fetched by using 6 (six). 
  227. * 
  228. * @access public 
  229. * 
  230. * @param int $weekday_number 0 for Sunday through 6 Saturday 
  231. * @return string Full translated weekday 
  232. */ 
  233. function get_weekday($weekday_number) { 
  234. return $this->weekday[$weekday_number]; 
  235.  
  236. /** 
  237. * Retrieve the translated weekday initial. 
  238. * 
  239. * The weekday initial is retrieved by the translated 
  240. * full weekday word. When translating the weekday initial 
  241. * pay attention to make sure that the starting letter does 
  242. * not conflict. 
  243. * 
  244. * @access public 
  245. * 
  246. * @param string $weekday_name 
  247. * @return string 
  248. */ 
  249. function get_weekday_initial($weekday_name) { 
  250. return $this->weekday_initial[$weekday_name]; 
  251.  
  252. /** 
  253. * Retrieve the translated weekday abbreviation. 
  254. * 
  255. * The weekday abbreviation is retrieved by the translated 
  256. * full weekday word. 
  257. * 
  258. * @access public 
  259. * 
  260. * @param string $weekday_name Full translated weekday word 
  261. * @return string Translated weekday abbreviation 
  262. */ 
  263. function get_weekday_abbrev($weekday_name) { 
  264. return $this->weekday_abbrev[$weekday_name]; 
  265.  
  266. /** 
  267. * Retrieve the full translated month by month number. 
  268. * 
  269. * The $month_number parameter has to be a string 
  270. * because it must have the '0' in front of any number 
  271. * that is less than 10. Starts from '01' and ends at 
  272. * '12'. 
  273. * 
  274. * You can use an integer instead and it will add the 
  275. * '0' before the numbers less than 10 for you. 
  276. * 
  277. * @access public 
  278. * 
  279. * @param string|int $month_number '01' through '12' 
  280. * @return string Translated full month name 
  281. */ 
  282. function get_month($month_number) { 
  283. return $this->month[zeroise($month_number, 2)]; 
  284.  
  285. function get_month_initial($month_name) { 
  286. return $this->month_initial[$month_name]; 
  287.  
  288. /** 
  289. * Retrieve translated version of month abbreviation string. 
  290. * 
  291. * The $month_name parameter is expected to be the translated or 
  292. * translatable version of the month. 
  293. * 
  294. * @access public 
  295. * 
  296. * @param string $month_name Translated month to get abbreviated version 
  297. * @return string Translated abbreviated month 
  298. */ 
  299. function get_month_abbrev($month_name) { 
  300. return $this->month_abbrev[$month_name]; 
  301.  
  302. /** 
  303. * Retrieve translated version of meridiem string. 
  304. * 
  305. * The $meridiem parameter is expected to not be translated. 
  306. * 
  307. * @access public 
  308. * 
  309. * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version. 
  310. * @return string Translated version 
  311. */ 
  312. function get_meridiem($meridiem) { 
  313. return $this->meridiem[$meridiem]; 
  314.  
  315. function get_datetime_formatstring($type = 'datetime') { 
  316. return $this->datetime_formatstring[$type]; 
  317.  
  318. /** 
  319. * Global variables are deprecated. For backwards compatibility only. 
  320. * 
  321. * @deprecated For backwards compatibility only. 
  322. * @access private 
  323. */ 
  324. function register_globals() { 
  325. $GLOBALS['weekday'] = $this->weekday; 
  326. $GLOBALS['weekday_initial'] = $this->weekday_initial; 
  327. $GLOBALS['weekday_abbrev'] = $this->weekday_abbrev; 
  328. $GLOBALS['month'] = $this->month; 
  329. $GLOBALS['month_abbrev'] = $this->month_abbrev; 
  330.  
  331. /** 
  332. * PHP4 style constructor which calls helper methods to set up object variables 
  333. * 
  334. * @uses BB_Locale::init() 
  335. * @uses BB_Locale::register_globals() 
  336. * 
  337. * @return BB_Locale 
  338. */ 
  339. function __construct() { 
  340. $this->init(); 
  341. $this->register_globals(); 
  342.  
  343. function BB_Locale() { 
  344. $this->__construct(); 
  345.  
  346. /** 
  347. * Retrieve the date in localized format, based on timestamp. 
  348. * 
  349. * If the locale specifies the locale month and weekday, then the locale will 
  350. * take over the format for the date. If it isn't, then the date format string 
  351. * will be used instead. 
  352. * 
  353. * @param string $dateformatstring Format to display the date. 
  354. * @param int $unixtimestamp Optional. Unix timestamp. 
  355. * @param bool $gmt Optional, default is true. Whether to convert to GMT for time. 
  356. * @return string The date, translated if locale specifies it. 
  357. */ 
  358. function bb_gmdate_i18n( $dateformatstring, $unixtimestamp = false, $gmt = true ) { 
  359. global $bb_locale; 
  360. $i = $unixtimestamp; 
  361. // Sanity check for PHP 5.1.0- 
  362. if ( false === $i || intval($i) < 0 ) { 
  363. if ( ! $gmt ) 
  364. $i = current_time( 'timestamp' ); 
  365. else 
  366. $i = time(); 
  367. // we should not let date() interfere with our 
  368. // specially computed timestamp 
  369. $gmt = true; 
  370.  
  371. // store original value for language with untypical grammars 
  372. // see http://core.trac.wordpress.org/ticket/9396 
  373. $req_format = $dateformatstring; 
  374.  
  375. $datefunc = $gmt? 'gmdate' : 'date'; 
  376.  
  377. if ( ( !empty( $bb_locale->month ) ) && ( !empty( $bb_locale->weekday ) ) ) { 
  378. $datemonth = $bb_locale->get_month( $datefunc( 'm', $i ) ); 
  379. $datemonth_abbrev = $bb_locale->get_month_abbrev( $datemonth ); 
  380. $dateweekday = $bb_locale->get_weekday( $datefunc( 'w', $i ) ); 
  381. $dateweekday_abbrev = $bb_locale->get_weekday_abbrev( $dateweekday ); 
  382. $datemeridiem = $bb_locale->get_meridiem( $datefunc( 'a', $i ) ); 
  383. $datemeridiem_capital = $bb_locale->get_meridiem( $datefunc( 'A', $i ) ); 
  384. $dateformatstring = ' '.$dateformatstring; 
  385. $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring ); 
  386. $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring ); 
  387. $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring ); 
  388. $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring ); 
  389. $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring ); 
  390. $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring ); 
  391.  
  392. $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 ); 
  393. $j = @$datefunc( $dateformatstring, $i ); 
  394. // allow plugins to redo this entirely for languages with untypical grammars 
  395. $j = apply_filters('bb_gmdate_i18n', $j, $req_format, $i, $gmt); 
  396. return $j; 
  397.  
  398. function bb_get_datetime_formatstring_i18n( $type = 'datetime' ) { 
  399. $formatstring = bb_get_option( $type . '_format' ); 
  400. if ( empty($formatstring) ) { 
  401. global $bb_locale; 
  402. $formatstring = $bb_locale->get_datetime_formatstring( $type ); 
  403. return $formatstring; 
  404.  
  405. function bb_datetime_format_i18n( $unixtimestamp, $type = 'datetime', $formatstring = '', $gmt = true ) { 
  406. if ( empty($formatstring) ) { 
  407. $formatstring = bb_get_datetime_formatstring_i18n( $type ); 
  408. return bb_gmdate_i18n( $formatstring, bb_offset_time( $unixtimestamp ), $gmt ); 
  409.  
  410. /** 
  411. * Convert number to format based on the locale. 
  412. * 
  413. * @since 2.3.0 
  414. * 
  415. * @param mixed $number The number to convert based on locale. 
  416. * @param int $decimals Precision of the number of decimal places. 
  417. * @return string Converted number in string format. 
  418. */ 
  419. function bb_number_format_i18n( $number, $decimals = null ) { 
  420. global $bb_locale; 
  421. // let the user override the precision only 
  422. $decimals = ( is_null( $decimals ) ) ? $bb_locale->number_format['decimals'] : intval( $decimals ); 
  423.  
  424. $num = number_format( $number, $decimals, $bb_locale->number_format['decimal_point'], $bb_locale->number_format['thousands_sep'] ); 
  425.  
  426. // let the user translate digits from latin to localized language 
  427. return apply_filters( 'bb_number_format_i18n', $num ); 
.