Core: Set locale for request

Created on 9 Oct 2019  路  5Comments  路  Source: api-platform/core

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

enhancement

Most helpful comment

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

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings