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