python-gettext

5.0 · active · verified Thu Apr 16

python-gettext is a Python library and command-line tool designed to compile Gettext `.po` (Portable Object) files into `.mo` (Machine Object) files. These `.mo` files are then used by internationalization (i18n) systems for runtime message translation. The current version is 5.0, with releases typically tied to Python version compatibility or API refinements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically compile a `.po` file into a `.mo` file using `gettext_compiler.compile_mo_file`. It creates a simple dummy `.po` file, compiles it, and then cleans up the generated files.

import os
from gettext_compiler import compile_mo_file

# Create a dummy .po file for demonstration
po_content = '''
msgid ""
msgstr ""
"Project-Id-Version: Test Project\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

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

po_file_path = "example.po"
mo_file_path = "example.mo"

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

# Compile the .po file to a .mo file
try:
    compile_mo_file(po_file_path, mo_file_path)
    print(f"Successfully compiled '{po_file_path}' to '{mo_file_path}'")
except Exception as e:
    print(f"Error compiling file: {e}")
finally:
    # Clean up the created files
    if os.path.exists(po_file_path):
        os.remove(po_file_path)
    if os.path.exists(mo_file_path):
        os.remove(mo_file_path)

view raw JSON →