Symfony 6 Certification New exam with updated questions 100% online Show your expertise

New in Symfony 5.4: Language Negotiation

Symfony 5.4 is backed by Private Packagist. Private Packagist is a fast, reliable, and secure Composer repository for your private packages. It mirrors all your open-source dependencies for better availability and monitors them for security vulnerabilities.

Contributed by
Robin Chalas and Grégoire Hébert
in #43108.

The Accept-Language request HTTP header indicates the natural language and locale that the user prefers when browsing contents. It can be as simple as Accept-Language: * to accept any languages/locales or as complex as providing multiple prioritized languages/locales like Accept-Language: es-MX, es;q=0.9, en;q=0.8, *;q=0.5.

In Symfony 5.4, we've added new config options to integrate Accept-Language into your applications seamlessly:

# config/packages/framework.yaml
    # ...
    set_locale_from_accept_language: true
    set_content_language_from_locale: true
    enabled_locales: ['da', 'no', 'sv']

When set_locale_from_accept_language (which is false by default) is set to true, the request locale is automatically set based on the Accept-Language value. The value of the locale route attribute always overrides this option.

Moreover, if you define the framework.enabled_locales option (which is the new name of the existing framework.translator.enabled_locales option) the request locale will be restricted to one of those values (in the above example, it can only be Danish, Norwegian or Swedish) regardless of the Accept-Language value.

When set_content_language_from_locale (which is false by default) is set to true, the value of the Content-Language HTTP response header is automatically set based on the request locale.

These new options will allow your application to better reply to the language preferences of your visitors and users. It's already being considered in Symfony ecosystem applications such as API Platform and Sylius.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.


Thank you and good job for this one!
That's a really nice feature. Many thanks!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.