Django:如何向应用程序添加中文支持 [英] Django: How to add Chinese support to the application

查看:341
本文介绍了Django:如何向应用程序添加中文支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为在Django中编写的应用程序添加一个中文语言,而且我真的很难过。我花了半天时间尝试不同的方法,没有成功。

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.

我的应用程序支持几种语言,这是 settings.py 文件的一部分:

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

在每个目录中,我有LC_MESSAGES目录,带* .mo和* .po文件。
* .po文件由Python编写的脚本创建,它将* .ODS转换为文本文件。
* .mo文件由 python manage.py compilemessages 命令创建。

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不加载中文翻译。那就是问题。简化和传统都不行。我在settings.py和locale目录中尝试了不同版本的语言和区域设置代码:zh-CN,zh-cn,zh_CN,zh_cn。没有成功。

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.

也许我犯了一个简单的错误?我添加了波兰语只是为了测试,一切顺利。基本上我也是这样做的。我已经添加('pl',u'Polish')元组到settings.py和locale / pl与* .po和* .mo和LC_MESSAGES目录...

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?

推荐答案

您需要使用小写字母为您的语言环境语言代码它正常工作即使用

You will need to use lower case for your locale language codes for it to work properly. i.e. use

LANGUAGES = (
    ('zh-cn', u'简体中文'), # instead of 'zh-CN'
    ('zh-tw', u'繁體中文'), # instead of 'zh-TW'
)

请参阅 https://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py 。你会看到它使用 zh-tw 而不是 zh-TW

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 of zh-TW.

最后,在本地文件夹中存储* .po和* .mo文件的语言目录需要命名为 zh_CN zh_TW 分别为翻译正常工作。

Finally, the language directories storing the *.po and *.mo files in your locales folder needs to be named zh_CN and zh_TW respectively for the translations to work properly.

这篇关于Django:如何向应用程序添加中文支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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