lithium\g11n\Locale::lookup PHP Method

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.
    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);
        }
    }

Usage Example

Beispiel #1
0
 public function testLookup()
 {
     $expected = 'zh_Hans_HK';
     $result = Locale::lookup(array('zh_Hans_REVISED', 'zh_Hans_HK', 'zh', 'zh_Hans'), 'zh_Hans_HK_REVISED');
     $this->assertEqual($expected, $result);
     $expected = 'zh_Hans_HK';
     $result = Locale::lookup(array('zh', 'zh_Hans_REVISED', 'zh_Hans_HK', 'zh_Hans'), 'zh_Hans_HK_REVISED');
     $this->assertEqual($expected, $result);
 }
All Usage Examples Of lithium\g11n\Locale::lookup