Skip to content

Instantly share code, notes, and snippets.

@ThePeterMick
Created October 16, 2018 19:00
Show Gist options
  • Save ThePeterMick/b1ecaca960296479844fd3fc49e51cf2 to your computer and use it in GitHub Desktop.
Save ThePeterMick/b1ecaca960296479844fd3fc49e51cf2 to your computer and use it in GitHub Desktop.
Symfony 4 use dashes in locales for subtags as per RFC5646
<?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()));
}
}
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