PHP Class lithium\g11n\Locale

In order to avoid unnecessary overhead all methods throughout the framework accepting a locale require it to be well-formed according to the structure laid out below. For assuring the correct format use Locale::canonicalize() once on the locale. However the methods within this class will also work with not-so-well-formed locales. They accept both underscores and hyphens as separators between and don't care about the case of the individual tags. The identifier used by Lithium is based in its structure upon Unicode's language identifier and is compliant to BCP 47. language[_Script][_TERRITORY][_VARIANT] - language The spoken language, here represented by an ISO 639-1 code, where not available ISO 639-3 and ISO 639-5 codes are allowed too) tag. The tag should be lower-cased and is required. - Script The tag should have its first character capitalized, all others lower-cased. The tag is optional. - TERRITORY A geographical area, here represented by an ISO 3166-1 code. Should be all upper-cased and is optional. - VARIANT Should be all upper-cased and is optional.
Inheritance: extends lithium\core\StaticObject
Datei anzeigen Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_tags array Properties for locale tags.

Public Methods

Method Description
__callStatic ( string $method, array $params = [] ) : mixed Magic method enabling language, script, territory and variant methods to parse and retrieve individual tags from a locale.
canonicalize ( string $locale ) : string Returns a locale in its canonical form with tags formatted properly.
cascade ( string $locale ) : array Cascades a locale.
compose ( array $tags ) : string Composes a locale from locale tags. This is the pendant to Locale::decompose().
decompose ( string $locale ) : array Parses a locale into locale tags. This is the pendant to Locale::compose().
lookup ( array $locales, string $locale ) : string Searches an array of locales for the best match to a locale. The locale is iteratively simplified until either it matches one of the locales in the list or the locale can't be further simplified.
preferred ( object | array $request, array $available = null ) : string Determines the preferred locale from a request or array. Optionally negotiates the preferred locale with available locales.
respondsTo ( string $method, boolean $internal = false ) : boolean Determines if a given method can be called.

Protected Methods

Method Description
_preferredAction ( Request $request ) : array Detects preferred locales from an action request by looking at the 'Accept-Language' header as described by RFC 2616, section 14.4.
_preferredConsole ( Request $request ) : array Detects preferred locales from a console request by looking at certain environment variables. The environment variables may be present or not depending on your system. If multiple variables are present the following hierarchy is used: 'LANGUAGE', 'LC_ALL', 'LANG'.

Method Details

__callStatic() public static method

Locale::language('en_US'); // returns 'en' Locale::territory('en_US'); // returns 'US'
See also: lithium\g11n\Locale::$_tags
See also: lithium\g11n\Locale::decompose()
public static __callStatic ( string $method, array $params = [] ) : mixed
$method string
$params array
return mixed

_preferredAction() protected static method

Detects preferred locales from an action request by looking at the 'Accept-Language' header as described by RFC 2616, section 14.4.
protected static _preferredAction ( Request $request ) : array
$request lithium\action\Request
return array Preferred locales in their canonical form (i.e. `'fr_CA'`).

_preferredConsole() protected static method

The locales of the 'LC_ALL' and the 'LANG' are formatted according to the posix standard: language(_territory)(.encoding)(@modifier). Locales having such a format are automatically canonicalized and transformed into the Locale class' format.
protected static _preferredConsole ( Request $request ) : array
$request lithium\console\Request
return array Preferred locales in their canonical form (i.e. `'fr_CA'`).

canonicalize() public static method

Returns a locale in its canonical form with tags formatted properly.
public static canonicalize ( string $locale ) : string
$locale string A locale in an arbitrary form (i.e. `'ZH-HANS-HK_REVISED'`).
return string A locale in its canonical form (i.e. `'zh_Hans_HK_REVISED'`).

cascade() public static method

Usage: Locale::cascade('en_US'); returns array('en_US', 'en', 'root') Locale::cascade('zh_Hans_HK_REVISED'); returns array('zh_Hans_HK_REVISED', 'zh_Hans_HK', 'zh_Hans', 'zh', 'root')
public static cascade ( string $locale ) : array
$locale string A locale in an arbitrary form (i.e. `'en_US'` or `'EN-US'`).
return array Indexed array of locales (starting with the most specific one).

compose() public static method

Composes a locale from locale tags. This is the pendant to Locale::decompose().
public static compose ( array $tags ) : string
$tags array An array as obtained from `Locale::decompose()`.
return string A locale with tags separated by underscores or `null` if none of the passed tags could be used to compose a locale.

decompose() public static method

Parses a locale into locale tags. This is the pendant to Locale::compose().
public static decompose ( string $locale ) : array
$locale string A locale in an arbitrary form (i.e. `'en_US'` or `'EN-US'`).
return array Parsed language, script, territory and variant tags.

lookup() public static method

This method partially implements the lookup matching scheme as described in RFC 4647, section 3.4 and thus does not strictly conform to the specification. Differences to specification: - No support for wildcards in the to-be-matched locales. - No support for locales with private subtags. - No support for a default return value. - Passed locales are required to be in canonical form (i.e. 'ja_JP').
public static lookup ( array $locales, string $locale ) : string
$locales array Locales to match against `$locale`.
$locale string A locale in its canonical form (i.e. `'zh_Hans_HK_REVISED'`).
return string The matched locale.

preferred() public static method

Determines the preferred locale from a request or array. Optionally negotiates the preferred locale with available locales.
See also: lithium\g11n\Locale::_preferredAction()
See also: lithium\g11n\Locale::_preferredConsole()
See also: lithium\g11n\Locale::lookup()
public static preferred ( object | array $request, array $available = null ) : string
$request object | array An action or console request object or an array of locales.
$available array A list of locales to negotiate the preferred locale with.
return string The preferred locale in its canonical form (i.e. `'fr_CA'`).

respondsTo() public static method

Determines if a given method can be called.
public static respondsTo ( string $method, boolean $internal = false ) : boolean
$method string Name of the method.
$internal boolean Provide `true` to perform check from inside the class/object. When `false` checks also for public visibility; defaults to `false`.
return boolean Returns `true` if the method can be called, `false` otherwise.

Property Details

$_tags protected_oe static_oe property

Properties for locale tags.
protected static array $_tags
return array