3 * Locale API: WP_Locale class
11 * Core class used to store translated data for a locale.
14 * @since 4.6.0 Moved to its own file from wp-includes/locale.php.
18 * Stores the translated strings for the full weekday names.
26 * Stores the translated strings for the one character weekday names.
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.
31 * @see WP_Locale::init() for how to handle the hack.
36 public $weekday_initial;
39 * Stores the translated strings for the abbreviated weekday names.
44 public $weekday_abbrev;
47 * Stores the default start of the week.
52 public $start_of_week;
55 * Stores the translated strings for the full month names.
63 * Stores the translated strings for the abbreviated month names.
71 * Stores the translated strings for 'am' and 'pm'.
73 * Also the capitalized versions.
81 * The text direction of the locale language.
83 * Default is left to right 'ltr'.
88 public $text_direction = 'ltr';
91 * The thousands separator and decimal point values used for localizing numbers.
97 public $number_format;
100 * Constructor which calls helper methods to set up object variables.
104 public function __construct() {
106 $this->register_globals();
110 * Sets up the translated strings and object properties.
112 * The method creates the translatable strings for various
113 * calendar elements. Which allows for specifying locale
114 * specific calendar names and text direction.
119 * @global string $text_direction
120 * @global string $wp_version
122 public function init() {
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');
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' );
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');
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' );
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' );
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' );
193 $this->meridiem['am'] = __('am');
194 $this->meridiem['pm'] = __('pm');
195 $this->meridiem['AM'] = __('AM');
196 $this->meridiem['PM'] = __('PM');
198 // Numbers formatting
199 // See https://secure.php.net/number_format
201 /* translators: $thousands_sep argument for https://secure.php.net/number_format, default is , */
202 $thousands_sep = __( 'number_format_thousands_sep' );
204 if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
205 // Replace space with a non-breaking space to avoid wrapping.
206 $thousands_sep = str_replace( ' ', ' ', $thousands_sep );
208 // PHP < 5.4.0 does not support multiple bytes in thousands separator.
209 $thousands_sep = str_replace( array( ' ', ' ' ), ' ', $thousands_sep );
212 $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
214 /* translators: $dec_point argument for https://secure.php.net/number_format, default is . */
215 $decimal_point = __( 'number_format_decimal_point' );
217 $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
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';
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' ) );
233 * Outputs an admin notice if the /build directory must be used for RTL.
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>';
244 * Retrieve the full translated weekday word.
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).
253 * @param int $weekday_number 0 for Sunday through 6 Saturday
254 * @return string Full translated weekday
256 public function get_weekday($weekday_number) {
257 return $this->weekday[$weekday_number];
261 * Retrieve the translated weekday initial.
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
271 * @param string $weekday_name
274 public function get_weekday_initial($weekday_name) {
275 return $this->weekday_initial[$weekday_name];
279 * Retrieve the translated weekday abbreviation.
281 * The weekday abbreviation is retrieved by the translated
287 * @param string $weekday_name Full translated weekday word
288 * @return string Translated weekday abbreviation
290 public function get_weekday_abbrev($weekday_name) {
291 return $this->weekday_abbrev[$weekday_name];
295 * Retrieve the full translated month by month number.
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
302 * You can use an integer instead and it will add the
303 * '0' before the numbers less than 10 for you.
308 * @param string|int $month_number '01' through '12'
309 * @return string Translated full month name
311 public function get_month($month_number) {
312 return $this->month[zeroise($month_number, 2)];
316 * Retrieve translated version of month abbreviation string.
318 * The $month_name parameter is expected to be the translated or
319 * translatable version of the month.
324 * @param string $month_name Translated month to get abbreviated version
325 * @return string Translated abbreviated month
327 public function get_month_abbrev($month_name) {
328 return $this->month_abbrev[$month_name];
332 * Retrieve translated version of meridiem string.
334 * The $meridiem parameter is expected to not be translated.
339 * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
340 * @return string Translated version
342 public function get_meridiem($meridiem) {
343 return $this->meridiem[$meridiem];
347 * Global variables are deprecated.
349 * For backward compatibility only.
351 * @deprecated For backward compatibility only.
354 * @global array $weekday
355 * @global array $weekday_initial
356 * @global array $weekday_abbrev
357 * @global array $month
358 * @global array $month_abbrev
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;
371 * Checks if current locale is RTL.
374 * @return bool Whether locale is RTL.
376 public function is_rtl() {
377 return 'rtl' == $this->text_direction;
381 * Register date/time format strings for general POT.
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.
389 public function _strings_for_pot() {
390 /* translators: localized date format, see https://secure.php.net/date */
392 /* translators: localized time format, see https://secure.php.net/date */
394 /* translators: localized date and time format, see https://secure.php.net/date */
395 __( 'F j, Y g:i a' );