Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force length 2 for locales #56

Open
rsereir opened this issue May 14, 2022 · 5 comments
Open

Force length 2 for locales #56

rsereir opened this issue May 14, 2022 · 5 comments

Comments

@rsereir
Copy link

rsereir commented May 14, 2022

Hello,

Is it possible to force locale have length of 2 chars: instead of fr_FR, i want only fr ?

Rédoine

@kevinG73
Copy link

Hello @rsereir ,
I don't understand what you mean , where do you want to force 2 chars exactly ?

@rsereir
Copy link
Author

rsereir commented May 14, 2022

@kevinG73 My Accept-Language header is provided by my browser when frontend ( react ) call an api request to my Api Platform API.

This accept language value can be 'fr_FR' but my $locale field have length of 2 chars in my database. So i want to force transformation of fr_FR to fr when ApiPlatformTranslationBundle try to get locale to use

@kevinG73
Copy link

@kevinG73 My Accept-Language header is provided by my browser when frontend ( react ) call an api request to my Api Platform API.

This accept language value can be 'fr_FR' but my $locale field have length of 2 chars in my database. So i want to force transformation of fr_FR to fr when ApiPlatformTranslationBundle try to get locale to use

You can create an EventSubscriber like it :

<?php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class LocaleSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => [['onKernelRequest', 20]],
        ];
    }

    public function onKernelRequest(RequestEvent $event)
    {
        $request = $event->getRequest();
        $accept_language = $request->headers->get("Accept-Language");
          if (empty($accept_language)) {
            $lang = "fr";
        }else{
             $lang = substr($accept_language, 0, 2);
        }
        $request->setLocale($lang);
        $request->headers->set('Accept-Language', $lang);
         return;
    }
}

@rsereir
Copy link
Author

rsereir commented May 14, 2022

Is there any other way than to rewrite all the language recovery logic already written in the package ApiPlatformTranslationBundle ?

@kevinG73
Copy link

@rsereir
yes but in this case then it is not for me to do , I'm not contributor .
But add this class simply in any folder inside SRC and it's done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants