{"id":8551,"library":"python-gettext","title":"python-gettext","description":"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.","status":"active","version":"5.0","language":"en","source_language":"en","source_url":"https://github.com/hannosch/python-gettext","tags":["i18n","localization","gettext","compiler","po","mo"],"install":[{"cmd":"pip install python-gettext","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary module was renamed from 'msgfmt_compiler' to 'gettext_compiler' in version 5.0.","wrong":"from msgfmt_compiler import compile_mo_file","symbol":"compile_mo_file","correct":"from gettext_compiler import compile_mo_file"}],"quickstart":{"code":"import os\nfrom gettext_compiler import compile_mo_file\n\n# Create a dummy .po file for demonstration\npo_content = '''\nmsgid \"\"\nmsgstr \"\"\n\"Project-Id-Version: Test Project\\n\"\n\"Language-Team: \\n\"\n\"MIME-Version: 1.0\\n\"\n\"Content-Type: text/plain; charset=utf-8\\n\"\n\"Content-Transfer-Encoding: 8bit\\n\"\n\nmsgid \"Hello, world!\"\nmsgstr \"Hola, mundo!\"\n'''\n\npo_file_path = \"example.po\"\nmo_file_path = \"example.mo\"\n\nwith open(po_file_path, \"w\", encoding=\"utf-8\") as f:\n    f.write(po_content)\n\n# Compile the .po file to a .mo file\ntry:\n    compile_mo_file(po_file_path, mo_file_path)\n    print(f\"Successfully compiled '{po_file_path}' to '{mo_file_path}'\")\nexcept Exception as e:\n    print(f\"Error compiling file: {e}\")\nfinally:\n    # Clean up the created files\n    if os.path.exists(po_file_path):\n        os.remove(po_file_path)\n    if os.path.exists(mo_file_path):\n        os.remove(mo_file_path)\n","lang":"python","description":"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."},"warnings":[{"fix":"Update your import statements from `from msgfmt_compiler import ...` to `from gettext_compiler import ...`.","message":"The primary import module name changed from `msgfmt_compiler` to `gettext_compiler` in version 5.0.","severity":"breaking","affected_versions":">=5.0"},{"fix":"Ensure your project is running on Python 3.7+ to use `python-gettext` versions 4.0 and higher.","message":"Python 2 support was dropped in version 4.0. The library now requires Python 3.7 or newer.","severity":"breaking","affected_versions":">=4.0"},{"fix":"Remember that `python-gettext` provides the `gettext_compiler` module for the build-time step of creating `.mo` files, while the standard `gettext` module is used at runtime for actual message lookup.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure your `.po` files are consistently encoded, preferably UTF-8. If using a different encoding, you may need to pre-process the `.po` content before passing it to the compiler.","message":"Incorrect encoding of `.po` files can lead to compilation errors or corrupted `.mo` files. The compiler defaults to UTF-8.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change your import statement to `from gettext_compiler import compile_mo_file`.","cause":"Attempting to import from the old module name (`msgfmt_compiler`) after upgrading to `python-gettext` version 5.0 or later.","error":"ModuleNotFoundError: No module named 'msgfmt_compiler'"},{"fix":"Install the package using `pip install python-gettext`. If in a virtual environment, ensure it's activated.","cause":"The `python-gettext` package is not installed in the current environment or the Python interpreter cannot find it.","error":"ModuleNotFoundError: No module named 'gettext_compiler'"},{"fix":"Verify 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.","cause":"The input `.po` file path provided to `compile_mo_file` does not exist or is incorrect.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'path/to/non_existent.po'"},{"fix":"Ensure 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.","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.","error":"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x... in position ...: invalid start byte"}]}