polib

1.2.0 · active · verified Thu Apr 09

polib is a pure Python library designed to manipulate gettext files, specifically .po (Portable Object) and .mo (Machine Object) files. It enables loading, creating, modifying, and saving these translation files. The library is stable, widely used, and supports Python versions from 2.7 to the latest 3.x. Its release cadence is irregular but indicates active maintenance, with version 1.2.0 released in February 2023.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a PO file (from a string for simplicity), iterate through its entries, add a new entry, modify an existing entry, and save the changes. For real-world use, replace `polib.pofile(dummy_po_content)` with `polib.pofile('/path/to/your/file.po')` to load from a file.

import polib
import os

# Create a dummy .po file for demonstration
dummy_po_content = '''\
msgid ""
msgstr ""
"Project-Id-Version: test\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"MIME-Version: 1.0\n"
"X-Generator: Python-polib\n"

#: main.py:10
msgid "Hello, world!"
msgstr ""

#: another.py:5
msgid "Another message"
msgstr "Other message"
'''

# Load an existing PO file (or from a string)
# For a real file, use: pofile = polib.pofile('/path/to/your/file.po')
pofile = polib.pofile(dummy_po_content)

print("\n--- Iterating through entries ---")
for entry in pofile:
    print(f"Msgid: {entry.msgid}, Msgstr: {entry.msgstr}")

# Add a new entry
new_entry = polib.POEntry(
    msgid='New string',
    msgstr='Nueva cadena',
    comment='A new comment for this string',
    occurrences=[('app.py', '20')]
)
pofile.append(new_entry)

print("\n--- After adding a new entry ---")
for entry in pofile:
    print(f"Msgid: {entry.msgid}, Msgstr: {entry.msgstr}")

# Modify an entry
if pofile.find('Hello, world!'):
    entry_to_modify = pofile.find('Hello, world!')
    entry_to_modify.msgstr = '¡Hola, mundo!'
    entry_to_modify.comment = 'Translated by quickstart'

print("\n--- After modifying an entry ---")
for entry in pofile:
    print(f"Msgid: {entry.msgid}, Msgstr: {entry.msgstr}")

# Save the modified PO file (to a dummy path for demonstration)
dummy_output_path = 'temp_output.po'
pofile.save(dummy_output_path)
print(f"\nSaved modified PO file to {dummy_output_path}")

# Clean up the dummy file
os.remove(dummy_output_path)
print(f"Cleaned up {dummy_output_path}")

view raw JSON →