Skip to content

Commit a293225

Browse files
committed
Reword
1 parent 618573d commit a293225

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

controller.rst

+2-32
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
623623

624624
.. _request-object-info:
625625

626-
The Request Object
627-
------------------
626+
The Request and Response Object
627+
-------------------------------
628628

629629
As mentioned :ref:`earlier <controller-request-argument>`, Symfony will
630630
pass the ``Request`` object to any controller argument that is type-hinted with
@@ -660,36 +660,6 @@ the ``Request`` class::
660660
The ``Request`` class has several public properties and methods that return any
661661
information you need about the request.
662662

663-
For example, the method ``getPreferredLanguage`` accepts an array of preferred languages and
664-
returns the best language for the user, based on the ``Accept-Language`` header.
665-
The locale is returned with language, script and region, if available (e.g. ``en_US``, ``fr_Latn_CH`` or ``pt``).
666-
667-
Before Symfony 7.1, this method had the following logic:
668-
669-
1. If no locale is set as a parameter, it returns the first language in the
670-
``Accept-Language`` header or ``null`` if the header is empty
671-
2. If no ``Accept-Language`` header is set, it returns the first locale passed
672-
as a parameter.
673-
3. If a locale is set as a parameter and in the ``Accept-Language`` header,
674-
it returns the first exact match.
675-
4. Then, it returns the first language passed in the ``Accept-Language`` header or as argument.
676-
677-
Starting from Symfony 7.1, the method has an additional step:
678-
679-
1. If no locale is set as a parameter, it returns the first language in the
680-
``Accept-Language`` header or ``null`` if the header is empty
681-
2. If no ``Accept-Language`` header is set, it returns the first locale passed
682-
as a parameter.
683-
3. If a locale is set as a parameter and in the ``Accept-Language`` header,
684-
it returns the first exact match.
685-
4. If a language matches the locale, but has a different script or region, it returns the first language in the
686-
``Accept-Language`` header that matches the locale's language, script or region combination
687-
(e.g. ``fr_CA`` will match ``fr_FR``).
688-
5. Then, it returns the first language passed in the ``Accept-Language`` header or as argument.
689-
690-
The Response Object
691-
-------------------
692-
693663
Like the ``Request``, the ``Response`` object has a public ``headers`` property.
694664
This object is of the type :class:`Symfony\\Component\\HttpFoundation\\ResponseHeaderBag`
695665
and provides methods for getting and setting response headers. The header names are

translation.rst

+19
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,25 @@ the framework:
981981
This ``default_locale`` is also relevant for the translator, as shown in the
982982
next section.
983983

984+
Selecting the Language Preferred by the User
985+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
986+
987+
If your application supports multiple languages, the first time a user visits your
988+
site it's common to redirect them to the best possible language according to their
989+
preferences. This is achieved with the ``getPreferredLanguage()`` method of the
990+
:ref:`Request object <controller-request-argument>`::
991+
992+
// get the Request object somehow (e.g. as a controller argument)
993+
$request = ...
994+
// pass an array of the locales (their script and region parts are optional) supported
995+
// by your application and the method returns the best locale for the current user
996+
$locale = $request->getPreferredLanguage(['pt', 'fr_Latn_CH', 'en_US'] );
997+
998+
Symfony finds the best possible language based on the locales passed as argument
999+
and the value of the ``Accept-Language`` HTTP header. If it can't find a perfect
1000+
match between them, this method returns the first locale passed as argument
1001+
(that's why the order of the passed locales is important).
1002+
9841003
.. _translation-fallback:
9851004

9861005
Fallback Translation Locales

0 commit comments

Comments
 (0)