捆绑包JMSI18nRoutingBundle只支持symfony < = 2.1.x。 好方法似乎使用丹尼尔科尔萨克的解决方案。这是一个更完整的参数例子。
namespace Path\ToYourBundle\Listeners;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpKernel\KernelEvents;
use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class LocaleListener implements EventSubscriberInterface
{
protected $domainLocales;
protected $defaultLocale;
public function __construct($container,$defaultLocale)
{
$this->domainLocales = $container->getParameter('domain_locales');
$this->defaultLocale = $defaultLocale;
}
/**
* Set default locale
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
// get domain name
$host = $request->getHttpHost();
// or $host = $request->getHost();
$locale = $this->defaultLocale;
if (array_key_exists($host, $this->domainLocales))
{
$locale = $this->domainLocales[$host];
}
$request->setLocale($locale);
}
/**
* {@inheritdoc}
*/
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
而在你services.yml:
services:
my_locale_listener:
class: Path\ToYourBundle\Listeners\LocaleListener
tags:
- { name: kernel.event_subscriber }
arguments: [@service_container,%locale%]
parameters:
domain_locales:
domain1: en
domain2: fr
有没有一个更建立的方式来做到这一点?因为为了生成url,我需要覆盖url生成器,然后我假设... –