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