Skip to content

Commit

Permalink
Merge pull request #7213 from pperejon/issue-7208-LocaleSelectorFilter
Browse files Browse the repository at this point in the history
LocaleSelectorFilter now sets the current locale (issue #7208)
  • Loading branch information
markstory committed Aug 11, 2015
2 parents fd37e91 + 4685fd3 commit b86dcd6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Routing/Filter/LocaleSelectorFilter.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Routing\Filter;

use Cake\Event\Event;
use Cake\I18n\I18n;
use Cake\Routing\DispatcherFilter;
use Locale;

Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct($config = [])
}

/**
* Inspects the request for the Accept-Language header and sets the default
* Inspects the request for the Accept-Language header and sets the
* Locale for the current runtime if it matches the list of valid locales
* as passed in the configuration.
*
Expand All @@ -64,6 +65,6 @@ public function beforeDispatch(Event $event)
return;
}

Locale::setDefault($locale);
I18n::locale($locale);
}
}
11 changes: 6 additions & 5 deletions tests/TestCase/Routing/Filter/LocaleSelectorFilterTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\Routing\Filter;

use Cake\Event\Event;
use Cake\I18n\I18n;
use Cake\Network\Request;
use Cake\Routing\Filter\LocaleSelectorFilter;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -63,19 +64,19 @@ public function testSimpleSelection()
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'en-GB,en;q=0.8,es;q=0.6,da;q=0.4']
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('en_GB', Locale::getDefault());
$this->assertEquals('en_GB', I18n::locale());

$request = new Request([
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'es_VE,en;q=0.8,es;q=0.6,da;q=0.4']
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('es_VE', Locale::getDefault());
$this->assertEquals('es_VE', I18n::locale());

$request = new Request([
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'en;q=0.4,es;q=0.6,da;q=0.8']
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('da', Locale::getDefault());
$this->assertEquals('da', I18n::locale());
}

/**
Expand All @@ -97,7 +98,7 @@ public function testWithWhitelist()
]
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('es_VE', Locale::getDefault());
$this->assertEquals('es_VE', I18n::locale());

Locale::setDefault('en_US');
$request = new Request([
Expand All @@ -106,6 +107,6 @@ public function testWithWhitelist()
]
]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('en_US', Locale::getDefault());
$this->assertEquals('en_US', I18n::locale());
}
}

0 comments on commit b86dcd6

Please sign in to comment.