首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Locale (class)

Introduction

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

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

Locale {

/* Methods */

代码语言:javascript
复制
public static string acceptFromHttp ( string $header )
代码语言:javascript
复制
public static string canonicalize ( string $locale )
代码语言:javascript
复制
public static string composeLocale ( array $subtags )
代码语言:javascript
复制
public static bool filterMatches ( string $langtag , string $locale [, bool $canonicalize = false ] )
代码语言:javascript
复制
public static array getAllVariants ( string $locale )
代码语言:javascript
复制
public static string getDefault ( void )
代码语言:javascript
复制
public static string getDisplayLanguage ( string $locale [, string $in_locale ] )
代码语言:javascript
复制
public static string getDisplayName ( string $locale [, string $in_locale ] )
代码语言:javascript
复制
public static string getDisplayRegion ( string $locale [, string $in_locale ] )
代码语言:javascript
复制
public static string getDisplayScript ( string $locale [, string $in_locale ] )
代码语言:javascript
复制
public static string getDisplayVariant ( string $locale [, string $in_locale ] )
代码语言:javascript
复制
public static array getKeywords ( string $locale )
代码语言:javascript
复制
public static string getPrimaryLanguage ( string $locale )
代码语言:javascript
复制
public static string getRegion ( string $locale )
代码语言:javascript
复制
public static string getScript ( string $locale )
代码语言:javascript
复制
public static string lookup ( array $langtag , string $locale [, bool $canonicalize = false [, string $default ]] )
代码语言:javascript
复制
public static array parseLocale ( string $locale )
代码语言:javascript
复制
public static bool setDefault ( string $locale )

}

Predefined Constants

Locale::DEFAULT_LOCALE (null) 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 getLocalte method of different classes.

Locale::ACTUAL_LOCALE (string) This is locale the data actually comes from.Locale::VALID_LOCALE (string) 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 (string) Language subtagLocale::EXTLANG_TAG (string) Extended language subtagLocale::SCRIPT_TAG (string) Script subtagLocale::REGION_TAG (string) Region subtagLocale::VARIANT_TAG (string) Variant subtagLocale::GRANDFATHERED_LANG_TAG (string) Grandfathered Language subtagLocale::PRIVATE_TAG (string) Private subtag

See Also

Table of Contents

  • Locale::acceptFromHttp — Tries to find out best available locale based on HTTP "Accept-Language" header
  • 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::lookup — Searches the language tag list for the best match to the language
  • Locale::parseLocale — Returns a key-value array of locale ID subtag elements.
  • Locale::setDefault — sets the default runtime locale

← NumberFormatter::setTextAttribute

Locale::acceptFromHttp →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com