Skip to content

Commit

Permalink
Simplifying and fixing the defaultLocale() logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 24, 2014
1 parent 6a17582 commit ff39244
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/I18n/I18n.php
Expand Up @@ -30,8 +30,6 @@ class I18n {

protected static $_collection;

protected static $_defaultLocale;

protected static $_defaultFormatter = 'basic';

public static function translators() {
Expand Down Expand Up @@ -85,22 +83,18 @@ public static function translator($package = 'default', $locale = null, callable
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 === '') {
$current = 'en_US';
ini_set('intl.default_locale', $current);
}

return static::$_defaultLocale = $current;
return $current;
}

public static function defaultFormatter($name = null) {
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -36,6 +36,7 @@ public function tearDown() {
parent::tearDown();
I18n::clear();
I18n::defaultFormatter('basic');
I18n::defaultLocale('en_US');
Plugin::unload();
}

Expand Down Expand Up @@ -134,7 +135,28 @@ public function testDefaultLocale() {
$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'));
}

}

0 comments on commit ff39244

Please sign in to comment.