php 更改 Symfony2 中的默认语言环境

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

Change default locale in Symfony2

phpsymfony

提问by gilden

I'm trying to change the default locale of my application. Things I've tried so far:

我正在尝试更改应用程序的默认语言环境。到目前为止我尝试过的事情:

  • set intl.default_locale to 'et_EE'
  • set locale to 'et' in app/config/parameters.ini
  • Changed the default locale in my bundle's boot() method described here
  • Implemented a class Locale that extends StubLocale and overwrites method getDefault() to return 'et_EE'.
  • 将 intl.default_locale 设置为“et_EE”
  • 将语言环境设置为“et” app/config/parameters.ini
  • 更改了此处描述的我的包的 boot() 方法中的默认语言环境
  • 实现了一个扩展 StubLocale 并覆盖方法 getDefault() 以返回“et_EE”的 Locale 类。

Here is the implementation. The Locale class does not seem to be getting overwritten as calling \Locale::getDefault()doesn't execute this method.

这是实现。Locale 类似乎没有被覆盖,因为调用\Locale::getDefault()不执行此方法。

<?php

use Symfony\Component\Locale\Stub\StubLocale;

class Locale extends StubLocale
{
    static public function getDefault()
    {
        return 'et_EE';
    }
}

After trying all these methods described, \Locale::getDefault()still returns en. I need it to return et_EEto render form widgets, such as country or language, in the proper locale.

在尝试了所描述的所有这些方法后,\Locale::getDefault()仍然返回en. 我需要它返回et_EE以在适当的语言环境中呈现表单小部件,例如国家/地区或语言。

How would I go doing this? Being able to support multiple locales later would also be great. Thanks.

我该怎么做呢?以后能够支持多个语言环境也很棒。谢谢。

回答by Flask

In Symfony 2.0:

在 Symfony 2.0 中:

# app/config/config.yml
framework:
  session: { default_locale: en }

In Symfony 2.1+:

在 Symfony 2.1+ 中:

# app/config/config.yml
framework:
  default_locale: en

回答by Luciano César Natale

In Symfony 2.0, you can set default_localefor the session too:

在 Symfony 2.0 中,您也可以设置default_locale会话:

framework:
  translator:      { fallback: %locale% }
  ...
  session:
    default_locale: %locale%
    auto_start:     true

The %locale%is a variable, and it's resolved from the parameters.inifile.

%locale%是一个变量,它是从解决parameters.ini文件。