使用 Babel:如何保护 GNU gettext PO 文件中的译者注释(和旧翻译)?

发布于 2024-09-03 06:34:54 字数 1638 浏览 4 评论 0 原文

使用基于 Python 的 Babel gettext 实用程序,是否有任何技术可以保留译者注释和旧的(“过时的”) ) 跨 .pot 文件更新的 .po 文件中的翻译(用 #~ 标记)?

过时的翻译第一次位于 .po 文件中,并且 pybabel update 运行时,翻译会被标记为 #~。这样一来,一方面,它被视为注释,直到译者检查并更改它后才使用,但另一方面,它不会被删除,因此译者可以参考它,或复制文本从它到他们的其他翻译。

但是,下次运行 pybabel update 时,所有注释都会从文件中永久删除。这意味着那些标有 #~ 的翻译也会被删除。

例如,使用 Babel 版本 0.9.6 和 Jinja2 版本 2.6 以及以下文件:

<代码>./babel.ini

[jinja2: **/templates/**.html]
encoding = utf-8

./templates/test.html./i18n/pt_PT/

<html><body>
<h1>{% trans %}My website{% endtrans %}</h1>
</body></html>

LC_MESSAGES/messages.po

# ... header stuff generated from
#     pybabel init -l pt_PT -d i18n -i i18n/messages.pot ...

# Don't forget, I want to remember something about this!
#~ msgid "My web page"
#~ msgstr "A minha página de web"

运行以下命令后:

$ pybabel extract -F babel.ini -o i18n/messages.pot .
$ pybabel update -l pt_PT -d i18n -i i18n/messages.pot

葡萄牙语 messages.po 文件将丢失所有旧注释,仅包含:

./i18n/pt_PT/LC_MESSAGES/messages.po

# ... similar header stuff ...

#: templates/test.html:2
msgid "My web site"
msgstr ""

如何更新我的翻译文件而不丢失我的评论和旧翻译?

With the Python-based Babel gettext utilities, are there any techniques to preserve translator comments and old ("obsolete") translations (marked with #~) in .po files across updates from the .pot file?

The first time an obsolete translation is in a .po file, and pybabel update is run, the translation is marked with #~. This is so that on the one hand, it is treated as a comment and not used until a translator looks it over and changes it, yet on the other hand, it is not removed, so a translator can refer to it, or copy text from it into their other translations.

However, the next time pybabel update is run, all comments are permanently removed from the file. This means those translations marked with #~ are removed, too.

For example, with Babel version 0.9.6 and Jinja2 version 2.6, and the following files:

./babel.ini:

[jinja2: **/templates/**.html]
encoding = utf-8

./templates/test.html:

<html><body>
<h1>{% trans %}My website{% endtrans %}</h1>
</body></html>

./i18n/pt_PT/LC_MESSAGES/messages.po:

# ... header stuff generated from
#     pybabel init -l pt_PT -d i18n -i i18n/messages.pot ...

# Don't forget, I want to remember something about this!
#~ msgid "My web page"
#~ msgstr "A minha página de web"

After the following commands are run:

$ pybabel extract -F babel.ini -o i18n/messages.pot .
$ pybabel update -l pt_PT -d i18n -i i18n/messages.pot

The Portuguese messages.po file loses all its old comments, and contains only:

./i18n/pt_PT/LC_MESSAGES/messages.po:

# ... similar header stuff ...

#: templates/test.html:2
msgid "My web site"
msgstr ""

How can I update my translation files without losing my comments and old translations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

醉南桥 2024-09-10 06:34:54

不使用 pybabel 更新,而是使用 msgmerge "noreferrer">gettext 实用程序

在您的情况下,它将是:

msgmerge ./i18n/pt_PT/LC_MESSAGES/messages.po ./i18n/messages.pot -o ./i18n/pt_PT/ LC_MESSAGES/messages.po

示例:

具有 reference.pot 文件:

msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: templates/test.html:2
msgid "My web site"
msgstr ""

pt_previous.po< /strong> 文件包含您以前的翻译:

msgid ""
msgstr ""
"Language-Team: \n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: pt\n"

# Don't forget, I want to remember something about this!
#~ msgid "My web site"
#~ msgstr "A minha página de web"

运行命令:

msgmerge pt_previous.po reference.pot -o pt_new.po

将生成 ./pt_new .po 文件如下所示:

msgid ""
msgstr ""
"Language-Team: \n"
"Language: pt\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

# Don't forget, I want to remember something about this!
#: templates/test.html:2
msgid "My web site"
msgstr "A minha página de web"

Instead of using pybabel update use msgmerge from gettext utilities

In your case it would be:

msgmerge ./i18n/pt_PT/LC_MESSAGES/messages.po ./i18n/messages.pot -o ./i18n/pt_PT/LC_MESSAGES/messages.po

Example:

having reference.pot file:

msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: templates/test.html:2
msgid "My web site"
msgstr ""

and pt_previous.po file with your previous translations:

msgid ""
msgstr ""
"Language-Team: \n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: pt\n"

# Don't forget, I want to remember something about this!
#~ msgid "My web site"
#~ msgstr "A minha página de web"

Running a command:

msgmerge pt_previous.po reference.pot -o pt_new.po

Will make ./pt_new.po file which looks like this:

msgid ""
msgstr ""
"Language-Team: \n"
"Language: pt\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

# Don't forget, I want to remember something about this!
#: templates/test.html:2
msgid "My web site"
msgstr "A minha página de web"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文