php 如何在 symfony 2.3 中获取当前语言环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18949397/
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 18:32:57  来源:igfitidea点击:

how to get the current locale in symfony 2.3?

phpsymfony

提问by Seriescw Esxille

How can I get the curent locale in Symfony 2.3 ?

我怎样才能在 Symfony 2.3 中获得当前的语言环境?

I have a url like this: /{_locale}/blog/articlewith FR as a default parameter. I try the following: $this->get('request')->getLocale()

我有一个这样的网址: /{_locale}/blog/article以 FR 作为默认参数。我尝试以下操作: $this->get('request')->getLocale()

but it always give me the default parameter.And i am aware that i can use $this->get('session')->set('_locale', 'fr');

但它总是给我默认参数。我知道我可以使用 $this->get('session')->set('_locale', 'fr');

but the problem is that when the user first visit my website he has nothing stored in his session.

但问题是,当用户第一次访问我的网站时,他的会话中没有存储任何内容。

回答by Azam Alvi

You can get current locale by this

您可以通过此获取当前语言环境

 $request = $this->get('request');
 echo $request->getLocale();

回答by AlexioVay

Simply use $request->getLocale();in Symfony 4 since this seems to the top link when you search in Google for this topic.

只需$request->getLocale();在 Symfony 4 中使用,因为当您在 Google 中搜索此主题时,这似乎是顶部链接。