The Locale class

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

Introduction

A "Locale" is an identifier used to get language, culture, or regionally-specific behavior from an API. PHP locales are organized and identified the same way that the CLDR locales used by ICU (and many vendors of Unix-like operating systems, the Mac, Java, and so forth) use. Locales are identified using RFC 4646 language tags (which use hyphen, not underscore) in addition to the more traditional underscore-using identifiers. Unless otherwise noted the functions in this class are tolerant of both formats.

Examples of identifiers include:

  • en-US (English, United States)
  • zh-Hant-TW (Chinese, Traditional Script, Taiwan)
  • fr-CA, fr-FR (French for Canada and France respectively)

The Locale class (and related procedural functions) are used to interact with locale identifiers--to verify that an ID is well-formed, valid, etc. The extensions used by CLDR in UAX #35 (and inherited by ICU) are valid and used wherever they would be in ICU normally.

Locales cannot be instantiated as objects. All of the functions/methods provided are static.

The null or empty string obtains the "root" locale. The "root" locale is equivalent to "en_US_POSIX" in CLDR. Language tags (and thus locale identifiers) are case insensitive. There exists a canonicalization function to make case match the specification.

Class synopsis

classLocale {
publicconstintACTUAL_LOCALE;
publicconstintVALID_LOCALE;
publicconstnullDEFAULT_LOCALE = null;
publicconststringLANG_TAG;
publicconststringEXTLANG_TAG;
publicconststringSCRIPT_TAG;
publicconststringREGION_TAG;
publicconststringVARIANT_TAG;
publicconststringPRIVATE_TAG;
publicstaticacceptFromHttp(string$header): string|false
publicstaticcanonicalize(string$locale): ?string
publicstaticcomposeLocale(array$subtags): string|false
publicstaticfilterMatches(string$languageTag, string$locale, bool$canonicalize = false): ?bool
publicstaticgetAllVariants(string$locale): ?array
publicstaticgetDefault(): string
publicstaticgetDisplayLanguage(string$locale, ?string$displayLocale = null): string|false
publicstaticgetDisplayName(string$locale, ?string$displayLocale = null): string|false
publicstaticgetDisplayRegion(string$locale, ?string$displayLocale = null): string|false
publicstaticgetDisplayScript(string$locale, ?string$displayLocale = null): string|false
publicstaticgetDisplayVariant(string$locale, ?string$displayLocale = null): string|false
publicstaticgetKeywords(string$locale): array|false|null
publicstaticgetPrimaryLanguage(string$locale): ?string
publicstaticgetRegion(string$locale): ?string
publicstaticgetScript(string$locale): ?string
publicstaticlookup(
    array$languageTag,
    string$locale,
    bool$canonicalize = false,
    ?string$defaultLocale = null
): ?string
publicstaticparseLocale(string$locale): ?array
publicstaticsetDefault(string$locale): bool
}

Predefined Constants

Locale::DEFAULT_LOCALE
Used as locale parameter with the methods of the various locale affected classes, such as NumberFormatter. This constant would make the methods to use default locale.

These constants describe the choice of the locale for the getLocale method of different classes.

Locale::ACTUAL_LOCALE
This is locale the data actually comes from.
Locale::VALID_LOCALE
This is the most specific locale supported by ICU.

These constants define how the Locales are parsed or composed. They should be used as keys in the argument array to locale_compose() and are returned from locale_parse() as keys of the returned associative array.

Locale::LANG_TAG
Language subtag
Locale::EXTLANG_TAG
Extended language subtag
Locale::SCRIPT_TAG
Script subtag
Locale::REGION_TAG
Region subtag
Locale::VARIANT_TAG
Variant subtag
Locale::GRANDFATHERED_LANG_TAG
Grandfathered Language subtag
Locale::PRIVATE_TAG
Private subtag

Table of Contents

To Top