2013-02-01 47 views
3

我想设置一个新的网站有2个区域设置,并且区域设置应该被检测到的域名所使用。任何想法如何做到这一点?symfony2检测基于域名的区域设置

for example locales: nl and fr 
when www.somenldomainname.be is used then the nl locale should be detected 
when www.somefrdomainname.be is used then the fr locale should be detected 

如果我在nl中产生一个url或fr选择了正确的域名,它也会很好。

亲切的问候,

大安

回答

3

您可以创建一个事件侦听器来检测你的域名:

class LocaleListener implements EventSubscriberInterface 
{ 

    /** 
    * 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 = 'en'; 
     if ($host == 'domain1') { 
      $locale = 'fr'; 
     } 
     $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: MyBundle\LocaleListener 
     tags: 
      - { name: kernel.event_subscriber } 

您可以从parameters.yml文件中放入侦听器构造函数默认位置,并且如果区域设置未被域设置缺省语言环境检测到。

+0

有没有一个更建立的方式来做到这一点?因为为了生成url,我需要覆盖url生成器,然后我假设... –

3

这里有一个包:https://github.com/schmittjoh/JMSI18nRoutingBundle

这是你如何把它架在config.yml

jms_i18n_routing: 
    default_locale: nl 
    locales: [nl, fr] 
    strategy: custom 
    hosts: 
     nl: www.somenldomainname.be 
     fr: www.somefrdomainname.be 
redirect_to_host: true 

详情请参阅documentation on usage scenarios

+0

这只是一个重定向到语言环境的主机?我想它应该以相反的方式工作 - 由主机设置区域设置 – dardarlt

+0

是的,它将检测主机的区域设置,而不是相反。我在使用这个包的2个域上运行一个项目。 –

1

捆绑包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