lithium\g11n\Locale::lookup()

public static method

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 its canonical form (i.e. 'zh_Hans_HK_REVISED').

Returns

string

The matched locale.

Source

	public static function lookup($locales, $locale) {
		$tags = static::decompose($locale);

		while (($count = count($tags)) > 0) {
			if (($key = array_search(static::compose($tags), $locales)) !== false) {
				return $locales[$key];
			}
			if ($count === 1) {
				foreach ($locales as $current) {
					if (strpos($current, current($tags) . '_') === 0) {
						return $current;
					}
				}
			}
			if (($key = array_search(static::compose($tags), $locales)) !== false) {
				return $locales[$key];
			}
			array_pop($tags);
		}
	}