Browse Source

Simplifying and fixing the defaultLocale() logic

Jose Lorenzo Rodriguez 11 years ago
parent
commit
ff392449d5
2 changed files with 24 additions and 8 deletions
  1. 2 8
      src/I18n/I18n.php
  2. 22 0
      tests/TestCase/I18n/I18nTest.php

+ 2 - 8
src/I18n/I18n.php

@@ -30,8 +30,6 @@ class I18n {
 
 	protected static $_collection;
 
-	protected static $_defaultLocale;
-
 	protected static $_defaultFormatter = 'basic';
 
 	public static function translators() {
@@ -85,14 +83,10 @@ class I18n {
 	public static function defaultLocale($locale = null) {
 		if (!empty($locale)) {
 			ini_set('intl.default_locale', $locale);
-			static::$_defaultLocale = $locale;
+			static::translators()->setLocale($locale);
 			return;
 		}
 
-		if (static::$_defaultLocale !== null) {
-			return static::$_defaultLocale;
-		}
-
 		$current = ini_get('intl.default_locale');
 
 		if ($current === '') {
@@ -100,7 +94,7 @@ class I18n {
 			ini_set('intl.default_locale', $current);
 		}
 
-		return static::$_defaultLocale = $current;
+		return $current;
 	}
 
 	public static function defaultFormatter($name = null) {

+ 22 - 0
tests/TestCase/I18n/I18nTest.php

@@ -36,6 +36,7 @@ class I18nTest extends TestCase {
 		parent::tearDown();
 		I18n::clear();
 		I18n::defaultFormatter('basic');
+		I18n::defaultLocale('en_US');
 		Plugin::unload();
 	}
 
@@ -134,7 +135,28 @@ class I18nTest extends TestCase {
 		$this->assertEquals('en_US', I18n::defaultLocale());
 		$this->assertEquals('en_US', ini_get('intl.default_locale'));
 		I18n::defaultLocale('fr_FR');
+		$this->assertEquals('fr_FR', I18n::defaultLocale());
 		$this->assertEquals('fr_FR', ini_get('intl.default_locale'));
 	}
 
+/**
+ * Tests that changing the default locale also changes the way translators
+ * are fetched
+ *
+ * @return void
+ */
+	public function testGetTranslatorByDefaultLocale() {
+		I18n::translator('custom', 'fr_FR', function() {
+			$package = new Package();
+			$package->setMessages([
+				'Cow' => 'Le moo'
+			]);
+			return $package;
+		});
+
+		I18n::defaultLocale('fr_FR');
+		$translator = I18n::translator('custom');
+		$this->assertEquals('Le moo', $translator->translate('Cow'));
+	}
+
 }