Table of Contents#
- Understanding GNU gettext PO Files
- The Problem: Why Translator Comments and Old Translations Get Lost
- Introduction to Babel
- Preserving Translator Comments with Babel
- Preserving Old (Obsolete) Translations with Babel
- Advanced Tips and Best Practices
- Troubleshooting Common Issues
- Conclusion
- References
Understanding GNU gettext PO Files#
Before diving into Babel, let’s clarify the structure of a PO file. A typical PO file includes:
- Header: Metadata like language, charset, and plural rules (e.g.,
msgid ""; msgstr ""withLanguage: fr). - Message Entries: Pairs of
msgid(original string) andmsgstr(translated string). - Comments: Three types of comments:
- Extractor comments (
#.): Auto-generated by tools (e.g.,#. Default: "Hello"). - Reference comments (
#:): Point to source code locations (e.g.,#: app.py:42). - Translator comments (
#): Manual notes for translators (e.g.,# Use formal tone here).
- Extractor comments (
Example PO Snippet:
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Language: es\n"
# Use formal greeting for users in Spain
msgid "Hello, World!"
msgstr "¡Hola, Mundo!"
#: app.py:10
msgid "Welcome back!"
msgstr "¡Bienvenido de nuevo!" Here, # Use formal greeting... is a translator comment, and #: app.py:10 is a reference comment.
The Problem: Why Translator Comments and Old Translations Get Lost#
When updating PO files (e.g., after source code changes), tools like Babel’s pybabel update may:
- Lose translator comments: If the
msgidis modified or removed, Babel might detach the comment from the original entry. - Delete old translations: When a
msgidis deleted from the source, itsmsgstrbecomes "obsolete." By default, some tools (including Babel without flags) may purge these obsolete entries.
Why does this matter?
- Translator comments provide critical context (e.g., tone, audience) that ensures accurate translations. Losing them forces translators to re-contextualize strings.
- Obsolete translations are often reusable. If a
msgidis later reintroduced (e.g., a feature is restored), reviving the oldmsgstrsaves time.
Introduction to Babel#
Babel is a Python library and command-line toolchain for internationalization. It simplifies extracting strings from source code, generating PO files, and updating translations. Key Babel commands include:
pybabel extract: Extractsmsgids from source files (e.g., Python, Jinja2) into a.pot(Portable Object Template) file.pybabel init: Creates a new PO file from a.pottemplate for a target language.pybabel update: Updates existing PO files with newmsgids from a.potfile (critical for preserving translations).
Babel is widely used in Python ecosystems (e.g., Flask, Django) but works with any project using gettext.
Preserving Translator Comments with Babel#
Translator comments (# ) are manual annotations and should be preserved during updates. Babel automatically preserves translator comments if:
- The
msgidremains unchanged. - The comment is correctly formatted (starts with
#, not#.or#:).
How Babel Handles Comments#
When you run pybabel update, Babel matches existing msgids in the PO file with new msgids in the .pot template. For unchanged msgids, Babel retains all associated comments (translator, reference, extractor).
Example Workflow:
-
Original PO File (
es.po):# Formal greeting for logged-in users msgid "Hello, User!" msgstr "¡Hola, Usuario!" -
Update the
.potTemplate (no change tomsgid "Hello, User!"). -
Run
pybabel update:pybabel update -i messages.pot -d translations -l es -
Result: The translator comment is preserved:
# Formal greeting for logged-in users msgid "Hello, User!" msgstr "¡Hola, Usuario!"
What If the msgid Changes?#
If the msgid is modified (e.g., msgid "Hello, User!" → msgid "Hello, Valued User!"), Babel will treat it as a new string. The original translator comment will not automatically transfer to the new msgid.
Solution: Manually re-add the comment to the new msgid or use tools like Poedit to link comments to similar strings.
Preserving Old (Obsolete) Translations with Babel#
When a msgid is removed from the source code, its msgstr becomes "obsolete." By default, Babel marks obsolete entries with #~ (e.g., #~ msgid "Old String"). However, without the --keep-obsolete flag, Babel may delete these entries during pybabel update.
The --keep-obsolete Flag#
To preserve obsolete translations, use the --keep-obsolete (or -k) flag with pybabel update. This ensures obsolete entries remain in the PO file as #~ comments.
Example Workflow:
-
Original PO File (
es.po) with an activemsgid:msgid "Legacy Feature" msgstr "Característica Legada" -
Source Code Change: The
msgid "Legacy Feature"is removed. -
Update Without
--keep-obsolete:pybabel update -i messages.pot -d translations -l esResult: The entry is deleted.
-
Update With
--keep-obsolete:pybabel update -i messages.pot -d translations -l es --keep-obsoleteResult: The entry is marked as obsolete but preserved:
#~ msgid "Legacy Feature" #~ msgstr "Característica Legada"
Reviving Obsolete Translations#
If a msgid is reintroduced, Babel will match it with the obsolete #~ msgid and revive the msgstr (removing the #~ prefix). This avoids retranslating the string.
Advanced Tips and Best Practices#
1. Configure Babel to Avoid Accidental Stripping#
-
Use a
babel.cfgFile: Define extraction rules to ensure comments and strings are not stripped. Examplebabel.cfg:[python: **.py] [jinja2: **/templates/**.html] extensions=jinja2.ext.autoescape,jinja2.ext.with_ -
Explicitly Preserve Comment Types: Babel’s extractor ignores non-standard comments by default. Ensure translator comments use
#(space after#) to avoid misclassification.
2. Automate with --keep-obsolete#
Add --keep-obsolete to your update script to make preservation default. For example, in Makefile:
update-translations:
pybabel extract -F babel.cfg -o messages.pot .
pybabel update -i messages.pot -d translations -l es --keep-obsolete
pybabel update -i messages.pot -d translations -l fr --keep-obsolete 3. Review Obsolete Entries Periodically#
Obsolete translations can bloat PO files. Use tools like msgattrib (from gettext) to clean up unused entries:
msgattrib --no-obsolete es.po -o es-clean.po 4. Combine Babel with Poedit#
Poedit is a GUI tool for editing PO files. It integrates with Babel and highlights obsolete entries, making it easy to review/revive them.
Troubleshooting Common Issues#
Issue: Translator Comments Disappearing#
- Root Cause: The
msgidwas modified, or the comment was misformatted (e.g.,#.instead of#). - Fix: Ensure comments use
#, and manually reattach comments to updatedmsgids.
Issue: Obsolete Translations Still Deleted#
- Root Cause: Forgetting
--keep-obsoleteinpybabel update. - Fix: Always use
--keep-obsoletewhen updating. Verify withgrep "#~" es.poto check for preserved entries.
Issue: Babel Removes Non-ASCII Characters#
- Root Cause: Incorrect charset in the PO header.
- Fix: Ensure the header includes
charset=UTF-8:msgstr "" "Content-Type: text/plain; charset=UTF-8\n"
Conclusion#
Preserving translator comments and obsolete translations in PO files is critical for maintaining translation quality and efficiency. Babel simplifies this with:
- Automatic preservation of translator comments for unchanged
msgids. - The
--keep-obsoleteflag to retain old translations as#~entries.
By following best practices—using --keep-obsolete, configuring babel.cfg, and reviewing obsolete entries—you ensure your i18n workflow remains robust and translator-friendly.