简体   繁体   English

Django makemessages未检测到可翻译字符串

[英]Django makemessages not detecting translatable strings

In an app 'purchase', there is a file reports.py. 在应用程序“购买”中,有一个reports.py文件。 In this app, I'm generating some pdf-reports that sometimes I want to translate to the Czech language. 在此应用程序中,我生成了一些pdf报告,有时我想将其翻译成捷克语。 Unfortunately the makemessages script doesn't detect the translatable strings. 不幸的是,makemessages脚本无法检测可翻译的字符串。

Running the following shows that reports.py is being processed. 运行以下命令表明正在处理reports.py。 But no files are added. 但是没有添加文件。

.././manage.py makemessages -l cs_CZ -v 2
processing file reports.py in .
processing locale cs_CZ

This is a snippet from the reports.py file: 这是reports.py文件的摘录:

from django.utils.translation import ugettext as trans

def purchase_order_report(purchase_order, language='en'):
    ...
    doc.add_text(trans('Items requested'), 'Heading2')
    ...

The Locale folder is setup like this: 语言环境文件夹的设置如下:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, "locale"),
)

Edit: It seems that django makemessages is not correctly translating the import. 编辑:似乎django makemessages无法正确翻译导入。

Running the following works (but may conflict when unpacking variables to _): 运行以下工作(但是将变量解压到_时可能会发生冲突):

from django.utils.translation import ugettext as _

def purchase_order_report(purchase_order, language='en'):
    ...
    doc.add_text(_('Items requested'), 'Heading2')
    ...

Running the following works as well (but is not very handy if you want to mix and match ugettext and ugettext_lazy): 运行以下命令也可以(但如果要混合使用ugettext和ugettext_lazy则不方便):

from django.utils.translation import ugettext

def purchase_order_report(purchase_order, language='en'):
    ...
    doc.add_text(ugettext('Items requested'), 'Heading2')
    ...

Any thoughts? 有什么想法吗?

Apps should have a locale dir of their own. 应用程序应具有自己的locale目录。 But it's not created if it does not exist. 但是如果不存在,则不会创建。 Create the directory within the app and then it should work. 在应用程序中创建目录,然后它应该可以工作。

The xgettext GNU command accepts a --keyword=KEYWORD option in order to detect additionnal keywords. xgettext GNU命令接受--keyword=KEYWORD选项,以便检测附加关键字。

I tried the following command in my shell and the result was as expected : 我在shell中尝试了以下命令,结果与预期的一样:

xgettext -o - --from-code=UTF-8 -L Python -d django -ktrans reports.py

Sadly, it seems that there is not any way to pass this option through the makemessages Django (2.1) command yet :'( 可悲的是,似乎还没有任何办法可以通过makemessages Django(2.1)命令传递此选项:
The keywords are hardcoded in the django/core/management/commands/makemessage.py file (around line 570). 关键字被硬编码在django/core/management/commands/makemessage.py文件中(约570行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM