捆綁包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生成器,然後我假設... –