Symfony 2,切换语言不起作用

时间:2015-07-23 16:32:45

标签: php symfony translation

我想知道为什么当我点击视图上的链接来切换语言时它不起作用。如果我将默认语言环境设置为en或km,它将分别显示英语或高棉语。我有什么问题,我无法点击下面的链接切换语言?请帮我 !非常感谢先进的答案。

在视图中

<div class="col-sm-3 language-switcher">
    <a href="{{ path('ngs_locale', {locale: 'en'}) }}">English</a> |
    <a href="{{ path('ngs_locale', {locale: 'km'}) }}">ខ្មែរ</a>
</div>

的routing.yml

ngs_locale:
path:     locale/{locale}
defaults: { _controller: NGSHomeBundle:Locale:locale }

LocaleController.php

namespace NGS\HomeBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class LocaleController extends Controller
{
    public function localeAction(Request $request, $locale)
    {
        /** ======== dump ========== **/
        dump($locale); //"km"
        $request->getSession()->set('_locale', $locale);

        /** ======== dump ========== **/
        dump($request->getLocale()); //"en"

        $referer = $request->headers->get('referer');

        /** ======== dump ========== **/
        dump($referer);die; // null

        if (empty($referer)) {
            throw $this->createNotFoundException('ngs_not_found');
        }

        return $this->redirect($referer);
    }
}

LocaleListener.php

namespace NGS\HomeBundle\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;
        }

        // try to see if the locale has been set as a _locale routing parameter
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', $locale);
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
        }
    }

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

Service.yml

services:
ngs_home.locale_listener:
    class: NGS\HomeBundle\EventListener\LocaleListener
    arguments: ["%kernel.default_locale%"]
    tags:
        - { name: kernel.event_subscriber }

1 个答案:

答案 0 :(得分:1)

现在,我解决了我的问题: 首先,我更改了链接:从locale到_locale

<div class="col-sm-3 language-switcher">
    <a href="{{ path('ngs_locale', {_locale: 'en'}) }}">English</a> |
    <a href="{{ path('ngs_locale', {_locale: 'km'}) }}">ខ្មែរ</a>
</div>

其次,我将routing.yml,locale更改为_locale

ngs_locale:
    path:     locale/{_locale}
    defaults: { _controller: NGSHomeBundle:Locale:locale 

第三,我更改了LocaleController.php。删除$ locale参数,然后通过$ request-&gt; getLocale()获取$ locale;

public function localeAction(Request $request)
{
    $locale = $locale = $request->getLocale();
    $request->getSession()->set('_locale', $locale);

    $referer = $request->headers->get('referer');
    if (empty($referer)) {
        throw $this->createNotFoundException('ngs_not_found');
    }

    return $this->redirect($referer);
}

第四,我为HomeBundle DependencyInjection \ Configuration.php和DependencyInjection \ NGSHomeExtension.php添加了DependencyInjection

就是这样!