WordPress build repository browser

source: branches/6.6/wp-includes/class-wp-locale.php

Last change on this file was 57663, checked in by SergeyBiryukov, 2 months ago

Docs: Correct the placement of @global tags in various files.

Follow-up to [6589], [8598], [10798], [25490], [25594], [31192], [31194], [35718], [37437], [44169], [44948], [45247], [56763], [56773], [57370], [57503].

Props shailu25.
Fixes #61295.
Built from https://develop.svn.wordpress.org/trunk@58200

  • Property svn:eol-style set to native
File size: 15.7 KB
Line 
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( ' ', '&nbsp;', $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.
319         */
320        public function get_month( $month_number ) {
321                return $this->month[ zeroise( $month_number, 2 ) ];
322        }
323
324        /**
325         * Retrieves translated version of month abbreviation string.
326         *
327         * The $month_name parameter is expected to be the translated or
328         * translatable version of the month.
329         *
330         * @since 2.1.0
331         *
332         * @param string $month_name Translated month to get abbreviated version.
333         * @return string Translated abbreviated month.
334         */
335        public function get_month_abbrev( $month_name ) {
336                return $this->month_abbrev[ $month_name ];
337        }
338
339        /**
340         * Retrieves translated version of meridiem string.
341         *
342         * The $meridiem parameter is expected to not be translated.
343         *
344         * @since 2.1.0
345         *
346         * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
347         * @return string Translated version
348         */
349        public function get_meridiem( $meridiem ) {
350                return $this->meridiem[ $meridiem ];
351        }
352
353        /**
354         * Global variables are deprecated.
355         *
356         * For backward compatibility only.
357         *
358         * @since 2.1.0
359         * @deprecated For backward compatibility only.
360         *
361         * @global array $weekday
362         * @global array $weekday_initial
363         * @global array $weekday_abbrev
364         * @global array $month
365         * @global array $month_abbrev
366         */
367        public function register_globals() {
368                $GLOBALS['weekday']         = $this->weekday;
369                $GLOBALS['weekday_initial'] = $this->weekday_initial;
370                $GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
371                $GLOBALS['month']           = $this->month;
372                $GLOBALS['month_abbrev']    = $this->month_abbrev;
373        }
374
375        /**
376         * Checks if current locale is RTL.
377         *
378         * @since 3.0.0
379         * @return bool Whether locale is RTL.
380         */
381        public function is_rtl() {
382                return 'rtl' === $this->text_direction;
383        }
384
385        /**
386         * Registers date/time format strings for general POT.
387         *
388         * Private, unused method to add some date/time formats translated
389         * on wp-admin/options-general.php to the general POT that would
390         * otherwise be added to the admin POT.
391         *
392         * @since 3.6.0
393         */
394        public function _strings_for_pot() {
395                /* translators: Localized date format, see https://www.php.net/manual/datetime.format.php */
396                __( 'F j, Y' );
397                /* translators: Localized time format, see https://www.php.net/manual/datetime.format.php */
398                __( 'g:i a' );
399                /* translators: Localized date and time format, see https://www.php.net/manual/datetime.format.php */
400                __( 'F j, Y g:i a' );
401        }
402
403        /**
404         * Retrieves the localized list item separator.
405         *
406         * @since 6.0.0
407         *
408         * @return string Localized list item separator.
409         */
410        public function get_list_item_separator() {
411                return $this->list_item_separator;
412        }
413
414        /**
415         * Retrieves the localized word count type.
416         *
417         * @since 6.2.0
418         *
419         * @return string Localized word count type. Possible values are `characters_excluding_spaces`,
420         *                `characters_including_spaces`, or `words`. Defaults to `words`.
421         */
422        public function get_word_count_type() {
423
424                /*
425                 * translators: If your word count is based on single characters (e.g. East Asian characters),
426                 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
427                 * Do not translate into your own language.
428                 */
429                $word_count_type = is_null( $this->word_count_type ) ? _x( 'words', 'Word count type. Do not translate!' ) : $this->word_count_type;
430
431                // Check for valid types.
432                if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) {
433                        // Defaults to 'words'.
434                        $word_count_type = 'words';
435                }
436
437                return $word_count_type;
438        }
439}
Note: See TracBrowser for help on using the repository browser.