Translator is not loading correct locale

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm trying to use symfony sticky locale to change the web locale through the url. For this as is explained y the page y linked before I've created a event subscriber which implements this on onKernelRequest function.

The Event subricreber is running and doing de if($loc) block correctly, but it seems not to be detected by the translator, so it could be a event subcriber priority problem, but I tried to change its priority an still getting same local.

Event Subscriber

php
// src/EventSubscriber/LocaleSubscriber.php
namespace App\EventSubscriber;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LocaleSubscriber implements EventSubscriberInterface
{
    private $defaultLocale;

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

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }
        $loc = $request->attributes->get('_locale');

        // try to see if the locale has been set as a _locale routing parameter
        if ($loc) {
            $request->getSession()->set('_locale', $loc);
            $request->setLocale($loc);
        } 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]],
        ];
    }
}

Controller

/**
* @Route("/{_locale}/pro/live")
*/
public function index(Request $request)
{
    return $this->render('pro/live/index.html.twig');
}

I'm expecting to get change the translator locale to which is writed in the url.

If I change the url to /en/pro/live/ $request->getLocale() in the controller returns the correct locale but the translator local still in es

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.