core icon indicating copy to clipboard operation
core copied to clipboard

Set locale for request

Open mxndshock opened this issue 6 years ago • 6 comments
trafficstars

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.

  public function onKernelRequest(RequestEvent $event)
    {

        $token = $this->tokenStorage->getToken();
        $user = ($token && $token->getUser()) ? $token->getUser() : null;
        $request = $event->getRequest();

        if(!$user || !($user instanceof User)) {
            $request->setLocale('it');
        } else {
            $request->setLocale($user->getLang()->getLang());
        }
    }

How can i set the locale for my request?

Thanks

mxndshock avatar Oct 09 '19 12:10 mxndshock

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

jeremyriverain avatar Oct 16 '19 07:10 jeremyriverain

This needs to be added in symfony framework :p.

soyuka avatar Oct 18 '19 08:10 soyuka

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

jeremyriverain avatar Oct 18 '19 09:10 jeremyriverain

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!

mxndshock avatar Oct 18 '19 10:10 mxndshock

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! :)

liarco avatar Sep 19 '20 10:09 liarco

You can use this https://symfony.com/blog/new-in-symfony-5-4-language-negotiation

unst avatar Jun 10 '22 01:06 unst

I was able to apply this solution on my Symfony/FOSRestBundle backend. Thank you very much

Astro-Otter-Space avatar Dec 20 '23 15:12 Astro-Otter-Space