Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #375 from mariano/g11n-locale-lookup-improvements

Improving Locale::lookup() so it better detects alternative locales
  • Loading branch information...
commit 78e30a07fef7ae0a11689ce52c9ef401566efcb2 2 parents ab8f628 + 6c95cb8
@nateabele nateabele authored
Showing with 44 additions and 4 deletions.
  1. +13 −3 g11n/Locale.php
  2. +31 −1 tests/cases/g11n/LocaleTest.php
View
16 g11n/Locale.php
@@ -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) {
}
}
-?>
+?>
View
32 tests/cases/g11n/LocaleTest.php
@@ -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);
}
}
-?>
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.