-
Notifications
You must be signed in to change notification settings - Fork 27
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
Comments
Hello @rsereir , |
@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;
}
} |
Is there any other way than to rewrite all the language recovery logic already written in the package ApiPlatformTranslationBundle ? |
@rsereir |
Hello,
Is it possible to force locale have length of 2 chars: instead of fr_FR, i want only fr ?
Rédoine
The text was updated successfully, but these errors were encountered: