WP_Locale Class Reference
Stores translated locale-specific data including weekdays, months, date formats, and text direction.
Stores translated locale-specific data including weekdays, months, date formats, and text direction.
Overview
Stores translated locale-specific data including weekdays, months, date formats, and text direction.
global $wp_locale;
echo $wp_locale->get_weekday( 0 ); // "Sunday" or translated equivalentProperties
Weekday Names
/**
* @var string[] Full weekday names, indexed 0-6 (Sunday-Saturday)
*/
public $weekday = [];
/**
* @var string[] Single-letter weekday initials, keyed by full name
*/
public $weekday_initial = [];
/**
* @var string[] Three-letter weekday abbreviations, keyed by full name
*/
public $weekday_abbrev = [];Month Names
/**
* @var string[] Full month names, indexed '01'-'12'
*/
public $month = [];
/**
* @var string[] Genitive month names (for languages that need them)
* @since 4.4.0
*/
public $month_genitive = [];
/**
* @var string[] Three-letter month abbreviations, keyed by full name
*/
public $month_abbrev = [];Time & Text
/**
* @var string[] Meridiem strings: 'am', 'pm', 'AM', 'PM'
*/
public $meridiem = [];
/**
* @var string Text direction: 'ltr' or 'rtl'
*/
public $text_direction = 'ltr';
/**
* @var string List item separator (e.g., ", " in English)
* @since 6.0.0
*/
public $list_item_separator;
/**
* @var string Word count type: 'words', 'characters_excluding_spaces', 'characters_including_spaces'
* @since 6.2.0
*/
public $word_count_type;Number Formatting
/**
* @var array {
* @type string $thousands_sep Thousands separator (e.g., "," or ".")
* @type string $decimal_point Decimal point (e.g., "." or ",")
* }
*/
public $number_format = [];Methods
Constructor
public function __construct()File: wp-includes/class-wp-locale.php
Since: WordPress 2.1.0
Global: Available as $wp_locale
init()
WP_Locale is instantiated once per request and stored in the global $wp_locale. It provides locale-aware data for date formatting, number formatting, and text direction.
public function init()Calls init() to set up translated strings and register_globals() for backward compatibility.
$this->weekday[0] = __( 'Sunday' );
$this->month['01'] = __( 'January' );
$this->meridiem['am'] = __( 'am' );Weekday Methods
get_weekday( $weekday_number )
Sets up all translated strings for weekdays, months, meridiems, number formats, and text direction.
public function get_weekday( $weekday_number ): stringInternally calls translation functions like:
$weekday_number(int) – 0 for Sunday through 6 for Saturday
Returns full translated weekday name.
global $wp_locale;
echo $wp_locale->get_weekday( 1 ); // "Monday" or "Lundi" etc.get_weekday_initial( $weekday_name )
Parameters:
public function get_weekday_initial( $weekday_name ): stringExample:
$weekday_name(string) – Full translated weekday name
Returns single-letter weekday initial.
global $wp_locale;
$weekday = $wp_locale->get_weekday( 2 );
echo $wp_locale->get_weekday_initial( $weekday ); // "T" for TuesdayParameters:
get_weekday_abbrev( $weekday_name )
Example:
public function get_weekday_abbrev( $weekday_name ): stringNote: Uses context to differentiate conflicting initials (Tuesday/Thursday, Sunday/Saturday).
global $wp_locale;
$weekday = $wp_locale->get_weekday( 3 );
echo $wp_locale->get_weekday_abbrev( $weekday ); // "Wed"Month Methods
get_month( $month_number )
Returns three-letter weekday abbreviation.
public function get_month( $month_number ): stringExample:
$month_number(string|int) – ’01’ through ’12’ (or 1-12, auto-padded)
Returns full translated month name.
global $wp_locale;
echo $wp_locale->get_month( 3 ); // "March"
echo $wp_locale->get_month( '03' ); // "March"Parameters:
get_month_abbrev( $month_name )
Example:
public function get_month_abbrev( $month_name ): stringReturns: Empty string if month number is invalid.
global $wp_locale;
$month = $wp_locale->get_month( 12 );
echo $wp_locale->get_month_abbrev( $month ); // "Dec"get_month_genitive( $month_number )
Returns three-letter month abbreviation.
public function get_month_genitive( $month_number ): stringExample:
Returns genitive form of month name (for grammatical cases).
global $wp_locale;
echo $wp_locale->get_month_genitive( 1 ); // "January" (genitive form)Since: WordPress 6.8.0
Meridiem & Text Direction
get_meridiem( $meridiem )
Example:
public function get_meridiem( $meridiem ): stringNote: In many languages, month names change form based on grammatical context.
$meridiem(string) – ‘am’, ‘pm’, ‘AM’, or ‘PM’
Returns translated meridiem string.
global $wp_locale;
echo $wp_locale->get_meridiem( 'AM' ); // "AM" or locale equivalentis_rtl()
Parameters:
public function is_rtl(): boolExample:
global $wp_locale;
if ( $wp_locale->is_rtl() ) {
echo 'Right-to-left language';
}Checks if the current locale is right-to-left.
List & Word Count
get_list_item_separator()
Example:
public function get_list_item_separator(): stringSee also: is_rtl() global function.
Returns the locale-appropriate list separator.
global $wp_locale;
$separator = $wp_locale->get_list_item_separator();
echo implode( $separator, ['one', 'two', 'three'] );
// English: "one, two, three"
// Some languages may use different separatorsget_word_count_type()
Since: WordPress 6.0.0
public function get_word_count_type(): stringExample:
Returns how words should be counted for this locale.
'words'– Count space-separated words (most languages)'characters_excluding_spaces'– Count characters without spaces (CJK languages)'characters_including_spaces'– Count all characters
Since: WordPress 6.2.0
global $wp_locale;
$type = $wp_locale->get_word_count_type();
// Japanese/Chinese: 'characters_excluding_spaces'
// English: 'words'Backward Compatibility
register_globals()
Returns: One of:
public function register_globals()Example:
$weekday$weekday_initial$weekday_abbrev$month$month_abbrev
Usage Examples
Formatting a Date
global $wp_locale;
$weekday_num = date( 'w' ); // 0-6
$month_num = date( 'm' ); // 01-12
$day = date( 'd' );
$year = date( 'Y' );
$weekday = $wp_locale->get_weekday( $weekday_num );
$month = $wp_locale->get_month( $month_num );
echo "$weekday, $month $day, $year";
// "Wednesday, February 14, 2024"Number Formatting
global $wp_locale;
$number = 1234567.89;
$formatted = number_format(
$number,
2,
$wp_locale->number_format['decimal_point'],
$wp_locale->number_format['thousands_sep']
);
// English: "1,234,567.89"
// German: "1.234.567,89"Building a Month Selector
global $wp_locale;
echo '<select name="month">';
for ( $i = 1; $i <= 12; $i++ ) {
$month_num = zeroise( $i, 2 );
$month_name = $wp_locale->get_month( $month_num );
echo "<option value="$month_num">$month_name</option>";
}
echo '</select>';RTL-Aware Layout
global $wp_locale;
$float_dir = $wp_locale->is_rtl() ? 'right' : 'left';
echo "<div style="float: $float_dir;">Content</div>";Related Classes
- WP_Locale_Switcher – Manages locale switching
- WP_Translation_Controller – Manages translation loading
Hooks
Registers locale data as global variables (deprecated, for backward compatibility).
Translatable Strings
Sets globals:
// Weekdays with context for disambiguation
_x( 'S', 'Sunday initial' );
_x( 'S', 'Saturday initial' );
// Month abbreviations with context
_x( 'Jan', 'January abbreviation' );
// Number format (special handling)
__( 'number_format_thousands_sep' ); // Returns ',' if untranslated
__( 'number_format_decimal_point' ); // Returns '.' if untranslated
// Text direction
_x( 'ltr', 'text direction' ); // Return 'rtl' for RTL languages