cyberangles blog

Using Babel: How to Preserve Translator Comments and Old (Obsolete) Translations in GNU gettext PO Files

In the world of software internationalization (i18n), GNU gettext PO (Portable Object) files are the gold standard for storing translations. These files bridge source code and localized content, containing original strings (msgid), translated strings (msgstr), and critical context like comments. For translators and developers alike, preserving two key elements is often vital: translator comments (contextual notes for translators) and old/obsolete translations (outdated but potentially reusable strings).

However, when updating PO files—for example, to reflect changes in source code—tools like Babel (a popular Python i18n library) can inadvertently strip comments or purge obsolete translations if misconfigured. This blog post dives into how Babel works with PO files, why these elements get lost, and step-by-step solutions to preserve them. Whether you’re a developer managing i18n workflows or a translator ensuring context isn’t lost, this guide will help you maintain robust, usable PO files.

2026-01

Table of Contents#

  1. Understanding GNU gettext PO Files
  2. The Problem: Why Translator Comments and Old Translations Get Lost
  3. Introduction to Babel
  4. Preserving Translator Comments with Babel
  5. Preserving Old (Obsolete) Translations with Babel
  6. Advanced Tips and Best Practices
  7. Troubleshooting Common Issues
  8. Conclusion
  9. 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 "" with Language: fr).
  • Message Entries: Pairs of msgid (original string) and msgstr (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).

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 msgid is modified or removed, Babel might detach the comment from the original entry.
  • Delete old translations: When a msgid is deleted from the source, its msgstr becomes "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 msgid is later reintroduced (e.g., a feature is restored), reviving the old msgstr saves 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: Extracts msgids from source files (e.g., Python, Jinja2) into a .pot (Portable Object Template) file.
  • pybabel init: Creates a new PO file from a .pot template for a target language.
  • pybabel update: Updates existing PO files with new msgids from a .pot file (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:

  1. The msgid remains unchanged.
  2. 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:

  1. Original PO File (es.po):

    # Formal greeting for logged-in users  
    msgid "Hello, User!"  
    msgstr "¡Hola, Usuario!"  
  2. Update the .pot Template (no change to msgid "Hello, User!").

  3. Run pybabel update:

    pybabel update -i messages.pot -d translations -l es  
  4. 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:

  1. Original PO File (es.po) with an active msgid:

    msgid "Legacy Feature"  
    msgstr "Característica Legada"  
  2. Source Code Change: The msgid "Legacy Feature" is removed.

  3. Update Without --keep-obsolete:

    pybabel update -i messages.pot -d translations -l es  

    Result: The entry is deleted.

  4. Update With --keep-obsolete:

    pybabel update -i messages.pot -d translations -l es --keep-obsolete  

    Result: 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.cfg File: Define extraction rules to ensure comments and strings are not stripped. Example babel.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 msgid was modified, or the comment was misformatted (e.g., #. instead of # ).
  • Fix: Ensure comments use # , and manually reattach comments to updated msgids.

Issue: Obsolete Translations Still Deleted#

  • Root Cause: Forgetting --keep-obsolete in pybabel update.
  • Fix: Always use --keep-obsolete when updating. Verify with grep "#~" es.po to 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-obsolete flag 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.

References#