Skip to content

Commit 28a86fb

Browse files
committed
Merge branch '7.0' into 7.1
* 7.0: Refactor Request object documentation and redefine method logic
2 parents 75ab793 + 45dbc06 commit 28a86fb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

translation.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,25 @@ the framework:
963963
This ``default_locale`` is also relevant for the translator, as shown in the
964964
next section.
965965

966+
Selecting the Language Preferred by the User
967+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
968+
969+
If your application supports multiple languages, the first time a user visits your
970+
site it's common to redirect them to the best possible language according to their
971+
preferences. This is achieved with the ``getPreferredLanguage()`` method of the
972+
:ref:`Request object <controller-request-argument>`::
973+
974+
// get the Request object somehow (e.g. as a controller argument)
975+
$request = ...
976+
// pass an array of the locales (their script and region parts are optional) supported
977+
// by your application and the method returns the best locale for the current user
978+
$locale = $request->getPreferredLanguage(['pt', 'fr_Latn_CH', 'en_US'] );
979+
980+
Symfony finds the best possible language based on the locales passed as argument
981+
and the value of the ``Accept-Language`` HTTP header. If it can't find a perfect
982+
match between them, this method returns the first locale passed as argument
983+
(that's why the order of the passed locales is important).
984+
966985
.. _translation-fallback:
967986

968987
Fallback Translation Locales

0 commit comments

Comments
 (0)