Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 2,切换语言不起作用_Php_Symfony_Translation - Fatal编程技术网

Php Symfony 2,切换语言不起作用

Php Symfony 2,切换语言不起作用,php,symfony,translation,Php,Symfony,Translation,我想知道为什么当我点击视图上的链接切换语言时它不起作用。如果我将默认语言环境设置为en或km,它将分别显示英语或高棉语。我有什么问题,我不能点击下面的链接切换语言吗?请帮帮我!非常感谢您的回答 在视图中 <div class="col-sm-3 language-switcher"> <a href="{{ path('ngs_locale', {locale: 'en'}) }}">English</a> | <a href="{{ p

我想知道为什么当我点击视图上的链接切换语言时它不起作用。如果我将默认语言环境设置为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>
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 }
现在,我解决了我的问题: 首先,我更改了链接:从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>
ngs_locale:
    path:     locale/{_locale}
    defaults: { _controller: NGSHomeBundle:Locale:locale 
第三,我更改了LocaleController.php。删除了$locale参数,然后通过$request->getLocale()获取$locale

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


就这样

这是用于粘性区域的吗?你不需要在路由文件上设置你的要求吗?我不确定,我会试试。我像下面那样试过,但它还不起作用
ngs_locale:path:locale/{locale}默认值:{u控制器:NGSHomeBundle:locale:locale}要求:{u locale:en | km
您得到的错误是什么?你能用这个错误更新你的问题吗?或者没有错误,只是它不会更改会话中的_语言环境?没有错误显示,只是不会更改会话中的_语言环境
ngs_locale:
    path:     locale/{_locale}
    defaults: { _controller: NGSHomeBundle:Locale: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);
}