2013-02-01 63 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