1<?php
2/**
3 * Locale API: WP_Locale class
4 *
5 * @package WordPress
6 * @subpackage i18n
7 * @since 4.6.0
8 */
9
10/**
11 * Core class used to store translated data for a locale.
12 *
13 * @since 2.1.0
14 * @since 4.6.0 Moved to its own file from wp-includes/locale.php.
15 */
16#[AllowDynamicProperties]
17class WP_Locale {
18 /**
19 * Stores the translated strings for the full weekday names.
20 *
21 * @since 2.1.0
22 * @since 6.2.0 Initialized to an empty array.
23 * @var string[]
24 */
25 public $weekday = array();
26
27 /**
28 * Stores the translated strings for the one character weekday names.
29 *
30 * There is a hack to make sure that Tuesday and Thursday, as well
31 * as Sunday and Saturday, don't conflict. See init() method for more.
32 *
33 * @see WP_Locale::init() for how to handle the hack.
34 *
35 * @since 2.1.0
36 * @since 6.2.0 Initialized to an empty array.
37 * @var string[]
38 */
39 public $weekday_initial = array();
40
41 /**
42 * Stores the translated strings for the abbreviated weekday names.
43 *
44 * @since 2.1.0
45 * @since 6.2.0 Initialized to an empty array.
46 * @var string[]
47 */
48 public $weekday_abbrev = array();
49
50 /**
51 * Stores the translated strings for the full month names.
52 *
53 * @since 2.1.0
54 * @since 6.2.0 Initialized to an empty array.
55 * @var string[]
56 */
57 public $month = array();
58
59 /**
60 * Stores the translated strings for the month names in genitive case, if the locale specifies.
61 *
62 * @since 4.4.0
63 * @since 6.2.0 Initialized to an empty array.
64 * @var string[]
65 */
66 public $month_genitive = array();
67
68 /**
69 * Stores the translated strings for the abbreviated month names.
70 *
71 * @since 2.1.0
72 * @since 6.2.0 Initialized to an empty array.
73 * @var string[]
74 */
75 public $month_abbrev = array();
76
77 /**
78 * Stores the translated strings for 'am' and 'pm'.
79 *
80 * Also the capitalized versions.
81 *
82 * @since 2.1.0
83 * @since 6.2.0 Initialized to an empty array.
84 * @var string[]
85 */
86 public $meridiem = array();
87
88 /**
89 * The text direction of the locale language.
90 *
91 * Default is left to right 'ltr'.
92 *
93 * @since 2.1.0
94 * @var string
95 */
96 public $text_direction = 'ltr';
97
98 /**
99 * The thousands separator and decimal point values used for localizing numbers.
100 *
101 * @since 2.3.0
102 * @since 6.2.0 Initialized to an empty array.
103 * @var array
104 */
105 public $number_format = array();
106
107 /**
108 * The separator string used for localizing list item separator.
109 *
110 * @since 6.0.0
111 * @var string
112 */
113 public $list_item_separator;
114
115 /**
116 * The word count type of the locale language.
117 *
118 * Default is 'words'.
119 *
120 * @since 6.2.0
121 * @var string
122 */
123 public $word_count_type;
124
125 /**
126 * Constructor which calls helper methods to set up object variables.
127 *
128 * @since 2.1.0
129 */
130 public function __construct() {
131 $this->init();
132 $this->register_globals();
133 }
134
135 /**
136 * Sets up the translated strings and object properties.
137 *
138 * The method creates the translatable strings for various
139 * calendar elements. Which allows for specifying locale
140 * specific calendar names and text direction.
141 *
142 * @since 2.1.0
143 *
144 * @global string $text_direction
145 */
146 public function init() {
147 // The weekdays.
148 $this->weekday[0] = /* translators: Weekday. */ __( 'Sunday' );
149 $this->weekday[1] = /* translators: Weekday. */ __( 'Monday' );
150 $this->weekday[2] = /* translators: Weekday. */ __( 'Tuesday' );
151 $this->weekday[3] = /* translators: Weekday. */ __( 'Wednesday' );
152 $this->weekday[4] = /* translators: Weekday. */ __( 'Thursday' );
153 $this->weekday[5] = /* translators: Weekday. */ __( 'Friday' );
154 $this->weekday[6] = /* translators: Weekday. */ __( 'Saturday' );
155
156 // The first letter of each day.
157 $this->weekday_initial[ $this->weekday[0] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Sunday initial' );
158 $this->weekday_initial[ $this->weekday[1] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'M', 'Monday initial' );
159 $this->weekday_initial[ $this->weekday[2] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Tuesday initial' );
160 $this->weekday_initial[ $this->weekday[3] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'W', 'Wednesday initial' );
161 $this->weekday_initial[ $this->weekday[4] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Thursday initial' );
162 $this->weekday_initial[ $this->weekday[5] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'F', 'Friday initial' );
163 $this->weekday_initial[ $this->weekday[6] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Saturday initial' );
164
165 // Abbreviations for each day.
166 $this->weekday_abbrev[ $this->weekday[0] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sun' );
167 $this->weekday_abbrev[ $this->weekday[1] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Mon' );
168 $this->weekday_abbrev[ $this->weekday[2] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Tue' );
169 $this->weekday_abbrev[ $this->weekday[3] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Wed' );
170 $this->weekday_abbrev[ $this->weekday[4] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Thu' );
171 $this->weekday_abbrev[ $this->weekday[5] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Fri' );
172 $this->weekday_abbrev[ $this->weekday[6] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sat' );
173
174 // The months.
175 $this->month['01'] = /* translators: Month name. */ __( 'January' );
176 $this->month['02'] = /* translators: Month name. */ __( 'February' );
177 $this->month['03'] = /* translators: Month name. */ __( 'March' );
178 $this->month['04'] = /* translators: Month name. */ __( 'April' );
179 $this->month['05'] = /* translators: Month name. */ __( 'May' );
180 $this->month['06'] = /* translators: Month name. */ __( 'June' );
181 $this->month['07'] = /* translators: Month name. */ __( 'July' );
182 $this->month['08'] = /* translators: Month name. */ __( 'August' );
183 $this->month['09'] = /* translators: Month name. */ __( 'September' );
184 $this->month['10'] = /* translators: Month name. */ __( 'October' );
185 $this->month['11'] = /* translators: Month name. */ __( 'November' );
186 $this->month['12'] = /* translators: Month name. */ __( 'December' );
187
188 // The months, genitive.
189 $this->month_genitive['01'] = /* translators: Month name, genitive. */ _x( 'January', 'genitive' );
190 $this->month_genitive['02'] = /* translators: Month name, genitive. */ _x( 'February', 'genitive' );
191 $this->month_genitive['03'] = /* translators: Month name, genitive. */ _x( 'March', 'genitive' );
192 $this->month_genitive['04'] = /* translators: Month name, genitive. */ _x( 'April', 'genitive' );
193 $this->month_genitive['05'] = /* translators: Month name, genitive. */ _x( 'May', 'genitive' );
194 $this->month_genitive['06'] = /* translators: Month name, genitive. */ _x( 'June', 'genitive' );
195 $this->month_genitive['07'] = /* translators: Month name, genitive. */ _x( 'July', 'genitive' );
196 $this->month_genitive['08'] = /* translators: Month name, genitive. */ _x( 'August', 'genitive' );
197 $this->month_genitive['09'] = /* translators: Month name, genitive. */ _x( 'September', 'genitive' );
198 $this->month_genitive['10'] = /* translators: Month name, genitive. */ _x( 'October', 'genitive' );
199 $this->month_genitive['11'] = /* translators: Month name, genitive. */ _x( 'November', 'genitive' );
200 $this->month_genitive['12'] = /* translators: Month name, genitive. */ _x( 'December', 'genitive' );
201
202 // Abbreviations for each month.
203 $this->month_abbrev[ $this->month['01'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jan', 'January abbreviation' );
204 $this->month_abbrev[ $this->month['02'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Feb', 'February abbreviation' );
205 $this->month_abbrev[ $this->month['03'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Mar', 'March abbreviation' );
206 $this->month_abbrev[ $this->month['04'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Apr', 'April abbreviation' );
207 $this->month_abbrev[ $this->month['05'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'May', 'May abbreviation' );
208 $this->month_abbrev[ $this->month['06'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jun', 'June abbreviation' );
209 $this->month_abbrev[ $this->month['07'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jul', 'July abbreviation' );
210 $this->month_abbrev[ $this->month['08'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Aug', 'August abbreviation' );
211 $this->month_abbrev[ $this->month['09'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Sep', 'September abbreviation' );
212 $this->month_abbrev[ $this->month['10'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Oct', 'October abbreviation' );
213 $this->month_abbrev[ $this->month['11'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Nov', 'November abbreviation' );
214 $this->month_abbrev[ $this->month['12'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Dec', 'December abbreviation' );
215
216 // The meridiems.
217 $this->meridiem['am'] = __( 'am' );
218 $this->meridiem['pm'] = __( 'pm' );
219 $this->meridiem['AM'] = __( 'AM' );
220 $this->meridiem['PM'] = __( 'PM' );
221
222 /*
223 * Numbers formatting.
224 * See https://www.php.net/number_format
225 */
226
227 /* translators: $thousands_sep argument for https://www.php.net/number_format, default is ',' */
228 $thousands_sep = __( 'number_format_thousands_sep' );
229
230 // Replace space with a non-breaking space to avoid wrapping.
231 $thousands_sep = str_replace( ' ', ' ', $thousands_sep );
232
233 $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
234
235 /* translators: $dec_point argument for https://www.php.net/number_format, default is '.' */
236 $decimal_point = __( 'number_format_decimal_point' );
237
238 $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
239
240 /* translators: Used between list items, there is a space after the comma. */
241 $this->list_item_separator = __( ', ' );
242
243 // Set text direction.
244 if ( isset( $GLOBALS['text_direction'] ) ) {
245 $this->text_direction = $GLOBALS['text_direction'];
246
247 /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
248 } elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) {
249 $this->text_direction = 'rtl';
250 }
251
252 // Set the word count type.
253 $this->word_count_type = $this->get_word_count_type();
254 }
255
256 /**
257 * Retrieves the full translated weekday word.
258 *
259 * Week starts on translated Sunday and can be fetched
260 * by using 0 (zero). So the week starts with 0 (zero)
261 * and ends on Saturday with is fetched by using 6 (six).
262 *
263 * @since 2.1.0
264 *
265 * @param int $weekday_number 0 for Sunday through 6 Saturday.
266 * @return string Full translated weekday.
267 */
268 public function get_weekday( $weekday_number ) {
269 return $this->weekday[ $weekday_number ];
270 }
271
272 /**
273 * Retrieves the translated weekday initial.
274 *
275 * The weekday initial is retrieved by the translated
276 * full weekday word. When translating the weekday initial
277 * pay attention to make sure that the starting letter does
278 * not conflict.
279 *
280 * @since 2.1.0
281 *
282 * @param string $weekday_name Full translated weekday word.
283 * @return string Translated weekday initial.
284 */
285 public function get_weekday_initial( $weekday_name ) {
286 return $this->weekday_initial[ $weekday_name ];
287 }
288
289 /**
290 * Retrieves the translated weekday abbreviation.
291 *
292 * The weekday abbreviation is retrieved by the translated
293 * full weekday word.
294 *
295 * @since 2.1.0
296 *
297 * @param string $weekday_name Full translated weekday word.
298 * @return string Translated weekday abbreviation.
299 */
300 public function get_weekday_abbrev( $weekday_name ) {
301 return $this->weekday_abbrev[ $weekday_name ];
302 }
303
304 /**
305 * Retrieves the full translated month by month number.
306 *
307 * The $month_number parameter has to be a string
308 * because it must have the '0' in front of any number
309 * that is less than 10. Starts from '01' and ends at
310 * '12'.
311 *
312 * You can use an integer instead and it will add the
313 * '0' before the numbers less than 10 for you.
314 *
315 * @since 2.1.0
316 *
317 * @param string|int $month_number '01' through '12'.
318 * @return string Translated full month name. If the month number is not found, an empty string is returned.
319 */
320 public function get_month( $month_number ) {
321 $month_number = zeroise( $month_number, 2 );
322 if ( ! isset( $this->month[ $month_number ] ) ) {
323 return '';
324 }
325 return $this->month[ $month_number ];
326 }
327
328 /**
329 * Retrieves translated version of month abbreviation string.
330 *
331 * The $month_name parameter is expected to be the translated or
332 * translatable version of the month.
333 *
334 * @since 2.1.0
335 *
336 * @param string $month_name Translated month to get abbreviated version.
337 * @return string Translated abbreviated month.
338 */
339 public function get_month_abbrev( $month_name ) {
340 return $this->month_abbrev[ $month_name ];
341 }
342
343 /**
344 * Retrieves translated version of month genitive string.
345 *
346 * The $month_number parameter has to be a string
347 * because it must have the '0' in front of any number
348 * that is less than 10. Starts from '01' and ends at
349 * '12'.
350 *
351 * You can use an integer instead and it will add the
352 * '0' before the numbers less than 10 for you.
353 *
354 * @since 6.8.0
355 *
356 * @param string|int $month_number '01' through '12'.
357 * @return string Translated genitive month name.
358 */
359 public function get_month_genitive( $month_number ) {
360 return $this->month_genitive[ zeroise( $month_number, 2 ) ];
361 }
362
363 /**
364 * Retrieves translated version of meridiem string.
365 *
366 * The $meridiem parameter is expected to not be translated.
367 *
368 * @since 2.1.0
369 *
370 * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
371 * @return string Translated version
372 */
373 public function get_meridiem( $meridiem ) {
374 return $this->meridiem[ $meridiem ];
375 }
376
377 /**
378 * Global variables are deprecated.
379 *
380 * For backward compatibility only.
381 *
382 * @since 2.1.0
383 * @deprecated For backward compatibility only.
384 *
385 * @global array $weekday
386 * @global array $weekday_initial
387 * @global array $weekday_abbrev
388 * @global array $month
389 * @global array $month_abbrev
390 */
391 public function register_globals() {
392 $GLOBALS['weekday'] = $this->weekday;
393 $GLOBALS['weekday_initial'] = $this->weekday_initial;
394 $GLOBALS['weekday_abbrev'] = $this->weekday_abbrev;
395 $GLOBALS['month'] = $this->month;
396 $GLOBALS['month_abbrev'] = $this->month_abbrev;
397 }
398
399 /**
400 * Checks if current locale is RTL.
401 *
402 * @since 3.0.0
403 * @return bool Whether locale is RTL.
404 */
405 public function is_rtl() {
406 return 'rtl' === $this->text_direction;
407 }
408
409 /**
410 * Registers date/time format strings for general POT.
411 *
412 * Private, unused method to add some date/time formats translated
413 * on wp-admin/options-general.php to the general POT that would
414 * otherwise be added to the admin POT.
415 *
416 * @since 3.6.0
417 */
418 public function _strings_for_pot() {
419 /* translators: Localized date format, see https://www.php.net/manual/datetime.format.php */
420 __( 'F j, Y' );
421 /* translators: Localized time format, see https://www.php.net/manual/datetime.format.php */
422 __( 'g:i a' );
423 /* translators: Localized date and time format, see https://www.php.net/manual/datetime.format.php */
424 __( 'F j, Y g:i a' );
425 }
426
427 /**
428 * Retrieves the localized list item separator.
429 *
430 * @since 6.0.0
431 *
432 * @return string Localized list item separator.
433 */
434 public function get_list_item_separator() {
435 return $this->list_item_separator;
436 }
437
438 /**
439 * Retrieves the localized word count type.
440 *
441 * @since 6.2.0
442 *
443 * @return string Localized word count type. Possible values are `characters_excluding_spaces`,
444 * `characters_including_spaces`, or `words`. Defaults to `words`.
445 */
446 public function get_word_count_type() {
447
448 /*
449 * translators: If your word count is based on single characters (e.g. East Asian characters),
450 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
451 * Do not translate into your own language.
452 */
453 $word_count_type = is_null( $this->word_count_type ) ? _x( 'words', 'Word count type. Do not translate!' ) : $this->word_count_type;
454
455 // Check for valid types.
456 if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) {
457 // Defaults to 'words'.
458 $word_count_type = 'words';
459 }
460
461 return $word_count_type;
462 }
463}
464