如何在Django中维护相同语言的不同国家/地区版本?
我想在Django中使用相同语言的几种不同版本,针对不同国家/地区进行自定义(例如locale/en
,locale/en_CA
,locale/en_US
等).如果没有特定国家/地区的语言,我希望使用默认语言版本(locale/en
)).
I would like to have a few different versions of the same language in Django, customized for different countries (e.g. locale/en
, locale/en_CA
, locale/en_US
, etc.). If there is no language for specific country I would expect to use the default language version (locale/en
)).
然后在每个站点的设置文件中,我指定LANGUAGE_CODE
和LANGUAGES
.
Then in the settings file for each site I specify LANGUAGE_CODE
and LANGUAGES
.
由于某些原因,即使我指定了以下设置,仍会使用locale/en_US
转换:
For some reason, even if I specify the following settings, the locale/en_US
translations are still used:
LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', ugettext('English')),
)
尽管我明确指定语言代码应为en
(而不是en-us
).
Though I clearly specify that the language code should be en
(not en-us
).
我错过了什么吗?已经尝试在多个地方找到答案,包括Django文档.
Am I missing something? Already tried to find the answer in multiple places, including Django documentation.
该问题的一种解决方法是将以下代码段添加到您的settings.py
文件中.
A workaround to the issue would be to add following snippet to your settings.py
file.
import locale
locale.locale_alias.pop('en', None)
特别感谢Venelin Stoykov,他能够研究Python locale
模块的行为.
Special credit to Venelin Stoykov who was able to investigate the behavior of the Python locale
module.