使用symfony 2.1更改语言环境 [英] Changing locale with symfony 2.1

查看:116
本文介绍了使用symfony 2.1更改语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在symfony 2.1网站上更改语言环境存在一些问题.

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

我找不到不在每条路线上都使用_locale即可更改lang的方法.我知道这违反了基本规则,但这内容例如,将不会被引擎索引,因为它只是成员.

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.

通常,我希望有一个简单的方法可以更改请求(2.1版的BC)或会话中的语言环境,但无法弄清楚如何做到这一点.我也想避免为此使用监听器.

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.

config.yml文件:

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

routing.yml文件:

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

简单的操作即可更新路由器的语言环境:

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'));
    }
}

出什么问题了?我想这与在config.yml主文件中设置的default_locale有关,但是文档并不清楚,任何帮助/提示都值得赞赏

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

推荐答案

我遇到了同样的问题,因为我们无法在网址(seo问题)中使用语言环境.此外,我们使用en_US之类的语言环境,这些语言环境存储在直接框架访问之外的配置中. 我所做的是注册一个事件侦听器,并挂接到onKernelRequest事件中.在那里,我检查会话中是否设置了语言环境,如果没有,则将其添加到请求和会话中. 这样,框架就可以像2.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 :-)

这篇关于使用symfony 2.1更改语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆