class-wp-locale.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #[AllowDynamicProperties]
  16. class WP_Locale {
  17. /**
  18. * Stores the translated strings for the full weekday names.
  19. *
  20. * @since 2.1.0
  21. * @var string[]
  22. */
  23. public $weekday;
  24. /**
  25. * Stores the translated strings for the one character weekday names.
  26. *
  27. * There is a hack to make sure that Tuesday and Thursday, as well
  28. * as Sunday and Saturday, don't conflict. See init() method for more.
  29. *
  30. * @see WP_Locale::init() for how to handle the hack.
  31. *
  32. * @since 2.1.0
  33. * @var string[]
  34. */
  35. public $weekday_initial;
  36. /**
  37. * Stores the translated strings for the abbreviated weekday names.
  38. *
  39. * @since 2.1.0
  40. * @var string[]
  41. */
  42. public $weekday_abbrev;
  43. /**
  44. * Stores the translated strings for the full month names.
  45. *
  46. * @since 2.1.0
  47. * @var string[]
  48. */
  49. public $month;
  50. /**
  51. * Stores the translated strings for the month names in genitive case, if the locale specifies.
  52. *
  53. * @since 4.4.0
  54. * @var string[]
  55. */
  56. public $month_genitive;
  57. /**
  58. * Stores the translated strings for the abbreviated month names.
  59. *
  60. * @since 2.1.0
  61. * @var string[]
  62. */
  63. public $month_abbrev;
  64. /**
  65. * Stores the translated strings for 'am' and 'pm'.
  66. *
  67. * Also the capitalized versions.
  68. *
  69. * @since 2.1.0
  70. * @var string[]
  71. */
  72. public $meridiem;
  73. /**
  74. * The text direction of the locale language.
  75. *
  76. * Default is left to right 'ltr'.
  77. *
  78. * @since 2.1.0
  79. * @var string
  80. */
  81. public $text_direction = 'ltr';
  82. /**
  83. * The thousands separator and decimal point values used for localizing numbers.
  84. *
  85. * @since 2.3.0
  86. * @var array
  87. */
  88. public $number_format;
  89. /**
  90. * The separator string used for localizing list item separator.
  91. *
  92. * @since 6.0.0
  93. * @var string
  94. */
  95. public $list_item_separator;
  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. *
  114. * @global string $text_direction
  115. * @global string $wp_version The WordPress version string.
  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[ $this->weekday[0] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Sunday initial' );
  128. $this->weekday_initial[ $this->weekday[1] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'M', 'Monday initial' );
  129. $this->weekday_initial[ $this->weekday[2] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Tuesday initial' );
  130. $this->weekday_initial[ $this->weekday[3] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'W', 'Wednesday initial' );
  131. $this->weekday_initial[ $this->weekday[4] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Thursday initial' );
  132. $this->weekday_initial[ $this->weekday[5] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'F', 'Friday initial' );
  133. $this->weekday_initial[ $this->weekday[6] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Saturday initial' );
  134. // Abbreviations for each day.
  135. $this->weekday_abbrev[ $this->weekday[0] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sun' );
  136. $this->weekday_abbrev[ $this->weekday[1] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Mon' );
  137. $this->weekday_abbrev[ $this->weekday[2] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Tue' );
  138. $this->weekday_abbrev[ $this->weekday[3] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Wed' );
  139. $this->weekday_abbrev[ $this->weekday[4] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Thu' );
  140. $this->weekday_abbrev[ $this->weekday[5] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Fri' );
  141. $this->weekday_abbrev[ $this->weekday[6] ] = /* 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[ $this->month['01'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jan', 'January abbreviation' );
  170. $this->month_abbrev[ $this->month['02'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Feb', 'February abbreviation' );
  171. $this->month_abbrev[ $this->month['03'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Mar', 'March abbreviation' );
  172. $this->month_abbrev[ $this->month['04'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Apr', 'April abbreviation' );
  173. $this->month_abbrev[ $this->month['05'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'May', 'May abbreviation' );
  174. $this->month_abbrev[ $this->month['06'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jun', 'June abbreviation' );
  175. $this->month_abbrev[ $this->month['07'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jul', 'July abbreviation' );
  176. $this->month_abbrev[ $this->month['08'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Aug', 'August abbreviation' );
  177. $this->month_abbrev[ $this->month['09'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Sep', 'September abbreviation' );
  178. $this->month_abbrev[ $this->month['10'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Oct', 'October abbreviation' );
  179. $this->month_abbrev[ $this->month['11'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Nov', 'November abbreviation' );
  180. $this->month_abbrev[ $this->month['12'] ] = /* 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://www.php.net/number_format
  188. /* translators: $thousands_sep argument for https://www.php.net/number_format, default is ',' */
  189. $thousands_sep = __( 'number_format_thousands_sep' );
  190. // Replace space with a non-breaking space to avoid wrapping.
  191. $thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
  192. $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
  193. /* translators: $dec_point argument for https://www.php.net/number_format, default is '.' */
  194. $decimal_point = __( 'number_format_decimal_point' );
  195. $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
  196. /* translators: used between list items, there is a space after the comma */
  197. $this->list_item_separator = __( ', ' );
  198. // Set text direction.
  199. if ( isset( $GLOBALS['text_direction'] ) ) {
  200. $this->text_direction = $GLOBALS['text_direction'];
  201. /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
  202. } elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) {
  203. $this->text_direction = 'rtl';
  204. }
  205. }
  206. /**
  207. * Retrieves the full translated weekday word.
  208. *
  209. * Week starts on translated Sunday and can be fetched
  210. * by using 0 (zero). So the week starts with 0 (zero)
  211. * and ends on Saturday with is fetched by using 6 (six).
  212. *
  213. * @since 2.1.0
  214. *
  215. * @param int $weekday_number 0 for Sunday through 6 Saturday.
  216. * @return string Full translated weekday.
  217. */
  218. public function get_weekday( $weekday_number ) {
  219. return $this->weekday[ $weekday_number ];
  220. }
  221. /**
  222. * Retrieves the translated weekday initial.
  223. *
  224. * The weekday initial is retrieved by the translated
  225. * full weekday word. When translating the weekday initial
  226. * pay attention to make sure that the starting letter does
  227. * not conflict.
  228. *
  229. * @since 2.1.0
  230. *
  231. * @param string $weekday_name Full translated weekday word.
  232. * @return string Translated weekday initial.
  233. */
  234. public function get_weekday_initial( $weekday_name ) {
  235. return $this->weekday_initial[ $weekday_name ];
  236. }
  237. /**
  238. * Retrieves the translated weekday abbreviation.
  239. *
  240. * The weekday abbreviation is retrieved by the translated
  241. * full weekday word.
  242. *
  243. * @since 2.1.0
  244. *
  245. * @param string $weekday_name Full translated weekday word.
  246. * @return string Translated weekday abbreviation.
  247. */
  248. public function get_weekday_abbrev( $weekday_name ) {
  249. return $this->weekday_abbrev[ $weekday_name ];
  250. }
  251. /**
  252. * Retrieves the full translated month by month number.
  253. *
  254. * The $month_number parameter has to be a string
  255. * because it must have the '0' in front of any number
  256. * that is less than 10. Starts from '01' and ends at
  257. * '12'.
  258. *
  259. * You can use an integer instead and it will add the
  260. * '0' before the numbers less than 10 for you.
  261. *
  262. * @since 2.1.0
  263. *
  264. * @param string|int $month_number '01' through '12'.
  265. * @return string Translated full month name.
  266. */
  267. public function get_month( $month_number ) {
  268. return $this->month[ zeroise( $month_number, 2 ) ];
  269. }
  270. /**
  271. * Retrieves translated version of month abbreviation string.
  272. *
  273. * The $month_name parameter is expected to be the translated or
  274. * translatable version of the month.
  275. *
  276. * @since 2.1.0
  277. *
  278. * @param string $month_name Translated month to get abbreviated version.
  279. * @return string Translated abbreviated month.
  280. */
  281. public function get_month_abbrev( $month_name ) {
  282. return $this->month_abbrev[ $month_name ];
  283. }
  284. /**
  285. * Retrieves translated version of meridiem string.
  286. *
  287. * The $meridiem parameter is expected to not be translated.
  288. *
  289. * @since 2.1.0
  290. *
  291. * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
  292. * @return string Translated version
  293. */
  294. public function get_meridiem( $meridiem ) {
  295. return $this->meridiem[ $meridiem ];
  296. }
  297. /**
  298. * Global variables are deprecated.
  299. *
  300. * For backward compatibility only.
  301. *
  302. * @deprecated For backward compatibility only.
  303. *
  304. * @global array $weekday
  305. * @global array $weekday_initial
  306. * @global array $weekday_abbrev
  307. * @global array $month
  308. * @global array $month_abbrev
  309. *
  310. * @since 2.1.0
  311. */
  312. public function register_globals() {
  313. $GLOBALS['weekday'] = $this->weekday;
  314. $GLOBALS['weekday_initial'] = $this->weekday_initial;
  315. $GLOBALS['weekday_abbrev'] = $this->weekday_abbrev;
  316. $GLOBALS['month'] = $this->month;
  317. $GLOBALS['month_abbrev'] = $this->month_abbrev;
  318. }
  319. /**
  320. * Checks if current locale is RTL.
  321. *
  322. * @since 3.0.0
  323. * @return bool Whether locale is RTL.
  324. */
  325. public function is_rtl() {
  326. return 'rtl' === $this->text_direction;
  327. }
  328. /**
  329. * Registers date/time format strings for general POT.
  330. *
  331. * Private, unused method to add some date/time formats translated
  332. * on wp-admin/options-general.php to the general POT that would
  333. * otherwise be added to the admin POT.
  334. *
  335. * @since 3.6.0
  336. */
  337. public function _strings_for_pot() {
  338. /* translators: Localized date format, see https://www.php.net/manual/datetime.format.php */
  339. __( 'F j, Y' );
  340. /* translators: Localized time format, see https://www.php.net/manual/datetime.format.php */
  341. __( 'g:i a' );
  342. /* translators: Localized date and time format, see https://www.php.net/manual/datetime.format.php */
  343. __( 'F j, Y g:i a' );
  344. }
  345. /**
  346. * Retrieves the localized list item separator.
  347. *
  348. * @since 6.0.0
  349. *
  350. * @return string Localized list item separator.
  351. */
  352. public function get_list_item_separator() {
  353. return $this->list_item_separator;
  354. }
  355. }