Closed mxndshock closed 1 year ago
Hi, just did it by creating this subscriber.:
<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class LocaleSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onKernelRequest', 200)),
);
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->headers->has("Accept-Language")) {
$locale = $request->headers->get('Accept-Language');
$request->setLocale($locale);
}
}
}
Then, from my frontend app, I attach the 'Accept-Language' header to my request and it works fine.
Hope this helps u to solve your problem.
Regards, Jérémy
This needs to be added in symfony framework :p.
I agree. It's a basic feature needed in most API. If I don't find any related PR on Symfony repo, I will create one
Hi, just did it by creating this subscriber.:
<?php namespace App\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; class LocaleSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return array( KernelEvents::REQUEST => array(array('onKernelRequest', 200)), ); } public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if ($request->headers->has("Accept-Language")) { $locale = $request->headers->get('Accept-Language'); $request->setLocale($locale); } } }
Then, from my frontend app, I attach the 'Accept-Language' header to my request and it works fine.
Hope this helps u to solve your problem.
Regards, Jérémy
It worked!
Thanks!
Hi, just did it by creating this subscriber.:
<?php namespace App\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; class LocaleSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return array( KernelEvents::REQUEST => array(array('onKernelRequest', 200)), ); } public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if ($request->headers->has("Accept-Language")) { $locale = $request->headers->get('Accept-Language'); $request->setLocale($locale); } } }
Then, from my frontend app, I attach the 'Accept-Language' header to my request and it works fine.
Hope this helps u to solve your problem.
Regards, Jérémy
For anyone using Symfony 4.3+: this solution works perfectly, except that GetResponseEvent
has been renamed to RequestEvent
(source: https://symfony.com/blog/new-in-symfony-4-3-simpler-event-dispatching#updated-httpkernel-event-classes).
Thank you @jeremyriverain for the tip! :)
You can use this https://symfony.com/blog/new-in-symfony-5-4-language-negotiation
I was able to apply this solution on my Symfony/FOSRestBundle backend. Thank you very much
Hi, i'm using ApiPlatform + Symfony 4.3.3 to build a web application. I can't find a way to set the locale for my request. It always fallbacks back to the default locale set in Symfony.
I tried using a RequestListener but it ignores the value i set.
How can i set the locale for my request?
Thanks