php8.5
Home/ Manual/ reference / intl/ The Locale class

The Locale class

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.

Intro

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. Use Locale::getDefault and Locale::setDefault to read and write the default locale used by ICU.

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

class Locale { }

Locale-constants

See Also

  • RFC 4646 - Tags for Identifying Languages
  • RFC 4647 - Matching of Language Tags
  • Unicode CLDR Project:Common Locale Data Repository
  • IANA Language Subtags Registry
  • ICU User Guide - Locale
  • ICU Locale api

Changelog

VersionDescription
8.5.0Locale methods now throw a ValueError when a locale argument contains null bytes.
8.4.0The class constants are now typed.

Locale

In this section

Locale::acceptFromHttp

Tries to find out best available locale based on HTTP "Accept-Language" header

Locale::addLikelySubtags

Add likely subtags to a locale

Locale::canonicalize

Canonicalize the locale string

Locale::composeLocale

Returns a correctly ordered and delimited locale ID

Locale::filterMatches

Checks if a language tag filter matches with locale

Locale::getAllVariants

Gets the variants for the input locale

Locale::getDefault

Gets the default locale value from the INTL global 'default_locale'

Locale::getDisplayLanguage

Returns an appropriately localized display name for language of the inputlocale

Locale::getDisplayName

Returns an appropriately localized display name for the input locale

Locale::getDisplayRegion

Returns an appropriately localized display name for region of the input locale

Locale::getDisplayScript

Returns an appropriately localized display name for script of the input locale

Locale::getDisplayVariant

Returns an appropriately localized display name for variants of the input locale

Locale::getKeywords

Gets the keywords for the input locale

Locale::getPrimaryLanguage

Gets the primary language for the input locale

Locale::getRegion

Gets the region for the input locale

Locale::getScript

Gets the script for the input locale

Locale::isRightToLeft

Check whether a locale uses a right-to-left writing system

Locale::lookup

Searches the language tag list for the best match to the language

Locale::minimizeSubtags

Remove likely subtags from a locale

Locale::parseLocale

Returns a key-value array of locale ID subtag elements

Locale::setDefault

Sets the default runtime locale

Source: reference/intl/locale.xml · from the official PHP manual (php/doc-en)