使用symfony 2.1更改语言环境

时间:2023-01-28 22:40:07

Having some issue changing the locale on a symfony 2.1 website.

在symfony 2.1网站上更改语言环境有问题。

I can't find a way to be able to change the lang without using the _locale on every routes. I know this is against the fundamental rule, but this content will for example not be indexed by engine as it is member only.

我无法找到一种方法,可以在每条路由上使用_locale来更改lang。我知道这是违反基本规则的,但是这个内容不会被引擎索引,因为它只是成员。

Typically, I would like a simple method to be able to change the locale on the request (BC from version 2.1), or on the session, but can't figure out how to do that smoothly. I also would like to avoid the use of a Listener for that.

通常,我希望一个简单的方法能够在请求上(从版本2.1)更改语言环境,或者在会话中更改,但不知道如何顺利进行。我也想避免使用监听器。

config.yml file :

配置。yml文件:

framework:
    translator:      { fallback: %locale% }
    session:

routing.yml file :

路由。yml文件:

route_change_lang:
    pattern:   /changelang/{newlang}
    defaults:  { _controller: AcmeCoreBundle:Default:switchLanguage, newlang: en }
    requirements:
        newlang: en|fr|de

Simple action to update the locale of the router :

更新路由器区域设置的简单操作:

public function switchLanguageAction($newlang)
{

    $request = $this->getRequest();

    $request->setLocale($newlang);

    $referer_url = $this->get('request')->headers->get('referer');
    if ($referer_url != null) {
        return $this->redirect($referer_url);
    } else {
        return $this->redirect($this->generateUrl('route_home'));
    }
}

What is the problem? I guess it is related to the default_locale set in the main config.yml file, but documentation is not really clear, any help/hint appreciated

这个问题是什么?我猜它与主配置中的default_locale集有关。yml文件,但文件不是很清楚,请帮忙

3 个解决方案

#1


1  

I've come across the same problem, since we cant' use locales in our urls (seo-issues). Also we use locales like en_US and those are stored in a config outside the direct framework access. What I did is registering an event listener and hooking into the onKernelRequest event. There I check if locale is set in session, if not, I add it to both, request and session. This way, the framework keeps on behaving like it did before 2.1 If you need more info on how to do this, comment and I'll edit some exaples in here :-)

我遇到了同样的问题,因为我们不能在我们的url (seo-issue)中使用locale。我们也使用像en_US这样的locale,它们存储在直接框架访问之外的配置中。我所做的是注册一个事件监听器并连接到onKernelRequest事件。在那里,我检查是否在会话中设置区域设置,如果没有设置,我将它添加到两个请求和会话中。这样,框架就像在2.1之前一样,如果你需要更多的信息来做这个,评论,我将编辑一些exaples在这里:-)

#2


0  

Restore the old behavior as explain in https://github.com/symfony/symfony/blob/master/UPGRADE-2.1.md#httpfoundation-1 And use the piece of code of Carlos Granados.

恢复旧的行为,如https://github.com/symfony/symfony/blob/master/UPGRADE-2.1.md# httpfoundations -1中解释的那样,并使用Carlos Granados的代码片段。

You can also read my another answer https://*.com/a/12952999/520114

您也可以阅读我的另一个答案https://*.com/a/12952999/520114

#3


-1  

If you set the locale in the request, this is just used for the current request. The next time that a request is issued, the default_locale will be used. Even if now (2.1) the locale is set in the request instead of the session, "It is also possible to store the locale in the session instead of on a per request basis. If you do this, each subsequent request will have this locale." (from the docs). So, you need to do:

如果您在请求中设置了语言环境,这只用于当前请求。下次发出请求时,将使用default_locale。即使现在(2.1)语言环境是在请求中而不是会话中设置的,“也可以将语言环境存储在会话中,而不是基于每个请求。如果这样做,每个后续请求都将具有此语言环境。(文档)。所以,你需要做的是:

$this->get('session')->set('_locale', $newlang);

#1


1  

I've come across the same problem, since we cant' use locales in our urls (seo-issues). Also we use locales like en_US and those are stored in a config outside the direct framework access. What I did is registering an event listener and hooking into the onKernelRequest event. There I check if locale is set in session, if not, I add it to both, request and session. This way, the framework keeps on behaving like it did before 2.1 If you need more info on how to do this, comment and I'll edit some exaples in here :-)

我遇到了同样的问题,因为我们不能在我们的url (seo-issue)中使用locale。我们也使用像en_US这样的locale,它们存储在直接框架访问之外的配置中。我所做的是注册一个事件监听器并连接到onKernelRequest事件。在那里,我检查是否在会话中设置区域设置,如果没有设置,我将它添加到两个请求和会话中。这样,框架就像在2.1之前一样,如果你需要更多的信息来做这个,评论,我将编辑一些exaples在这里:-)

#2


0  

Restore the old behavior as explain in https://github.com/symfony/symfony/blob/master/UPGRADE-2.1.md#httpfoundation-1 And use the piece of code of Carlos Granados.

恢复旧的行为,如https://github.com/symfony/symfony/blob/master/UPGRADE-2.1.md# httpfoundations -1中解释的那样,并使用Carlos Granados的代码片段。

You can also read my another answer https://*.com/a/12952999/520114

您也可以阅读我的另一个答案https://*.com/a/12952999/520114

#3


-1  

If you set the locale in the request, this is just used for the current request. The next time that a request is issued, the default_locale will be used. Even if now (2.1) the locale is set in the request instead of the session, "It is also possible to store the locale in the session instead of on a per request basis. If you do this, each subsequent request will have this locale." (from the docs). So, you need to do:

如果您在请求中设置了语言环境,这只用于当前请求。下次发出请求时,将使用default_locale。即使现在(2.1)语言环境是在请求中而不是会话中设置的,“也可以将语言环境存储在会话中,而不是基于每个请求。如果这样做,每个后续请求都将具有此语言环境。(文档)。所以,你需要做的是:

$this->get('session')->set('_locale', $newlang);