Django:如何为应用程序添加中文支持

发布于 2024-12-09 16:15:29 字数 1078 浏览 0 评论 0原文

我正在尝试将中文添加到我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人疚 2024-12-16 16:15:29

您需要使用小写字母作为区域设置语言代码才能正常工作。即使用

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

最后,您的 locales 文件夹中存储 *.po 和 *.mo 文件的语言目录需要分别命名为 zh_CNzh_TW 才能使翻译正常工作。

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'
)

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.

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.

り繁华旳梦境 2024-12-16 16:15:29

对于 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

为了彻底,这里是方法:

def to_locale(language, to_lower=False):
    """
    Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
    True, the last component is lower-cased (en_us).
    """
    p = language.find('-')
    if p >= 0:
        if to_lower:
            return language[:p].lower() + '_' + language[p + 1:].lower()
        else:
            # Get correct locale for sr-latn
            if len(language[p + 1:]) > 2:
                return language[:p].lower() + '_' + language[p + 1].upper() + language[p + 2:].lower()
            return language[:p].lower() + '_' + language[p + 1:].upper()
    else:
return language.lower()

注意 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 named zh_Hans.

And for traditional Chinese:

zh-hant in your config, and your directory should be named zh_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/locale

Basically, 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:

def to_locale(language, to_lower=False):
    """
    Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
    True, the last component is lower-cased (en_us).
    """
    p = language.find('-')
    if p >= 0:
        if to_lower:
            return language[:p].lower() + '_' + language[p + 1:].lower()
        else:
            # Get correct locale for sr-latn
            if len(language[p + 1:]) > 2:
                return language[:p].lower() + '_' + language[p + 1].upper() + language[p + 2:].lower()
            return language[:p].lower() + '_' + language[p + 1:].upper()
    else:
return language.lower()

Note that en-us is turned into en_US, based on the source code above, because us is 2 characters or less.

爱你不解释 2024-12-16 16:15:29

我遇到了同样的问题[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.

属性 2024-12-16 16:15:29

不确定您稍后是否能够解决此问题,但问题出在语言环境目录中的语言目录名称上。所有短代码中带有破折号的语言都会发生这种情况。解决办法是重命名中文目录,将破折号替换为下划线:

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.

穿透光 2024-12-16 16:15:29

在 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文