Skip to content

Commit

Permalink
Merge pull request #12663 from Zuluru/master
Browse files Browse the repository at this point in the history
Use Locale::lookup to handle generic options as a fallback
  • Loading branch information
markstory committed Nov 14, 2018
2 parents 4da56e3 + afa1fe9 commit b17e866
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/I18n/Middleware/LocaleSelectorMiddleware.php
Expand Up @@ -55,7 +55,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
if (!$locale) {
return $next($request, $response);
}
if (in_array($locale, $this->locales) || $this->locales === ['*']) {
if ($this->locales !== ['*']) {
$locale = Locale::lookup($this->locales, $locale, true);
}
if ($locale || $this->locales === ['*']) {
I18n::setLocale($locale);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/I18n/Middleware/LocaleSelectorMiddlewareTest.php
Expand Up @@ -101,6 +101,20 @@ public function testInvokeLocaleAccepted()
$this->assertSame('es', I18n::getLocale(), 'es is accepted');
}

/**
* The default locale should change when the request locale has an accepted fallback option
*
* @return void
*/
public function testInvokeLocaleAcceptedFallback()
{
$request = ServerRequestFactory::fromGlobals(['HTTP_ACCEPT_LANGUAGE' => 'es-ES;q=0.8,da;q=0.4']);
$response = new Response();
$middleware = new LocaleSelectorMiddleware(['en_CA', 'es']);
$middleware($request, $response, $this->next);
$this->assertSame('es', I18n::getLocale(), 'es is accepted');
}

/**
* The default locale should change when the '*' is accepted
*
Expand Down

0 comments on commit b17e866

Please sign in to comment.