Created
October 16, 2018 19:00
-
-
Save ThePeterMick/b1ecaca960296479844fd3fc49e51cf2 to your computer and use it in GitHub Desktop.
Symfony 4 use dashes in locales for subtags as per RFC5646
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Listener; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | |
class LocaleListener { | |
/** | |
* | |
* @var \Symfony\Component\DependencyInjection\ContainerInterface $container | |
*/ | |
protected $container; | |
/** | |
* Construct the listener | |
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container | |
*/ | |
public function __construct(ContainerInterface $container) { // this is @service_container | |
$this->container = $container; | |
} | |
/** | |
* Listen for request events | |
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event | |
*/ | |
public function onKernelRequest(GetResponseEvent $event) { | |
if (!$event->isMasterRequest()) { | |
// don't do anything if it's not the master request | |
return; | |
} | |
$request = $event->getRequest(); | |
$request->setLocale(str_replace('_', '-', $request->getLocale())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
app.locale_listener: | |
class: App\Listener\LocaleListener | |
arguments: ['@service_container'] | |
tags: | |
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment