~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/class-wp-locale.php

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

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