Django:如何为应用程序添加中文支持
我正在尝试将中文添加到我用 Django 编写的应用程序中,但我确实遇到了困难。我花了半天时间尝试了不同的方法,但没有成功。
我的应用程序支持几种语言,这是 settings.py 文件的一部分:
TIME_ZONE = 'Europe/Dublin'
LANGUAGE_CODE = 'en'
LOCALES = (
#English
('en', u'English'),
#Norwegian
('no', u'Norsk'),
#Finish
('fi', u'Suomi'),
#Simplified Chinese
('zh-CN', u'简体中文'),
#Traditional Chinese
('zh-TW', u'繁體中文'),
#Japanese
('ja', u'日本語'),
)
目前所有(除了中文)语言都可以完美运行。这是语言环境目录的内容:
$ ls locale/
en
fi
ja
no
zh_CN
zh_TW
在每个目录中,我都有 LC_MESSAGES 目录,其中包含 *.mo 和 *.po 文件。 *.po 文件是由 Python 编写的脚本创建的,该脚本将 *.ODS 转换为文本文件。 *.mo 文件是由 python manage.pycompilemessages 命令创建的。
用户可以从我的应用程序的“首选项”部分中的正确表单中选择语言。
Django 不加载中文翻译。这就是问题所在。简体和繁体都不起作用。我在 settings.py 和 locale 目录中尝试了不同的语言和区域设置代码变体:zh-CN、zh-cn、zh_CN、zh_cn。没有成功。
也许我犯了一个简单的错误?我添加了波兰语只是为了测试,一切都很顺利。基本上我也做了同样的事情。我已将 ('pl', u'Polish') 元组添加到 settings.py 和“locale/pl”,其中包含 *.po 和 *.mo 以及 LC_MESSAGES 目录...
您知道可能出了什么问题吗?
I am trying to add a Chinese language to my application written in Django and I have a really hard time with that. I have spent half a day trying different approaches, no success.
My application supports few languages, this is part of settings.py file:
TIME_ZONE = 'Europe/Dublin'
LANGUAGE_CODE = 'en'
LOCALES = (
#English
('en', u'English'),
#Norwegian
('no', u'Norsk'),
#Finish
('fi', u'Suomi'),
#Simplified Chinese
('zh-CN', u'简体中文'),
#Traditional Chinese
('zh-TW', u'繁體中文'),
#Japanese
('ja', u'日本語'),
)
At the moment all (but Chinese) languages work perfectly. This is a content of locale directory:
$ ls locale/
en
fi
ja
no
zh_CN
zh_TW
In every directory I have LC_MESSAGES directory with *.mo and *.po files.
*.po files are created by script written in Python, which converts *.ODS to a text file.
*.mo files are created by python manage.py compilemessages command.
Language can be selected by user from the proper form in "Preferences" section in my application.
Django does not load Chinese translation. That is the problem. Both simplified and traditional does not work. I have tried different variations of language and locale codes in settings.py and in locale directory: zh-CN, zh-cn, zh_CN, zh_cn. No success.
Perhaps I made a simple mistake? I have added Polish language just for test and everything went fine. Basically I did the same. I have added ('pl', u'Polish') tuple to the settings.py and "locale/pl" with *.po and *.mo and LC_MESSAGES directory...
Do you know what might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用小写字母作为区域设置语言代码才能正常工作。即使用
查看 https://code.djangoproject.com/browser/ 中指定的语言代码django/trunk/django/conf/global_settings.py。您将看到它使用
zh-tw
而不是zh-TW
。最后,您的 locales 文件夹中存储 *.po 和 *.mo 文件的语言目录需要分别命名为
zh_CN
和zh_TW
才能使翻译正常工作。You will need to use lower case for your locale language codes for it to work properly. i.e. use
See the language codes specified in https://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py. You will see that it uses
zh-tw
instead ofzh-TW
.Finally, the language directories storing the *.po and *.mo files in your locales folder needs to be named
zh_CN
andzh_TW
respectively for the translations to work properly.对于 Django 1.7 及更高版本,您需要
在配置中执行以下操作:
zh-hans
,并确保您的目录名为zh_Hans
。对于繁体中文:
配置中的
zh-hant
,并且您的目录应命名为zh_Hant
。以下是
auth
贡献者包如何在语言环境目录中布置其翻译的示例:https://github.com/django/django/tree/master/django/contrib/auth/locale基本上,对于我们的中文来说代码中,
-
替换为_
,第二个作品的第一个字母大写。此逻辑的源代码位于: https://github.com/django/django/blob/7a42cfcfdc94c1e7cd653f3140b9eb30492bae4f/django/utils/translation/init.py#L272-L285
为了彻底,这里是方法:
注意
en-us
变成了en_US
,基于上面的源代码,因为us
是2个字符或更少。For Django 1.7 and up, you'll need to do the following:
zh-hans
in your config, and make sure that your directory is namedzh_Hans
.And for traditional Chinese:
zh-hant
in your config, and your directory should be namedzh_Hant
.Here is an example of how the
auth
contributor package lays out its translations in the locale directory: https://github.com/django/django/tree/master/django/contrib/auth/localeBasically, for our Chinese language codes, the
-
is replaced with a_
, and the first letter of the second work is capitalized.Source code for this logic is here: https://github.com/django/django/blob/7a42cfcfdc94c1e7cd653f3140b9eb30492bae4f/django/utils/translation/init.py#L272-L285
and just to be thorough, here is the method:
Note that
en-us
is turned intoen_US
, based on the source code above, becauseus
is 2 characters or less.我遇到了同样的问题[Django-1.6.1],并通过将中文的语言环境目录从 zh-cn (或 zh_cn)重命名为 zh_CN (下划线和大写 CN)来解决它。
奇怪的是,Django 需要一个“zh-cn”作为 LANGUAGE_CODE 或分别带有 i18n_pattern 的 url。
顺便说一句,没有记录的好东西。
希望有帮助。
I had the same problem [Django-1.6.1] and solved it by renaming the locale directories for Chinese from zh-cn (or zh_cn) to zh_CN (underscore and uppercase CN).
Strangely enough Django requires a "zh-cn" as LANGUAGE_CODE or url with an i18n_pattern respectively.
Non-documented goodness btw.
Hope that helps.
不确定您稍后是否能够解决此问题,但问题出在语言环境目录中的语言目录名称上。所有短代码中带有破折号的语言都会发生这种情况。解决办法是重命名中文目录,将破折号替换为下划线:
zh-cn --> zh_cn
zh-tw --> zh_tw
希望这有帮助。
Not sure if you were able to resolve this later, but the problem is with the language directory names in the locale directory. This happens with all languages with a dash in their short-code. The solution is to rename the Chinese directories by replacing the dashes with underscores:
zh-cn --> zh_cn
zh-tw --> zh_tw
Hope this helps.
在 .po 文件中,在“Language: \n”中添加 zh-cn 或 zh-tw,即变为“Language: zh-en\n”或“Language: zh-tw\n”
再次编译消息并运行服务器。
In .po file, add zh-cn or zh-tw in "Language: \n", which becomes "Language: zh-en\n" or "Language: zh-tw\n"
Compile the messages and runserver again.