Skip to content
Permalink
Browse files

LocaleSelectorFilter now sets the current locale

    
LocaleSelectorFilter now sets the current locale based on the HTTP
Accept header, not the default one that is stablished application wide.

fixes #7208
  • Loading branch information...
pperejon committed Aug 11, 2015
1 parent fd37e91 commit 48d0c2c0ea50fdcb5797e01d8a003957ee99e30a
@@ -17,6 +17,7 @@
use Cake\Event\Event; use Cake\Event\Event;
use Cake\Routing\DispatcherFilter; use Cake\Routing\DispatcherFilter;
use Locale; use Locale;
use Cake\I18n\I18n;
/** /**
* Sets the runtime default locale for the request based on the * Sets the runtime default locale for the request based on the
@@ -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 * Locale for the current runtime if it matches the list of valid locales
* as passed in the configuration. * as passed in the configuration.
* *
@@ -64,6 +65,6 @@ public function beforeDispatch(Event $event)
return; return;
} }
Locale::setDefault($locale); I18n::locale($locale);
} }
} }
@@ -19,6 +19,7 @@
use Cake\Routing\Filter\LocaleSelectorFilter; use Cake\Routing\Filter\LocaleSelectorFilter;
use Cake\TestSuite\TestCase; use Cake\TestSuite\TestCase;
use Locale; use Locale;
use Cake\I18n\I18n;
/** /**
* Locale selector filter test. * Locale selector filter test.
@@ -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'] '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])); $filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('en_GB', Locale::getDefault()); $this->assertEquals('en_GB', I18n::locale());
$request = new Request([ $request = new Request([
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'es_VE,en;q=0.8,es;q=0.6,da;q=0.4'] '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])); $filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('es_VE', Locale::getDefault()); $this->assertEquals('es_VE', I18n::locale());
$request = new Request([ $request = new Request([
'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'en;q=0.4,es;q=0.6,da;q=0.8'] 'environment' => ['HTTP_ACCEPT_LANGUAGE' => 'en;q=0.4,es;q=0.6,da;q=0.8']
]); ]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request])); $filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('da', Locale::getDefault()); $this->assertEquals('da', I18n::locale());
} }
/** /**
@@ -97,7 +98,7 @@ public function testWithWhitelist()
] ]
]); ]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request])); $filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('es_VE', Locale::getDefault()); $this->assertEquals('es_VE', I18n::locale());
Locale::setDefault('en_US'); Locale::setDefault('en_US');
$request = new Request([ $request = new Request([
@@ -106,6 +107,6 @@ public function testWithWhitelist()
] ]
]); ]);
$filter->beforeDispatch(new Event('name', null, ['request' => $request])); $filter->beforeDispatch(new Event('name', null, ['request' => $request]));
$this->assertEquals('en_US', Locale::getDefault()); $this->assertEquals('en_US', I18n::locale());
} }
} }

0 comments on commit 48d0c2c

Please sign in to comment.
You can’t perform that action at this time.