首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于域名的symfony2语言环境检测

问基于域名的symfony2语言环境检测
EN

Stack Overflow用户
提问于 2013-02-01 22:26:23
回答 3查看 3.5K关注 0票数 3

我想设置一个新的网站与2个地区和地区应检测到的域名,使用。你知道怎么做吗?

代码语言:javascript
复制
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的正确域名被选择。

致以亲切的问候,

大安

EN

回答 3

Stack Overflow用户

发布于 2013-02-02 20:42:20

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

代码语言:javascript
复制
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中:

代码语言:javascript
复制
services:

    my_locale_listener:
        class: MyBundle\LocaleListener
        tags:
            -  { name: kernel.event_subscriber }

您可以在侦听器构造函数中添加来自parameters.yml文件的默认locle,如果域未检测到区域设置,则设置默认区域设置。

票数 3
EN

Stack Overflow用户

发布于 2013-08-12 05:46:13

有一个包可以做到这一点:https://github.com/schmittjoh/JMSI18nRoutingBundle。

这就是在config.yml中设置它的方式

代码语言:javascript
复制
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。

票数 3
EN

Stack Overflow用户

发布于 2015-01-07 18:21:05

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

代码语言:javascript
复制
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中:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14648458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档