0

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.

ximo12
  • 147
  • 2
  • 9

2 Answers2

3

If you're changing the session in the controller, you'll need to reload after doing so. Try something like this:

/**
 * @Route("/{lang}", name="homepage_lang")
 * @param Request $request
 * @param $lang
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function indexLangAction(Request $request, $lang)
{
    $session = $this->get('session');
    if ($session->has("_locale") && $lang !== $session->get("_locale")) {
        $session->set("_locale", $lang);
        return new RedirectResponse('/' . $lang);
    }

    return $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
    ]);
}
OK sure
  • 2,617
  • 16
  • 29
2

The cleanest way to do that is to use a link to the named route:

<a href="{{ path('homepage_lang') }}" title="Español">Español</a>
// if you are using bootstrap as a CSS framework then add the btn class
// so the link will look like a button
<a href="{{ path('homepage_lang') }}" title="Español" class="btn btn-primary">Español</a>

Edit

// for a route with parameters, you can use the following
<a href="{{ path('homepage_lang', {'lang': 'es'}) }}" title="Español" class="btn btn-primary">Español</a>

Edit2

Seems like the fact of changing the locale alone does not take effect immediatly, you need to somehow to do a redirect, refer to this question, you'll find more details, and you can even go with the best practice and add your event listener for this.

User locale does not work at first request

teeyo
  • 3,665
  • 3
  • 22
  • 37
  • So how can I get the language value in controller? – ximo12 Oct 02 '17 at 08:04
  • Perfect! it works! thanks. but I have the same problem, I have to click two times for refresh the language. I click one and refresh the page but not the language. – ximo12 Oct 02 '17 at 08:12
  • Which means it wasn't the Javascript's fault in the link, let me take a deep look at that code in the action of the controller – teeyo Oct 02 '17 at 08:20
  • @ximo12 I updated the answer, you can use a redirect to fix your issue, or maybe craft your own eventListener for this. – teeyo Oct 02 '17 at 08:32