php 更改语言环境 symfony 2.3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17243426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:34:19  来源:igfitidea点击:

change locale symfony 2.3

phpsymfonylocale

提问by Ajouve

I just began with symfony I'm trying to build a multilang website but I have a problem to change the locale

我刚开始使用 symfony 我正在尝试建立一个多语言网站,但我在更改语言环境时遇到了问题

I read some posts and I read the documentation about this but the locale don't change, I try:

我阅读了一些帖子并阅读了有关此的文档,但语言环境没有改变,我尝试:

public function indexAction()
{    
    $this->get('session')->set('_locale', 'fr');

    $request = $this->getRequest();
    $locale = $request->getLocale();
    return $this->render('PhoneMainBundle:Default:index.html.twig',array('locale'=>$locale));
}

but the value in $locale is always 'en' (my default locale)

但 $locale 中的值始终为“en”(我的默认语言环境)

I also try

我也试试

public function indexAction()
{    
    $this->get('session')->set('_locale', 'fr');

    $request = $this->getRequest();
    $request->setLocale('fr');
    $locale = $request->getLocale();

    return $this->render('PhoneMainBundle:Default:index.html.twig',array('locale'=>$locale));
}

In this case $locale is fr but the translations are always from messages.en.yml

在这种情况下 $locale 是 fr 但翻译总是来自 messages.en.yml

I'd like in a first time to detect the user locale using $_SERVER['HTTP_ACCEPT_LANGUAGE'], maybe using a listner on each page actualisation ?

我想第一次使用 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 检测用户语言环境,也许在每个页面实现上使用一个监听器?

and after I will create a route to change the locale

然后我将创建一条路线来更改语言环境

But I 'd like to find a way to change the locale.

但我想找到一种方法来更改语言环境。

Thanks for your help

谢谢你的帮助

回答by Hast

Based on thisand thisanswers.

基于这个这个答案。

LanguageListener.php:

语言监听器.php:

<?php

namespace Acme\UserBundle\EventListener;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class LanguageListener
{
    private $session;

    public function setSession(Session $session)
    {
        $this->session = $session;
    }

    public function setLocale(GetResponseEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
            return;
        }

        $request = $event->getRequest();
        $request->setLocale($request->getPreferredLanguage(array('en', 'de')));

    }
}

services.yml:

服务.yml:

acme.language.kernel_request_listener:
    class: Acme\UserBundle\EventListener\LanguageListener
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: setLocale }

About wrong locale detection in twig, there could be a lot of different causes. Search through the SO, you'll definitely find the answer. Make sure that your '_local' var is defined right, make sure that you put your languages files in the right place, etc. FInally, read again the last version of the documentation: http://symfony.com/doc/current/book/translation.html

关于树枝中错误的区域设置检测,可能有很多不同的原因。搜索SO,您一定会找到答案。确保您的“_local”变量定义正确,确保您将语言文件放在正确的位置等。最后,再次阅读文档的最新版本:http://symfony.com/doc/current/书/translation.html

回答by WickStargazer

I however added this to make it more dynamic

但是,我添加了它以使其更具动态性

services.yml

服务.yml

 services:
        acme.language.kernel_request_listener:
            class: Acme\UserBundle\EventListener\LanguageListener
            tags:
                - { name: kernel.event_listener, event: kernel.request, method: setLocale }
            arguments: [ @router, @service_container ]

LanguageListener.php:

语言监听器.php:

<?php

namespace Acme\UserBundle\EventListener;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class LanguageListener
{
     private $session;
     private $container;
     private $router;

    public function __construct($router, $container)
    {
        // ...
        $this->router= $router;
        $this->container = $container;
    }

    public function setSession(Session $session)
    {
        $this->session = $session;
    }

    public function setLocale(GetResponseEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
            return;
        }

        $request = $event->getRequest();    
        $request->setLocale($request->getPreferredLanguage($this->container->parameters['jms_i18n_routing.locales']));

    }
}

Just to be able to get the parameters and values from config.yml.

只是为了能够从 config.yml 获取参数和值。

Regards, Wick

问候,威克

回答by Remy Mellet

If for example your default locale is french, except for one controller you want to have the default locale set to english can do that:

例如,如果您的默认语言环境是法语,除了一个控制器,您希望将默认语言环境设置为英语可以这样做:

routing.yml

路由.yml

desktop_comingsoonpage:
    resource: "@RemmelComparabusBundle/Controller/ComingsoonpageController.php"
    defaults: { _locale: en }
    type:     annotation

more info : Symfony doc

更多信息:Symfony 文档