With the Python-based Babel gettext utilities, are there any techniques to preserve transla开发者_开发技巧tor 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?
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"
精彩评论