[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-locale.php (source)

   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]
  17  class 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|null
 122       *
 123       * @phpstan-var 'characters_excluding_spaces'|'characters_including_spaces'|'words'|null
 124       */
 125      public $word_count_type;
 126  
 127      /**
 128       * Constructor which calls helper methods to set up object variables.
 129       *
 130       * @since 2.1.0
 131       */
 132  	public function __construct() {
 133          $this->init();
 134          $this->register_globals();
 135      }
 136  
 137      /**
 138       * Sets up the translated strings and object properties.
 139       *
 140       * The method creates the translatable strings for various
 141       * calendar elements. Which allows for specifying locale
 142       * specific calendar names and text direction.
 143       *
 144       * @since 2.1.0
 145       *
 146       * @global string $text_direction
 147       */
 148  	public function init() {
 149          // The weekdays.
 150          $this->weekday[0] = /* translators: Weekday. */ __( 'Sunday' );
 151          $this->weekday[1] = /* translators: Weekday. */ __( 'Monday' );
 152          $this->weekday[2] = /* translators: Weekday. */ __( 'Tuesday' );
 153          $this->weekday[3] = /* translators: Weekday. */ __( 'Wednesday' );
 154          $this->weekday[4] = /* translators: Weekday. */ __( 'Thursday' );
 155          $this->weekday[5] = /* translators: Weekday. */ __( 'Friday' );
 156          $this->weekday[6] = /* translators: Weekday. */ __( 'Saturday' );
 157  
 158          // The first letter of each day.
 159          $this->weekday_initial[ $this->weekday[0] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Sunday initial' );
 160          $this->weekday_initial[ $this->weekday[1] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'M', 'Monday initial' );
 161          $this->weekday_initial[ $this->weekday[2] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Tuesday initial' );
 162          $this->weekday_initial[ $this->weekday[3] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'W', 'Wednesday initial' );
 163          $this->weekday_initial[ $this->weekday[4] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Thursday initial' );
 164          $this->weekday_initial[ $this->weekday[5] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'F', 'Friday initial' );
 165          $this->weekday_initial[ $this->weekday[6] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Saturday initial' );
 166  
 167          // Abbreviations for each day.
 168          $this->weekday_abbrev[ $this->weekday[0] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sun' );
 169          $this->weekday_abbrev[ $this->weekday[1] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Mon' );
 170          $this->weekday_abbrev[ $this->weekday[2] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Tue' );
 171          $this->weekday_abbrev[ $this->weekday[3] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Wed' );
 172          $this->weekday_abbrev[ $this->weekday[4] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Thu' );
 173          $this->weekday_abbrev[ $this->weekday[5] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Fri' );
 174          $this->weekday_abbrev[ $this->weekday[6] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sat' );
 175  
 176          // The months.
 177          $this->month['01'] = /* translators: Month name. */ __( 'January' );
 178          $this->month['02'] = /* translators: Month name. */ __( 'February' );
 179          $this->month['03'] = /* translators: Month name. */ __( 'March' );
 180          $this->month['04'] = /* translators: Month name. */ __( 'April' );
 181          $this->month['05'] = /* translators: Month name. */ __( 'May' );
 182          $this->month['06'] = /* translators: Month name. */ __( 'June' );
 183          $this->month['07'] = /* translators: Month name. */ __( 'July' );
 184          $this->month['08'] = /* translators: Month name. */ __( 'August' );
 185          $this->month['09'] = /* translators: Month name. */ __( 'September' );
 186          $this->month['10'] = /* translators: Month name. */ __( 'October' );
 187          $this->month['11'] = /* translators: Month name. */ __( 'November' );
 188          $this->month['12'] = /* translators: Month name. */ __( 'December' );
 189  
 190          // The months, genitive.
 191          $this->month_genitive['01'] = /* translators: Month name, genitive. */ _x( 'January', 'genitive' );
 192          $this->month_genitive['02'] = /* translators: Month name, genitive. */ _x( 'February', 'genitive' );
 193          $this->month_genitive['03'] = /* translators: Month name, genitive. */ _x( 'March', 'genitive' );
 194          $this->month_genitive['04'] = /* translators: Month name, genitive. */ _x( 'April', 'genitive' );
 195          $this->month_genitive['05'] = /* translators: Month name, genitive. */ _x( 'May', 'genitive' );
 196          $this->month_genitive['06'] = /* translators: Month name, genitive. */ _x( 'June', 'genitive' );
 197          $this->month_genitive['07'] = /* translators: Month name, genitive. */ _x( 'July', 'genitive' );
 198          $this->month_genitive['08'] = /* translators: Month name, genitive. */ _x( 'August', 'genitive' );
 199          $this->month_genitive['09'] = /* translators: Month name, genitive. */ _x( 'September', 'genitive' );
 200          $this->month_genitive['10'] = /* translators: Month name, genitive. */ _x( 'October', 'genitive' );
 201          $this->month_genitive['11'] = /* translators: Month name, genitive. */ _x( 'November', 'genitive' );
 202          $this->month_genitive['12'] = /* translators: Month name, genitive. */ _x( 'December', 'genitive' );
 203  
 204          // Abbreviations for each month.
 205          $this->month_abbrev[ $this->month['01'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jan', 'January abbreviation' );
 206          $this->month_abbrev[ $this->month['02'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Feb', 'February abbreviation' );
 207          $this->month_abbrev[ $this->month['03'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Mar', 'March abbreviation' );
 208          $this->month_abbrev[ $this->month['04'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Apr', 'April abbreviation' );
 209          $this->month_abbrev[ $this->month['05'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'May', 'May abbreviation' );
 210          $this->month_abbrev[ $this->month['06'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jun', 'June abbreviation' );
 211          $this->month_abbrev[ $this->month['07'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jul', 'July abbreviation' );
 212          $this->month_abbrev[ $this->month['08'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Aug', 'August abbreviation' );
 213          $this->month_abbrev[ $this->month['09'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Sep', 'September abbreviation' );
 214          $this->month_abbrev[ $this->month['10'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Oct', 'October abbreviation' );
 215          $this->month_abbrev[ $this->month['11'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Nov', 'November abbreviation' );
 216          $this->month_abbrev[ $this->month['12'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Dec', 'December abbreviation' );
 217  
 218          // The meridiems.
 219          $this->meridiem['am'] = __( 'am' );
 220          $this->meridiem['pm'] = __( 'pm' );
 221          $this->meridiem['AM'] = __( 'AM' );
 222          $this->meridiem['PM'] = __( 'PM' );
 223  
 224          /*
 225           * Numbers formatting.
 226           * See https://www.php.net/number_format
 227           */
 228  
 229          /* translators: $thousands_sep argument for https://www.php.net/number_format, default is ',' */
 230          $thousands_sep = __( 'number_format_thousands_sep' );
 231  
 232          // Replace space with a non-breaking space to avoid wrapping.
 233          $thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
 234  
 235          $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
 236  
 237          /* translators: $dec_point argument for https://www.php.net/number_format, default is '.' */
 238          $decimal_point = __( 'number_format_decimal_point' );
 239  
 240          $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
 241  
 242          /* translators: Used between list items, there is a space after the comma. */
 243          $this->list_item_separator = __( ', ' );
 244  
 245          // Set text direction.
 246          if ( isset( $GLOBALS['text_direction'] ) ) {
 247              $this->text_direction = $GLOBALS['text_direction'];
 248  
 249              /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
 250          } elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) {
 251              $this->text_direction = 'rtl';
 252          }
 253  
 254          // Set the word count type.
 255          $this->word_count_type = $this->get_word_count_type();
 256      }
 257  
 258      /**
 259       * Retrieves the full translated weekday word.
 260       *
 261       * Week starts on translated Sunday and can be fetched
 262       * by using 0 (zero). So the week starts with 0 (zero)
 263       * and ends on Saturday with is fetched by using 6 (six).
 264       *
 265       * @since 2.1.0
 266       *
 267       * @param int $weekday_number 0 for Sunday through 6 Saturday.
 268       * @return string Full translated weekday.
 269       *
 270       * @phpstan-param int<0, 6> $weekday_number
 271       */
 272  	public function get_weekday( $weekday_number ) {
 273          return $this->weekday[ $weekday_number ];
 274      }
 275  
 276      /**
 277       * Retrieves the translated weekday initial.
 278       *
 279       * The weekday initial is retrieved by the translated
 280       * full weekday word. When translating the weekday initial
 281       * pay attention to make sure that the starting letter does
 282       * not conflict.
 283       *
 284       * @since 2.1.0
 285       *
 286       * @param string $weekday_name Full translated weekday word.
 287       * @return string Translated weekday initial.
 288       */
 289  	public function get_weekday_initial( $weekday_name ) {
 290          return $this->weekday_initial[ $weekday_name ];
 291      }
 292  
 293      /**
 294       * Retrieves the translated weekday abbreviation.
 295       *
 296       * The weekday abbreviation is retrieved by the translated
 297       * full weekday word.
 298       *
 299       * @since 2.1.0
 300       *
 301       * @param string $weekday_name Full translated weekday word.
 302       * @return string Translated weekday abbreviation.
 303       */
 304  	public function get_weekday_abbrev( $weekday_name ) {
 305          return $this->weekday_abbrev[ $weekday_name ];
 306      }
 307  
 308      /**
 309       * Retrieves the full translated month by month number.
 310       *
 311       * The $month_number parameter has to be a string
 312       * because it must have the '0' in front of any number
 313       * that is less than 10. Starts from '01' and ends at
 314       * '12'.
 315       *
 316       * You can use an integer instead and it will add the
 317       * '0' before the numbers less than 10 for you.
 318       *
 319       * @since 2.1.0
 320       *
 321       * @param string|int $month_number '01' through '12'.
 322       * @return string Translated full month name. If the month number is not found, an empty string is returned.
 323       */
 324  	public function get_month( $month_number ) {
 325          $month_number = zeroise( $month_number, 2 );
 326          if ( ! isset( $this->month[ $month_number ] ) ) {
 327              return '';
 328          }
 329          return $this->month[ $month_number ];
 330      }
 331  
 332      /**
 333       * Retrieves translated version of month abbreviation string.
 334       *
 335       * The $month_name parameter is expected to be the translated or
 336       * translatable version of the month.
 337       *
 338       * @since 2.1.0
 339       *
 340       * @param string $month_name Translated month to get abbreviated version.
 341       * @return string Translated abbreviated month.
 342       */
 343  	public function get_month_abbrev( $month_name ) {
 344          return $this->month_abbrev[ $month_name ];
 345      }
 346  
 347      /**
 348       * Retrieves translated version of month genitive string.
 349       *
 350       * The $month_number parameter has to be a string
 351       * because it must have the '0' in front of any number
 352       * that is less than 10. Starts from '01' and ends at
 353       * '12'.
 354       *
 355       * You can use an integer instead and it will add the
 356       * '0' before the numbers less than 10 for you.
 357       *
 358       * @since 6.8.0
 359       *
 360       * @param string|int $month_number '01' through '12'.
 361       * @return string Translated genitive month name.
 362       *
 363       * @phpstan-param int<1, 12>|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10'|'11'|'12'|'01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09' $month_number
 364       */
 365  	public function get_month_genitive( $month_number ) {
 366          return $this->month_genitive[ zeroise( $month_number, 2 ) ];
 367      }
 368  
 369      /**
 370       * Retrieves translated version of meridiem string.
 371       *
 372       * The $meridiem parameter is expected to not be translated.
 373       *
 374       * @since 2.1.0
 375       *
 376       * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
 377       * @return string Translated version
 378       *
 379       * @phpstan-param 'am'|'pm'|'AM'|'PM' $meridiem
 380       */
 381  	public function get_meridiem( $meridiem ) {
 382          return $this->meridiem[ $meridiem ];
 383      }
 384  
 385      /**
 386       * Global variables are deprecated.
 387       *
 388       * For backward compatibility only.
 389       *
 390       * @since 2.1.0
 391       * @deprecated For backward compatibility only.
 392       *
 393       * @global array $weekday
 394       * @global array $weekday_initial
 395       * @global array $weekday_abbrev
 396       * @global array $month
 397       * @global array $month_abbrev
 398       */
 399  	public function register_globals() {
 400          $GLOBALS['weekday']         = $this->weekday;
 401          $GLOBALS['weekday_initial'] = $this->weekday_initial;
 402          $GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
 403          $GLOBALS['month']           = $this->month;
 404          $GLOBALS['month_abbrev']    = $this->month_abbrev;
 405      }
 406  
 407      /**
 408       * Checks if current locale is RTL.
 409       *
 410       * @since 3.0.0
 411       * @return bool Whether locale is RTL.
 412       */
 413  	public function is_rtl() {
 414          return 'rtl' === $this->text_direction;
 415      }
 416  
 417      /**
 418       * Registers date/time format strings for general POT.
 419       *
 420       * Private, unused method to add some date/time formats translated
 421       * on wp-admin/options-general.php to the general POT that would
 422       * otherwise be added to the admin POT.
 423       *
 424       * @since 3.6.0
 425       */
 426  	public function _strings_for_pot() {
 427          /* translators: Localized date format, see https://www.php.net/manual/datetime.format.php */
 428          __( 'F j, Y' );
 429          /* translators: Localized time format, see https://www.php.net/manual/datetime.format.php */
 430          __( 'g:i a' );
 431          /* translators: Localized date and time format, see https://www.php.net/manual/datetime.format.php */
 432          __( 'F j, Y g:i a' );
 433      }
 434  
 435      /**
 436       * Retrieves the localized list item separator.
 437       *
 438       * @since 6.0.0
 439       *
 440       * @return string Localized list item separator.
 441       */
 442  	public function get_list_item_separator() {
 443          return $this->list_item_separator;
 444      }
 445  
 446      /**
 447       * Retrieves the localized word count type.
 448       *
 449       * @since 6.2.0
 450       *
 451       * @return string Localized word count type. Possible values are `characters_excluding_spaces`,
 452       *                `characters_including_spaces`, or `words`. Defaults to `words`.
 453       *
 454       * @phpstan-return 'characters_excluding_spaces'|'characters_including_spaces'|'words'
 455       */
 456  	public function get_word_count_type() {
 457  
 458          /*
 459           * translators: If your word count is based on single characters (e.g. East Asian characters),
 460           * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
 461           * Do not translate into your own language.
 462           */
 463          $word_count_type = $this->word_count_type ?? _x( 'words', 'Word count type. Do not translate!' );
 464  
 465          // Check for valid types.
 466          if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) {
 467              // Defaults to 'words'.
 468              $word_count_type = 'words';
 469          }
 470  
 471          return $word_count_type;
 472      }
 473  }


Generated : Wed Dec 2 08:20:02 2026 Cross-referenced by PHPXref