Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Default locale is ignored when set_locale_from_accept_language is used #53086

Copy link
Copy link
Closed
@jkobus

Description

@jkobus
Issue body actions

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.