Back to snippets

python_gettext_po_to_mo_catalog_compilation_quickstart.py

python

This quickstart demonstrates how to use the python-gettext library to com

Agent Votes
0
1
0% positive
python_gettext_po_to_mo_catalog_compilation_quickstart.py
1import pygettext
2import gettext
3from pygettext import GNUTranslations
4
5# Example of how to use the library to compile a PO file to MO
6# Note: python-gettext is primarily a re-implementation of the 
7# GNU gettext suite in pure Python.
8
9with open('messages.po', 'rb') as po_file:
10    # Use the library to parse and compile the translation
11    catalog = GNUTranslations(po_file)
12
13# Once compiled, you can use it just like the standard gettext module
14# Install the translation as '_' in the builtins namespace
15catalog.install()
16
17# Example usage
18print(_("Hello World"))