From da1fce814faa47918a1f5a6c897b3808bedfb0e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 12 Aug 2021 17:36:47 +0200 Subject: [PATCH] Switch on the fly locale with fb_locale=xx set --- EventSubscriber/FacebookSubscriber.php | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 EventSubscriber/FacebookSubscriber.php diff --git a/EventSubscriber/FacebookSubscriber.php b/EventSubscriber/FacebookSubscriber.php new file mode 100644 index 0000000..d950a54 --- /dev/null +++ b/EventSubscriber/FacebookSubscriber.php @@ -0,0 +1,75 @@ +locales = $locales; + + //Set router + $this->router = $router; + } + + /** + * Change locale for request with ?fb_locale=xx + * + * @param RequestEvent The request event + */ + public function onKernelRequest(RequestEvent $event) { + //Retrieve request + $request = $event->getRequest(); + + //Check for facebook locale + if ( + $request->query->has('fb_locale') && + in_array($preferred = $request->query->get('fb_locale'), $this->locales) + ) { + //Set locale + $request->setLocale($preferred); + + //Set default locale + $request->setDefaultLocale($preferred); + + //Get router context + $context = $this->router->getContext(); + + //Set context locale + $context->setParameter('_locale', $preferred); + + //Set back router context + $this->router->setContext($context); + } + } + + /** + * Get subscribed events + * + * @return array The subscribed events + */ + public static function getSubscribedEvents() { + return [ + // must be registered before the default locale listener + KernelEvents::REQUEST => [['onKernelRequest', 10]] + ]; + } +} -- 2.41.0