Permalink
Browse files

Improving Locale::lookup() so it better detects alternative locales

  • Loading branch information...
1 parent ab8f628 commit 6c95cb80bcf465ffc7695dd6bc387b887a535741 @mariano mariano committed Mar 11, 2012
Showing with 44 additions and 4 deletions.
  1. +13 −3 g11n/Locale.php
  2. +31 −1 tests/cases/g11n/LocaleTest.php
View
@@ -191,12 +191,22 @@ public static function cascade($locale) {
*/
public static function lookup($locales, $locale) {
$tags = static::decompose($locale);
-
- while (count($tags) > 0) {
+ $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);
}
}
@@ -291,4 +301,4 @@ protected static function _preferredConsole($request) {
}
}
-?>
+?>
@@ -220,6 +220,30 @@ public function testLookup() {
'zh_Hans_HK_REVISED'
);
$this->assertEqual('zh_Hans_HK', $result);
+
+ $result = Locale::lookup(
+ array('en', 'en_UK', 'en_US', 'es', 'es_AR'),
+ 'en'
+ );
+ $this->assertEqual('en', $result);
+
+ $result = Locale::lookup(
+ array('en', 'en_UK', 'en_US', 'es', 'es_AR'),
+ 'en_UK'
+ );
+ $this->assertEqual('en_UK', $result);
+
+ $result = Locale::lookup(
+ array('en', 'en_UK', 'en_US', 'es', 'es_AR'),
+ 'es_ES'
+ );
+ $this->assertEqual('es', $result);
+
+ $result = Locale::lookup(
+ array('en', 'en_UK', 'en_US', 'es_AR'),
+ 'es_ES'
+ );
+ $this->assertEqual('es_AR', $result);
}
public function testPreferredFromActionRequest() {
@@ -345,7 +369,13 @@ public function testPreferredAvailableNegotiation() {
array('zh_Hans_HK_REVISED', 'zh_Hans_HK', 'zh', 'en')
);
$this->assertEqual('zh', $result);
+
+ $result = Locale::preferred(
+ array('es_ES', 'en'),
+ array('da', 'en_GB', 'en', 'es_AR')
+ );
+ $this->assertEqual('es_AR', $result);
}
}
-?>
+?>

0 comments on commit 6c95cb8

Please sign in to comment.