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 */
16class WP_Locale {
17	/**
18	 * Stores the translated strings for the full weekday names.
19	 *
20	 * @since 2.1.0
21	 * @var array
22	 */
23	public $weekday;
24
25	/**
26	 * Stores the translated strings for the one character weekday names.
27	 *
28	 * There is a hack to make sure that Tuesday and Thursday, as well
29	 * as Sunday and Saturday, don't conflict. See init() method for more.
30	 *
31	 * @see WP_Locale::init() for how to handle the hack.
32	 *
33	 * @since 2.1.0
34	 * @var array
35	 */
36	public $weekday_initial;
37
38	/**
39	 * Stores the translated strings for the abbreviated weekday names.
40	 *
41	 * @since 2.1.0
42	 * @var array
43	 */
44	public $weekday_abbrev;
45
46	/**
47	 * Stores the translated strings for the full month names.
48	 *
49	 * @since 2.1.0
50	 * @var array
51	 */
52	public $month;
53
54	/**
55	 * Stores the translated strings for the month names in genitive case, if the locale specifies.
56	 *
57	 * @since 4.4.0
58	 * @var array
59	 */
60	public $month_genitive;
61
62	/**
63	 * Stores the translated strings for the abbreviated month names.
64	 *
65	 * @since 2.1.0
66	 * @var array
67	 */
68	public $month_abbrev;
69
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	/**
81	 * The text direction of the locale language.
82	 *
83	 * Default is left to right 'ltr'.
84	 *
85	 * @since 2.1.0
86	 * @var string
87	 */
88	public $text_direction = 'ltr';
89
90	/**
91	 * The thousands separator and decimal point values used for localizing numbers.
92	 *
93	 * @since 2.3.0
94	 * @var array
95	 */
96	public $number_format;
97
98	/**
99	 * Constructor which calls helper methods to set up object variables.
100	 *
101	 * @since 2.1.0
102	 */
103	public function __construct() {
104		$this->init();
105		$this->register_globals();
106	}
107
108	/**
109	 * Sets up the translated strings and object properties.
110	 *
111	 * The method creates the translatable strings for various
112	 * calendar elements. Which allows for specifying locale
113	 * specific calendar names and text direction.
114	 *
115	 * @since 2.1.0
116	 *
117	 * @global string $text_direction
118	 * @global string $wp_version     The WordPress version string.
119	 */
120	public function init() {
121		// The weekdays.
122		$this->weekday[0] = /* translators: Weekday. */ __( 'Sunday' );
123		$this->weekday[1] = /* translators: Weekday. */ __( 'Monday' );
124		$this->weekday[2] = /* translators: Weekday. */ __( 'Tuesday' );
125		$this->weekday[3] = /* translators: Weekday. */ __( 'Wednesday' );
126		$this->weekday[4] = /* translators: Weekday. */ __( 'Thursday' );
127		$this->weekday[5] = /* translators: Weekday. */ __( 'Friday' );
128		$this->weekday[6] = /* translators: Weekday. */ __( 'Saturday' );
129
130		// The first letter of each day.
131		$this->weekday_initial[ __( 'Sunday' ) ]    = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Sunday initial' );
132		$this->weekday_initial[ __( 'Monday' ) ]    = /* translators: One-letter abbreviation of the weekday. */ _x( 'M', 'Monday initial' );
133		$this->weekday_initial[ __( 'Tuesday' ) ]   = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Tuesday initial' );
134		$this->weekday_initial[ __( 'Wednesday' ) ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'W', 'Wednesday initial' );
135		$this->weekday_initial[ __( 'Thursday' ) ]  = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Thursday initial' );
136		$this->weekday_initial[ __( 'Friday' ) ]    = /* translators: One-letter abbreviation of the weekday. */ _x( 'F', 'Friday initial' );
137		$this->weekday_initial[ __( 'Saturday' ) ]  = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Saturday initial' );
138
139		// Abbreviations for each day.
140		$this->weekday_abbrev[ __( 'Sunday' ) ]    = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sun' );
141		$this->weekday_abbrev[ __( 'Monday' ) ]    = /* translators: Ttree-letter abbreviation of the weekday. */ __( 'Mon' );
142		$this->weekday_abbrev[ __( 'Tuesday' ) ]   = /* translators: Three-letter abbreviation of the weekday. */ __( 'Tue' );
143		$this->weekday_abbrev[ __( 'Wednesday' ) ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Wed' );
144		$this->weekday_abbrev[ __( 'Thursday' ) ]  = /* translators: Three-letter abbreviation of the weekday. */ __( 'Thu' );
145		$this->weekday_abbrev[ __( 'Friday' ) ]    = /* translators: Three-letter abbreviation of the weekday. */ __( 'Fri' );
146		$this->weekday_abbrev[ __( 'Saturday' ) ]  = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sat' );
147
148		// The months.
149		$this->month['01'] = /* translators: Month name. */ __( 'January' );
150		$this->month['02'] = /* translators: Month name. */ __( 'February' );
151		$this->month['03'] = /* translators: Month name. */ __( 'March' );
152		$this->month['04'] = /* translators: Month name. */ __( 'April' );
153		$this->month['05'] = /* translators: Month name. */ __( 'May' );
154		$this->month['06'] = /* translators: Month name. */ __( 'June' );
155		$this->month['07'] = /* translators: Month name. */ __( 'July' );
156		$this->month['08'] = /* translators: Month name. */ __( 'August' );
157		$this->month['09'] = /* translators: Month name. */ __( 'September' );
158		$this->month['10'] = /* translators: Month name. */ __( 'October' );
159		$this->month['11'] = /* translators: Month name. */ __( 'November' );
160		$this->month['12'] = /* translators: Month name. */ __( 'December' );
161
162		// The months, genitive.
163		$this->month_genitive['01'] = /* translators: Month name, genitive. */ _x( 'January', 'genitive' );
164		$this->month_genitive['02'] = /* translators: Month name, genitive. */ _x( 'February', 'genitive' );
165		$this->month_genitive['03'] = /* translators: Month name, genitive. */ _x( 'March', 'genitive' );
166		$this->month_genitive['04'] = /* translators: Month name, genitive. */ _x( 'April', 'genitive' );
167		$this->month_genitive['05'] = /* translators: Month name, genitive. */ _x( 'May', 'genitive' );
168		$this->month_genitive['06'] = /* translators: Month name, genitive. */ _x( 'June', 'genitive' );
169		$this->month_genitive['07'] = /* translators: Month name, genitive. */ _x( 'July', 'genitive' );
170		$this->month_genitive['08'] = /* translators: Month name, genitive. */ _x( 'August', 'genitive' );
171		$this->month_genitive['09'] = /* translators: Month name, genitive. */ _x( 'September', 'genitive' );
172		$this->month_genitive['10'] = /* translators: Month name, genitive. */ _x( 'October', 'genitive' );
173		$this->month_genitive['11'] = /* translators: Month name, genitive. */ _x( 'November', 'genitive' );
174		$this->month_genitive['12'] = /* translators: Month name, genitive. */ _x( 'December', 'genitive' );
175
176		// Abbreviations for each month.
177		$this->month_abbrev[ __( 'January' ) ]   = /* translators: Three-letter abbreviation of the month. */ _x( 'Jan', 'January abbreviation' );
178		$this->month_abbrev[ __( 'February' ) ]  = /* translators: Three-letter abbreviation of the month. */ _x( 'Feb', 'February abbreviation' );
179		$this->month_abbrev[ __( 'March' ) ]     = /* translators: Three-letter abbreviation of the month. */ _x( 'Mar', 'March abbreviation' );
180		$this->month_abbrev[ __( 'April' ) ]     = /* translators: Three-letter abbreviation of the month. */ _x( 'Apr', 'April abbreviation' );
181		$this->month_abbrev[ __( 'May' ) ]       = /* translators: Three-letter abbreviation of the month. */ _x( 'May', 'May abbreviation' );
182		$this->month_abbrev[ __( 'June' ) ]      = /* translators: Three-letter abbreviation of the month. */ _x( 'Jun', 'June abbreviation' );
183		$this->month_abbrev[ __( 'July' ) ]      = /* translators: Three-letter abbreviation of the month. */ _x( 'Jul', 'July abbreviation' );
184		$this->month_abbrev[ __( 'August' ) ]    = /* translators: Three-letter abbreviation of the month. */ _x( 'Aug', 'August abbreviation' );
185		$this->month_abbrev[ __( 'September' ) ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Sep', 'September abbreviation' );
186		$this->month_abbrev[ __( 'October' ) ]   = /* translators: Three-letter abbreviation of the month. */ _x( 'Oct', 'October abbreviation' );
187		$this->month_abbrev[ __( 'November' ) ]  = /* translators: Three-letter abbreviation of the month. */ _x( 'Nov', 'November abbreviation' );
188		$this->month_abbrev[ __( 'December' ) ]  = /* translators: Three-letter abbreviation of the month. */ _x( 'Dec', 'December abbreviation' );
189
190		// The meridiems.
191		$this->meridiem['am'] = __( 'am' );
192		$this->meridiem['pm'] = __( 'pm' );
193		$this->meridiem['AM'] = __( 'AM' );
194		$this->meridiem['PM'] = __( 'PM' );
195
196		// Numbers formatting.
197		// See https://www.php.net/number_format
198
199		/* translators: $thousands_sep argument for https://www.php.net/number_format, default is ',' */
200		$thousands_sep = __( 'number_format_thousands_sep' );
201
202		// Replace space with a non-breaking space to avoid wrapping.
203		$thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
204
205		$this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
206
207		/* translators: $dec_point argument for https://www.php.net/number_format, default is '.' */
208		$decimal_point = __( 'number_format_decimal_point' );
209
210		$this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
211
212		// Set text direction.
213		if ( isset( $GLOBALS['text_direction'] ) ) {
214			$this->text_direction = $GLOBALS['text_direction'];
215
216			/* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
217		} elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) {
218			$this->text_direction = 'rtl';
219		}
220	}
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	 *
231	 * @param int $weekday_number 0 for Sunday through 6 Saturday.
232	 * @return string Full translated weekday.
233	 */
234	public function get_weekday( $weekday_number ) {
235		return $this->weekday[ $weekday_number ];
236	}
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	 *
248	 * @param string $weekday_name Full translated weekday word.
249	 * @return string Translated weekday initial.
250	 */
251	public function get_weekday_initial( $weekday_name ) {
252		return $this->weekday_initial[ $weekday_name ];
253	}
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	 *
263	 * @param string $weekday_name Full translated weekday word.
264	 * @return string Translated weekday abbreviation.
265	 */
266	public function get_weekday_abbrev( $weekday_name ) {
267		return $this->weekday_abbrev[ $weekday_name ];
268	}
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	 *
283	 * @param string|int $month_number '01' through '12'.
284	 * @return string Translated full month name.
285	 */
286	public function get_month( $month_number ) {
287		return $this->month[ zeroise( $month_number, 2 ) ];
288	}
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	 *
298	 * @param string $month_name Translated month to get abbreviated version.
299	 * @return string Translated abbreviated month.
300	 */
301	public function get_month_abbrev( $month_name ) {
302		return $this->month_abbrev[ $month_name ];
303	}
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	 *
312	 * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
313	 * @return string Translated version
314	 */
315	public function get_meridiem( $meridiem ) {
316		return $this->meridiem[ $meridiem ];
317	}
318
319	/**
320	 * Global variables are deprecated.
321	 *
322	 * For backward compatibility only.
323	 *
324	 * @deprecated For backward compatibility only.
325	 *
326	 * @global array $weekday
327	 * @global array $weekday_initial
328	 * @global array $weekday_abbrev
329	 * @global array $month
330	 * @global array $month_abbrev
331	 *
332	 * @since 2.1.0
333	 */
334	public function register_globals() {
335		$GLOBALS['weekday']         = $this->weekday;
336		$GLOBALS['weekday_initial'] = $this->weekday_initial;
337		$GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
338		$GLOBALS['month']           = $this->month;
339		$GLOBALS['month_abbrev']    = $this->month_abbrev;
340	}
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	/**
353	 * Register date/time format strings for general POT.
354	 *
355	 * Private, unused method to add some date/time formats translated
356	 * on wp-admin/options-general.php to the general POT that would
357	 * otherwise be added to the admin POT.
358	 *
359	 * @since 3.6.0
360	 */
361	public function _strings_for_pot() {
362		/* translators: Localized date format, see https://www.php.net/manual/datetime.format.php */
363		__( 'F j, Y' );
364		/* translators: Localized time format, see https://www.php.net/manual/datetime.format.php */
365		__( 'g:i a' );
366		/* translators: Localized date and time format, see https://www.php.net/manual/datetime.format.php */
367		__( 'F j, Y g:i a' );
368	}
369}
370