0

I am new to symfony.

Trying to add translation to my website when user change language with a dropdown. using Synfony Render controller to add the form to all pages.

But translation not working when changing dopdown.Only after refreshing page.

public function __construct(string $defaultLocale = 'en')
{
    $this->defaultLocale = $defaultLocale;
}

public function onKernelRequest(RequestEvent $event)
{
    $request = $event->getRequest();
    if (!$request->hasPreviousSession()) {
        return;
    }

    // try to see if the locale has been set as a _locale routing parameter
    if ($locale = $request->attributes->get('_locale')) {
        $request->getSession()->set('_locale', $locale);
    } else {
        // if no explicit locale has been set on this request, use one from the session
        $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
    }
}

public static function getSubscribedEvents()
{
    return [
        // must be registered before (i.e. with a higher priority than) the default Locale listener
        KernelEvents::REQUEST => [['onKernelRequest', 20]],
    ];
}

}

In the controller class ,I am managing the form and rendering it in the header.Whenever I change dropdown and refresh page -my code works and change language.


$form = $this->createForm(LangForm::class,$languageEntity,array('code' => $languageData));
        $form->handleRequest($masterRequest);
        $masterRequest->getSession()->set('_locale',$form->getData()->getlanguageCode());
return $this->render('header.html.twig', [
            'form' =>  $form->createView(),
        ]);

How can I fix this issue?

Note: I haven't added the The Locale and the URL section yet.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Mattew
  • 1
  • 1
  • Any suggetion will be really helpful . I am new to programming and really stuck with this problem. – Mattew Jun 22 '23 at 07:11

0 Answers0