New in Symfony 5.4: Language Negotiation

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:

    1

2 3 4 5 6

config/packages/framework.yaml

framework:

...

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.

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-5-4-language-negotiation?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | Nov 18, 2021, 9:20:22 AM


Login to add comment

Other posts in this group

SymfonyOnline June 2025: Inside a Financial App Breach: Debugging a Million-Dollar Bug

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 22, 2025, 1:50:03 PM | Symfony
New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

Apr 22, 2025, 9:10:36 AM | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

Apr 20, 2025, 8:30:06 AM | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 16, 2025, 4:30:02 PM | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

Apr 15, 2025, 2:50:24 PM | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

Apr 13, 2025, 9:40:02 AM | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 10, 2025, 4:40:34 PM | Symfony