Description
Symfony version(s) affected
6.4.0
Description
Default locale setting is ignored when "set_locale_from_accept_language" is used.
How to reproduce
I have the following configuration:
framework:
default_locale: en
set_locale_from_accept_language: true
set_content_language_from_locale: false
enabled_locales: ['pl', 'en']
Then, by issuing curl https://localhost
which has following code:
class IndexNoLocaleController extends AbstractController
{
#[Route('/')]
public function __invoke(Request $request): Response
{
return $this->redirectToRoute('app_default', ['_locale' => $request->getLocale()]);
}
}
I am redirected to /pl
.
I expect to be redirected to /en
instead.
Note: changing order of enabled_locales: ['en', 'pl']
will result in redirection to /en
as the first value from the array is used as "default", yet still default from the "default_locale" is ignored.
Possible Solution
The \Symfony\Component\HttpFoundation\Request::getPreferredLanguage
accepts ['pl', 'en'] as input, as there are no languages set by curl it just falls into:
if (!$preferredLanguages) {
return $locales[0];
}
The call originates from LocaleListener.php:72
:
private function setLocale(Request $request): void
{
if ($locale = $request->attributes->get('_locale')) {
$request->setLocale($locale);
} elseif ($this->useAcceptLanguageHeader) {
if ($preferredLanguage = $request->getPreferredLanguage($this->enabledLocales)) {
$request->setLocale($preferredLanguage);
}
$request->attributes->set('_vary_by_language', true);
}
}
In that listener, there is $defaultLocale
property available, as well as in the request, therefore maybe the solution is to use the default locale rather than picking "first entry" from enabled locales.
Additional Context
This is an edge case, but can affect mostly Google Bots as bots do not set accept language headers.