3

2 つのロケールで新しい Web サイトをセットアップしたいのですが、使用されているドメイン名によってロケールが検出されるはずです。これを行う方法はありますか?

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 を生成すると、それも素晴らしいことです。

敬具、

大安

4

3 に答える 3

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 ファイルからリスナー コンストラクターの既定のロケールを設定できます。ドメインによってロケールが検出されない場合は、既定のロケールを設定します。

于 2013-02-02T12:42:20.073 に答える
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

詳細については、使用シナリオに関するドキュメントを参照してください。

于 2013-08-11T21:46:13.347 に答える
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
于 2015-01-07T10:21:05.107 に答える