0

我正在 Django 中做我的第一步,并试图让我通过 .ini 文件传递​​给应用程序的文本翻译工作。

说我init.ini的是:

[test]
GREETING = Hello World

在我的 中settings.py,我正在做:

from six.moves import configparser
from django.utils.translation import ugettext_lazy as _

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Retrieve initialization configuration
init_parser = configparser.ConfigParser()
init_parser.read(os.path.join(BASE_DIR, 'init.ini'))

...
HELLO = init_parser.get('test', 'GREETING')
HELLO = _(init_parser.get('test', 'GREETING'))

调用时我的标签翻译不显示makemessage

文件说_

(与前面两个例子一样,使用变量或计算值的警告是 Django 的翻译字符串检测实用程序 django-admin makemessages 将无法找到这些字符串。稍后会详细介绍 makemessages。)

但是它们虽然makemessage在文档页面上有很多介绍,但它没有提供关于如何翻译变量/计算值的解决方案。因此我的问题是:

是否有任何推荐的实用方法将字符串作为变量传递到 python 模块并makemessage捕获它们?

编辑
添加扩展名django-admin makemessages -l de --e=html,py,txt,ini也不起作用,但现在我很好奇如何制作一个txt将被覆盖的文件。也许这是一个想法。

4

1 回答 1

0

所以这django-admin makemessages -l de --e=html,py,txt,ini让我走上了正确的轨道,因为如果按照文档txt默认包含,它必须以某种方式工作。

我创建了foo.txt

_("yeah why not")
{% trans "maybe like this" %}

第二个片段.po在调用时出现在文件中makemessages

我创建了foo.ini

_("from ini, yeah why not")
{% trans "from ini, maybe like this" %}

并且运行也django-admin makemessages -l de --e=html,py,txt,ini包括文件中的第二个片段.po

所以{% trans "" %} -ifying 初始化文件中的所有文本片段似乎可以完成这项工作。然后我可以从我的模块中获取翻译后的值。

于 2018-07-01T18:27:23.380 回答