Extends
lithium\core\StaticObject
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.
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'`).
Parameters
- array $locales Locales to match against `$locale`.
- string $locale A locale in it's canonical form (i.e. `'zh_Hans_HK_REVISED'`).
Returns
string The matched locale.Source
public static function lookup($locales, $locale) {
$tags = static::decompose($locale);
$count = count($tags);
while ($count > 0) {
if (($key = array_search(static::compose($tags), $locales)) !== false) {
return $locales[$key];
} elseif ($count == 1) {
foreach($locales as $currentLocale) {
if (strpos($currentLocale, current($tags) . '_') === 0) {
return $currentLocale;
}
}
}
if (($key = array_search(static::compose($tags), $locales)) !== false) {
return $locales[$key];
}
array_pop($tags);
$count = count($tags);
}
}