I have this function in controller:
/**
* @Route("/{lang}", name="homepage_lang")
* @param Request $request
* @param $lang
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexLangAction(Request $request, $lang)
{
if (isset($lang)){
$newLang = $lang;
}else{
$localeLang = $request->getLocale();
$newLang = $localeLang;
}
$this->get('session')->set("_locale", $newLang);
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}
And Buttons in twig like this:
<button onclick="window.location.href='/es'" type="button">Español</button>
When I click the page refresh but not change the language, I have to click two times to change the language.