I'm currently working on symfony 5 and I've fully translated my website in several languages. I have a button to select the language, but I would like the default language of the site to be the one of the user (of his browser more precisely). For the moment I have found a solution, but it is not optimal at all.
What I do is that in my index, I check if the user has already browsed the site before, and if not, I redirect him to a change_locale route which will take the language in question as a parameter (He enters the condition only if it's his first visit)
public function index(Request $request): Response
{
// If this is the first visit to the site, the default language is set according to the user's browser language
if (!$request->hasPreviousSession()) {
return $this->redirectToRoute('change_locale', ['locale' => strtolower(str_split($_SERVER['HTTP_ACCEPT_LANGUAGE'], 2)[0])]);
}
return $this->render('accueil/index.html.twig');
}
Here I simply register the variable in the session to change the language.
And my problem is just after this step. When the user simply clicks on the language change button on the site, he goes back to the previous page (he doesn't enter the if).
However, if he comes to the site for the first time, he is redirected from the index, and when he comes to this route, he enters the condition if (!$request->hasPreviousSession()) and... That's the problem. Because if he hasn't visited anything before, I can't redirect him to the page he was visiting.
/**
* @Route("/change-locale/{locale}", name="change_locale")
*/
public function changeLocale($locale, Request $request)
{
$request->getSession()->set('_locale', $locale); // Storing the requested language in the session
// If it's the first page visited by the user
if (!$request->headers->get('referer')) {
return $this->redirectToRoute('index');
}
// Back to the previous page
return $this->redirect($request->headers->get('referer'));
}
So I try to remove this condition from my change_locale route and find a way to add in the header of the request the attribute 'referer' which points to the previous page.
I could do this in my index before doing the redirectToRoute to change_locale.