python-gettext
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
-
ModuleNotFoundError: No module named 'msgfmt_compiler'
cause Attempting to import from the old module name (`msgfmt_compiler`) after upgrading to `python-gettext` version 5.0 or later.fixChange your import statement to `from gettext_compiler import compile_mo_file`. -
ModuleNotFoundError: No module named 'gettext_compiler'
cause The `python-gettext` package is not installed in the current environment or the Python interpreter cannot find it.fixInstall the package using `pip install python-gettext`. If in a virtual environment, ensure it's activated. -
FileNotFoundError: [Errno 2] No such file or directory: 'path/to/non_existent.po'
cause The input `.po` file path provided to `compile_mo_file` does not exist or is incorrect.fixVerify that the path to your `.po` file is correct and the file exists at that location relative to your script's execution directory, or provide an absolute path. -
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x... in position ...: invalid start byte
cause The input `.po` file is not encoded in UTF-8, or contains invalid UTF-8 sequences, and the compiler is attempting to decode it as such.fixEnsure your `.po` file is correctly saved with UTF-8 encoding. If your `.po` file uses a different encoding, you may need to convert it to UTF-8 before processing.
Warnings
- breaking The primary import module name changed from `msgfmt_compiler` to `gettext_compiler` in version 5.0.
- breaking Python 2 support was dropped in version 4.0. The library now requires Python 3.7 or newer.
- gotcha This library (`python-gettext`) is for *compiling* `.po` files to `.mo` files. It is often confused with Python's built-in `gettext` module, which provides runtime translation APIs using already compiled `.mo` files.
- gotcha Incorrect encoding of `.po` files can lead to compilation errors or corrupted `.mo` files. The compiler defaults to UTF-8.
Install
-
pip install python-gettext
Imports
- compile_mo_file
from msgfmt_compiler import compile_mo_file
from gettext_compiler import compile_mo_file
Quickstart
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)