api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.45k stars 876 forks source link

Set locale for request #3161

Closed mxndshock closed 1 year ago

mxndshock commented 5 years ago

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

jeremyriverain commented 5 years 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

soyuka commented 5 years ago

This needs to be added in symfony framework :p.

jeremyriverain commented 5 years ago

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

mxndshock commented 5 years 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

It worked!

Thanks!

liarco commented 4 years 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

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

unst commented 2 years ago

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

Astro-Otter-Space commented 11 months ago

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