Python Django 中的 makemessages 无法识别 ugettext 和 ugettext_lazy 函数

2022-01-18 00:00:00 python django internationalization

问题描述

我正在使用 Django 1.5.1,并且在翻译时遇到了一些奇怪的行为".我在同一个 Python 文件中使用 ugettextugettext_lazy.如果我将导入组织为:

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

我的结论是:ugettextugettext_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))

(可以在 这里找到源代码).因此,一些关键字是由 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

关键字 trans 不在这些关键字集中,因此您不应使用它来标记要翻译的文本.

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

相关文章