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

Utworzony 3y | 18 lis 2021, 09:20:22


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

Just one month to go before SymfonyLive Paris 2025 workshops begin!

SymfonyLive Paris 2025, conference in French language only, will already start in 1 month with the workshops! Have a look on the topics and join us! Schedule details are available here.

📣

25 lut 2025, 15:20:33 | Symfony
New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24 lut 2025, 16:20:03 | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

24 lut 2025, 13:50:07 | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

23 lut 2025, 10:10:09 | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

21 lut 2025, 09:20:12 | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

20 lut 2025, 10:10:13 | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

19 lut 2025, 08:40:05 | Symfony