symfony2根据域名检测区域设置

时间:2013-02-01 14:26:23

标签: locale symfony-2.1

我想设置一个包含2个区域设置的新网站,并且应该通过所使用的域名检测区域设置。知道怎么做吗?

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或fr中生成一个url,那么选择正确的域名也会很棒。

亲切的问候,

大安

3 个答案:

答案 0 :(得分: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文件中放入侦听器构造函数默认的locle,如果域集默认语言环境未检测到locale。

答案 1 :(得分: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

答案 2 :(得分:1)

捆绑包JMSI18nRoutingBundle仅支持symfony< = 2.1.x. 好方法似乎使用Daniel Korsak的解决方案。这是一个更完整的参数示例。

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