File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -963,6 +963,25 @@ the framework:
963963 This ``default_locale `` is also relevant for the translator, as shown in the
964964next 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
968987Fallback Translation Locales
You can’t perform that action at this time.
0 commit comments