Symfony 2语言环境更改不起作用

时间:2016-01-08 10:43:06

标签: php symfony translation

我正在尝试更改与Symfony文档中相同的语言环境。

我的LocaleListener.php

namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LocaleListener implements EventSubscriberInterface
{
    private $defaultLocale;

    public function __construct($defaultLocale = 'en')
    {
        $this->defaultLocale = $defaultLocale;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }

        if ($locale = $request->query->get('_locale')) {
            $request->getSession()->set('_locale', $locale);
        } else {
            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
        }

    }

    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
        );
    }
}

services.yml

app.locale_listener:     class:AppBundle \ EventListener \ LocaleListener     参数:['%kernel.default_locale%']     标签:          - {name:kernel.event_subscriber}

HTML

<li><a href="?_locale=en">{{ 'language.en' | trans }}</a></li>
<li><a href="?_locale=ru">{{ 'language.ru' | trans }}</a></li>

我只是无法理解它为什么不起作用。这有什么不对吗?

编辑:当我转到另一个页面时,语言环境正在改变。怎么做,在同一页面上改变它?

1 个答案:

答案 0 :(得分:0)

我正在使用这个监听器并为我工作。 你可以通过在查询字符串中传递“lang”param来改变语言。

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\HttpKernel;


class LanaguageListener implements EventSubscriberInterface
{

    private $defaultLocale;

    public function __construct($defaultLocale)
    {
        $this->defaultLocale = $defaultLocale;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (HttpKernel::MASTER_REQUEST != $event->getRequestType())
        {
            return;
        }

        $locale = $request->attributes->get('lang');

        if (!$locale)
        {
            $locale = $request->query->get("lang");
            if (!$locale)
            {
                $locale = $request->getSession()->get('_locale', $this->defaultLocale);
            }
        }

        $request->getSession()->set('_locale', $locale);
        $request->setLocale($locale);
        $request->setDefaultLocale($locale);
        $request->getSession()->set('_locale', $locale);
        $request->request->set("lang", $locale);
        $request->query->set("lang", $locale);
//        echo ($locale);
    }

    public static function getSubscribedEvents()
    {
        return array(
            // must be registered before the default Locale listener
            \Symfony\Component\HttpKernel\KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
        );
    }

}