Translate Toolkit

3.19.5 · active · verified Thu Apr 16

The Translate Toolkit is a comprehensive suite of software and a Python API designed to enhance the productivity of localizers and streamline localization and translation engineering workflows. It supports a wide array of file formats, including common standards like Gettext PO and XLIFF, alongside many others. As of April 2026, the current version is 3.19.5, actively maintained with regular releases that include improvements and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically load, inspect, and modify a Gettext PO file using the Translate Toolkit's API. It creates a temporary PO file, reads its content, prints individual translation units, shows how to update a target string, and optionally saves the changes to a new file, finally cleaning up the temporary files.

import os
from translate.storage import po

# 1. Create a dummy PO file for demonstration
po_content = '''
msgid ""
msgstr ""
"Project-Id-Version: Example Project 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"

msgid "Hello, world!"
msgstr "¡Hola, mundo!"

msgid "Welcome to our application."
msgstr "Bienvenido a nuestra aplicación."
'''

with open("example.po", "w", encoding="utf-8") as f:
    f.write(po_content)

# 2. Load the PO file
try:
    po_file = po.pofile.PoFile("example.po")
    print(f"Loaded PO file with {len(po_file.units)} translation units (including header).")

    # 3. Iterate and print translation units
    for unit in po_file.units:
        if not unit.isblank(): # Skip the header unit
            print(f"---\nOriginal: '{unit.source}'\nTranslated: '{unit.target}'")

    # 4. Modify a translation unit (example)
    if len(po_file.units) > 1: # Ensure there's at least one real unit after header
        first_translatable_unit = po_file.units[1]
        print(f"\nModifying unit: '{first_translatable_unit.source}'")
        first_translatable_unit.target = "¡Saludos, universo!"
        print(f"New translation: '{first_translatable_unit.target}'")

    # 5. Save the modified PO file (optional)
    po_file.save("example_modified.po")
    print("\nModified file saved to example_modified.po")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # 6. Clean up dummy files
    if os.path.exists("example.po"):
        os.remove("example.po")
    if os.path.exists("example_modified.po"):
        os.remove("example_modified.po")
    print("\nCleaned up dummy files.")

view raw JSON →