class-wp-locale.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /**
  3. * Locale API: WP_Locale class
  4. *
  5. * @package WordPress
  6. * @subpackage i18n
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used to store translated data for a locale.
  11. *
  12. * @since 2.1.0
  13. * @since 4.6.0 Moved to its own file from wp-includes/locale.php.
  14. */
  15. class WP_Locale {
  16. /**
  17. * Stores the translated strings for the full weekday names.
  18. *
  19. * @since 2.1.0
  20. * @var array
  21. */
  22. public $weekday;
  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 WP_Locale::init() for how to handle the hack.
  30. *
  31. * @since 2.1.0
  32. * @var array
  33. */
  34. public $weekday_initial;
  35. /**
  36. * Stores the translated strings for the abbreviated weekday names.
  37. *
  38. * @since 2.1.0
  39. * @var array
  40. */
  41. public $weekday_abbrev;
  42. /**
  43. * Stores the default start of the week.
  44. *
  45. * @since 4.4.0
  46. * @var string
  47. */
  48. public $start_of_week;
  49. /**
  50. * Stores the translated strings for the full month names.
  51. *
  52. * @since 2.1.0
  53. * @var array
  54. */
  55. public $month;
  56. /**
  57. * Stores the translated strings for the month names in genitive case, if the locale specifies.
  58. *
  59. * @since 4.4.0
  60. * @var array
  61. */
  62. public $month_genitive;
  63. /**
  64. * Stores the translated strings for the abbreviated month names.
  65. *
  66. * @since 2.1.0
  67. * @var array
  68. */
  69. public $month_abbrev;
  70. /**
  71. * Stores the translated strings for 'am' and 'pm'.
  72. *
  73. * Also the capitalized versions.
  74. *
  75. * @since 2.1.0
  76. * @var array
  77. */
  78. public $meridiem;
  79. /**
  80. * The text direction of the locale language.
  81. *
  82. * Default is left to right 'ltr'.
  83. *
  84. * @since 2.1.0
  85. * @var string
  86. */
  87. public $text_direction = 'ltr';
  88. /**
  89. * The thousands separator and decimal point values used for localizing numbers.
  90. *
  91. * @since 2.3.0
  92. * @access public
  93. * @var array
  94. */
  95. public $number_format;
  96. /**
  97. * Constructor which calls helper methods to set up object variables.
  98. *
  99. * @since 2.1.0
  100. */
  101. public function __construct() {
  102. $this->init();
  103. $this->register_globals();
  104. }
  105. /**
  106. * Sets up the translated strings and object properties.
  107. *
  108. * The method creates the translatable strings for various
  109. * calendar elements. Which allows for specifying locale
  110. * specific calendar names and text direction.
  111. *
  112. * @since 2.1.0
  113. * @access public
  114. *
  115. * @global string $text_direction
  116. */
  117. public function init() {
  118. // The Weekdays
  119. $this->weekday[0] = /* translators: weekday */ __('Sunday');
  120. $this->weekday[1] = /* translators: weekday */ __('Monday');
  121. $this->weekday[2] = /* translators: weekday */ __('Tuesday');
  122. $this->weekday[3] = /* translators: weekday */ __('Wednesday');
  123. $this->weekday[4] = /* translators: weekday */ __('Thursday');
  124. $this->weekday[5] = /* translators: weekday */ __('Friday');
  125. $this->weekday[6] = /* translators: weekday */ __('Saturday');
  126. // The first letter of each day.
  127. $this->weekday_initial[ __( 'Sunday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Sunday initial' );
  128. $this->weekday_initial[ __( 'Monday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'M', 'Monday initial' );
  129. $this->weekday_initial[ __( 'Tuesday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Tuesday initial' );
  130. $this->weekday_initial[ __( 'Wednesday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'W', 'Wednesday initial' );
  131. $this->weekday_initial[ __( 'Thursday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Thursday initial' );
  132. $this->weekday_initial[ __( 'Friday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'F', 'Friday initial' );
  133. $this->weekday_initial[ __( 'Saturday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Saturday initial' );
  134. // Abbreviations for each day.
  135. $this->weekday_abbrev[__('Sunday')] = /* translators: three-letter abbreviation of the weekday */ __('Sun');
  136. $this->weekday_abbrev[__('Monday')] = /* translators: three-letter abbreviation of the weekday */ __('Mon');
  137. $this->weekday_abbrev[__('Tuesday')] = /* translators: three-letter abbreviation of the weekday */ __('Tue');
  138. $this->weekday_abbrev[__('Wednesday')] = /* translators: three-letter abbreviation of the weekday */ __('Wed');
  139. $this->weekday_abbrev[__('Thursday')] = /* translators: three-letter abbreviation of the weekday */ __('Thu');
  140. $this->weekday_abbrev[__('Friday')] = /* translators: three-letter abbreviation of the weekday */ __('Fri');
  141. $this->weekday_abbrev[__('Saturday')] = /* translators: three-letter abbreviation of the weekday */ __('Sat');
  142. // The Months
  143. $this->month['01'] = /* translators: month name */ __( 'January' );
  144. $this->month['02'] = /* translators: month name */ __( 'February' );
  145. $this->month['03'] = /* translators: month name */ __( 'March' );
  146. $this->month['04'] = /* translators: month name */ __( 'April' );
  147. $this->month['05'] = /* translators: month name */ __( 'May' );
  148. $this->month['06'] = /* translators: month name */ __( 'June' );
  149. $this->month['07'] = /* translators: month name */ __( 'July' );
  150. $this->month['08'] = /* translators: month name */ __( 'August' );
  151. $this->month['09'] = /* translators: month name */ __( 'September' );
  152. $this->month['10'] = /* translators: month name */ __( 'October' );
  153. $this->month['11'] = /* translators: month name */ __( 'November' );
  154. $this->month['12'] = /* translators: month name */ __( 'December' );
  155. // The Months, genitive
  156. $this->month_genitive['01'] = /* translators: month name, genitive */ _x( 'January', 'genitive' );
  157. $this->month_genitive['02'] = /* translators: month name, genitive */ _x( 'February', 'genitive' );
  158. $this->month_genitive['03'] = /* translators: month name, genitive */ _x( 'March', 'genitive' );
  159. $this->month_genitive['04'] = /* translators: month name, genitive */ _x( 'April', 'genitive' );
  160. $this->month_genitive['05'] = /* translators: month name, genitive */ _x( 'May', 'genitive' );
  161. $this->month_genitive['06'] = /* translators: month name, genitive */ _x( 'June', 'genitive' );
  162. $this->month_genitive['07'] = /* translators: month name, genitive */ _x( 'July', 'genitive' );
  163. $this->month_genitive['08'] = /* translators: month name, genitive */ _x( 'August', 'genitive' );
  164. $this->month_genitive['09'] = /* translators: month name, genitive */ _x( 'September', 'genitive' );
  165. $this->month_genitive['10'] = /* translators: month name, genitive */ _x( 'October', 'genitive' );
  166. $this->month_genitive['11'] = /* translators: month name, genitive */ _x( 'November', 'genitive' );
  167. $this->month_genitive['12'] = /* translators: month name, genitive */ _x( 'December', 'genitive' );
  168. // Abbreviations for each month.
  169. $this->month_abbrev[ __( 'January' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Jan', 'January abbreviation' );
  170. $this->month_abbrev[ __( 'February' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Feb', 'February abbreviation' );
  171. $this->month_abbrev[ __( 'March' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Mar', 'March abbreviation' );
  172. $this->month_abbrev[ __( 'April' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Apr', 'April abbreviation' );
  173. $this->month_abbrev[ __( 'May' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'May', 'May abbreviation' );
  174. $this->month_abbrev[ __( 'June' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Jun', 'June abbreviation' );
  175. $this->month_abbrev[ __( 'July' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Jul', 'July abbreviation' );
  176. $this->month_abbrev[ __( 'August' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Aug', 'August abbreviation' );
  177. $this->month_abbrev[ __( 'September' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Sep', 'September abbreviation' );
  178. $this->month_abbrev[ __( 'October' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Oct', 'October abbreviation' );
  179. $this->month_abbrev[ __( 'November' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Nov', 'November abbreviation' );
  180. $this->month_abbrev[ __( 'December' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Dec', 'December abbreviation' );
  181. // The Meridiems
  182. $this->meridiem['am'] = __('am');
  183. $this->meridiem['pm'] = __('pm');
  184. $this->meridiem['AM'] = __('AM');
  185. $this->meridiem['PM'] = __('PM');
  186. // Numbers formatting
  187. // See https://secure.php.net/number_format
  188. /* translators: $thousands_sep argument for https://secure.php.net/number_format, default is , */
  189. $thousands_sep = __( 'number_format_thousands_sep' );
  190. if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
  191. // Replace space with a non-breaking space to avoid wrapping.
  192. $thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
  193. } else {
  194. // PHP < 5.4.0 does not support multiple bytes in thousands separator.
  195. $thousands_sep = str_replace( array( '&nbsp;', '&#160;' ), ' ', $thousands_sep );
  196. }
  197. $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
  198. /* translators: $dec_point argument for https://secure.php.net/number_format, default is . */
  199. $decimal_point = __( 'number_format_decimal_point' );
  200. $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
  201. // Set text direction.
  202. if ( isset( $GLOBALS['text_direction'] ) )
  203. $this->text_direction = $GLOBALS['text_direction'];
  204. /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
  205. elseif ( 'rtl' == _x( 'ltr', 'text direction' ) )
  206. $this->text_direction = 'rtl';
  207. if ( 'rtl' === $this->text_direction && strpos( get_bloginfo( 'version' ), '-src' ) ) {
  208. $this->text_direction = 'ltr';
  209. add_action( 'all_admin_notices', array( $this, 'rtl_src_admin_notice' ) );
  210. }
  211. }
  212. /**
  213. * Outputs an admin notice if the /build directory must be used for RTL.
  214. *
  215. * @since 3.8.0
  216. * @access public
  217. */
  218. public function rtl_src_admin_notice() {
  219. /* translators: %s: Name of the directory (build) */
  220. echo '<div class="error"><p>' . sprintf( __( 'The %s directory of the develop repository must be used for RTL.' ), '<code>build</code>' ) . '</p></div>';
  221. }
  222. /**
  223. * Retrieve the full translated weekday word.
  224. *
  225. * Week starts on translated Sunday and can be fetched
  226. * by using 0 (zero). So the week starts with 0 (zero)
  227. * and ends on Saturday with is fetched by using 6 (six).
  228. *
  229. * @since 2.1.0
  230. * @access public
  231. *
  232. * @param int $weekday_number 0 for Sunday through 6 Saturday
  233. * @return string Full translated weekday
  234. */
  235. public function get_weekday($weekday_number) {
  236. return $this->weekday[$weekday_number];
  237. }
  238. /**
  239. * Retrieve the translated weekday initial.
  240. *
  241. * The weekday initial is retrieved by the translated
  242. * full weekday word. When translating the weekday initial
  243. * pay attention to make sure that the starting letter does
  244. * not conflict.
  245. *
  246. * @since 2.1.0
  247. * @access public
  248. *
  249. * @param string $weekday_name
  250. * @return string
  251. */
  252. public function get_weekday_initial($weekday_name) {
  253. return $this->weekday_initial[$weekday_name];
  254. }
  255. /**
  256. * Retrieve the translated weekday abbreviation.
  257. *
  258. * The weekday abbreviation is retrieved by the translated
  259. * full weekday word.
  260. *
  261. * @since 2.1.0
  262. * @access public
  263. *
  264. * @param string $weekday_name Full translated weekday word
  265. * @return string Translated weekday abbreviation
  266. */
  267. public function get_weekday_abbrev($weekday_name) {
  268. return $this->weekday_abbrev[$weekday_name];
  269. }
  270. /**
  271. * Retrieve the full translated month by month number.
  272. *
  273. * The $month_number parameter has to be a string
  274. * because it must have the '0' in front of any number
  275. * that is less than 10. Starts from '01' and ends at
  276. * '12'.
  277. *
  278. * You can use an integer instead and it will add the
  279. * '0' before the numbers less than 10 for you.
  280. *
  281. * @since 2.1.0
  282. * @access public
  283. *
  284. * @param string|int $month_number '01' through '12'
  285. * @return string Translated full month name
  286. */
  287. public function get_month($month_number) {
  288. return $this->month[zeroise($month_number, 2)];
  289. }
  290. /**
  291. * Retrieve translated version of month abbreviation string.
  292. *
  293. * The $month_name parameter is expected to be the translated or
  294. * translatable version of the month.
  295. *
  296. * @since 2.1.0
  297. * @access public
  298. *
  299. * @param string $month_name Translated month to get abbreviated version
  300. * @return string Translated abbreviated month
  301. */
  302. public function get_month_abbrev($month_name) {
  303. return $this->month_abbrev[$month_name];
  304. }
  305. /**
  306. * Retrieve translated version of meridiem string.
  307. *
  308. * The $meridiem parameter is expected to not be translated.
  309. *
  310. * @since 2.1.0
  311. * @access public
  312. *
  313. * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
  314. * @return string Translated version
  315. */
  316. public function get_meridiem($meridiem) {
  317. return $this->meridiem[$meridiem];
  318. }
  319. /**
  320. * Global variables are deprecated.
  321. *
  322. * For backward compatibility only.
  323. *
  324. * @deprecated For backward compatibility only.
  325. * @access public
  326. *
  327. * @global array $weekday
  328. * @global array $weekday_initial
  329. * @global array $weekday_abbrev
  330. * @global array $month
  331. * @global array $month_abbrev
  332. *
  333. * @since 2.1.0
  334. */
  335. public function register_globals() {
  336. $GLOBALS['weekday'] = $this->weekday;
  337. $GLOBALS['weekday_initial'] = $this->weekday_initial;
  338. $GLOBALS['weekday_abbrev'] = $this->weekday_abbrev;
  339. $GLOBALS['month'] = $this->month;
  340. $GLOBALS['month_abbrev'] = $this->month_abbrev;
  341. }
  342. /**
  343. * Checks if current locale is RTL.
  344. *
  345. * @since 3.0.0
  346. * @return bool Whether locale is RTL.
  347. */
  348. public function is_rtl() {
  349. return 'rtl' == $this->text_direction;
  350. }
  351. /**
  352. * Register date/time format strings for general POT.
  353. *
  354. * Private, unused method to add some date/time formats translated
  355. * on wp-admin/options-general.php to the general POT that would
  356. * otherwise be added to the admin POT.
  357. *
  358. * @since 3.6.0
  359. */
  360. public function _strings_for_pot() {
  361. /* translators: localized date format, see https://secure.php.net/date */
  362. __( 'F j, Y' );
  363. /* translators: localized time format, see https://secure.php.net/date */
  364. __( 'g:i a' );
  365. /* translators: localized date and time format, see https://secure.php.net/date */
  366. __( 'F j, Y g:i a' );
  367. }
  368. }