ugettext和ugettext_lazy函数在Python Django中不被makemessages识别 [英] ugettext and ugettext_lazy functions not recognized by makemessages in Python Django

查看:1529
本文介绍了ugettext和ugettext_lazy函数在Python Django中不被makemessages识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和Django 1.5.1一起工作,我正在翻译一些奇怪的行为。我在同一个Python文件中使用 ugettext ugettext_lazy 。如果我从django.utils组织导入为

I'm working with Django 1.5.1 and I'm experiencing some "strange behaviour" with translations. I'm using ugettext and ugettext_lazy in the same Python file. If I organize the imports as:

from django.utils.translation import ugettext as trans
from django.utils.translation import ugettext_lazy as _

from django.utils.translation import ugettext as trans, ugettext_lazy as _

当运行 makemessages 命令时,标记为 trans(string)的字符串将被跳过。

The strings marked as trans("string") are skipped when running makemessages command.

但是,如果我不重命名 ugettext ,这两个版本都可以正常工作:

However, if I don't rename the ugettext it works well with both versions:

from django.utils.translation import ugettext
from django.utils.translation import ugettext_lazy as _

from django.utils.translation import ugettext, ugettext_lazy as _

现在 trans(string)工作得很好。

有谁知道为什么导入重命名导致重命名的函数不被调用?这是一个实际的Python限制我不知道什么时候重命名同一个模块中的多个功能?

So, does anybody know why this import renaming is causing the renamed function not to be called? Is this an actual Python "limitation" I didn't know when renaming more than one function inside the same module?

更新

经过一些测试,我意识到即使在应用程序中创建一个空的python模块,使用以下代码也不起作用:

After some testing, I've realized that even creating an empty python module inside an app with the following code it doesn't work:

from django.utils.translation import ugettext_lazy as translate

a = translate("string")

但是,如果使用 _ 为其别名起作用:

However, if using _ for the alias it works:

from django.utils.translation import ugettext_lazy as _

a = _("string")

我的结论是:您只能使用 _ 别名 ugettext ugettext_lazy (或任何其他相关的翻译功能)在Django中,否则它将不被 makemessages 命令识别。技术说明可以在Robert Lujo的回答中找到。

My conclusion is: You can only use the _ alias for ugettext and ugettext_lazy (or any other related translation function) in Django or else it won't be recognized by makemessages command. The technical explanation can be found in Robert Lujo's answer.

谢谢!

推荐答案

Django命令实用程序makemessages内部调用 xgettext 程序如下:

Django command utility makemessages internally calls xgettext program like this:

cmd = (
    'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
    '--keyword=ugettext_noop --keyword=ugettext_lazy '
    '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
    '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
    '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
    '--add-comments=Translators -o - "%s"' %
    (domain, wrap, location, work_file))

(源可以找到 here )。所以,一些关键字是由 xgettext 实用程序(检查--keyword的参考):

(source can be found here). So, some keywords are predefined by the xgettext utility (check reference for --keyword):


  • 为python - gettext,ugettext,dgettext,ngettext,ungettext,dngettext,_

,其中一些由django实用程序添加:

and some are added by django utility:


  • gettext_lazy,ngettext_lazy,ugettext_noop,ugettext_lazy,ungettext_lazy,pgettext,npgettext,pgettext_lazy,npgettext_lazy

关键字 strong>不属于这些关键字集合,因此您不应将其用于标记翻译文本。

Keyword trans is not in any of these keyword sets, so you should not use it for marking texts for translations.

这篇关于ugettext和ugettext_lazy函数在Python Django中不被makemessages识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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