Search code examples
pythoninternationalizationgettextglib

Glib equivalent of `textdomain()` and `bindtextdomain()`


I am adding I18N support to some Python code that uses GLib-based interface to the rest of the application. For consistency with the rest of the application I want to use GLib's gettext implementation/wrapper.

I import the GLib API with:

import gi
from gi.repository import GLib

However, if I see some *gettext() functions in the library (dcgettext, dgettext, dngettext, dpgettext, dpgettext2) I cannot find the equivalent of textdomain()and bindtextdomain().

Am I missing something?


Solution

  • GLib doesn't have its own implementation of gettext. It definitely uses it though for translation and -especially on the C side- provides useful wrappers for it.

    When using GTK on Python, that means you would normally do to call bindtextdomain(): you use the Python-provided locale module. If Python was built without gettext support, you'll have to import the gettext module

    import gettext
    import locale
    
    try:
        locale.bindtextdomain(app_id, locale_dir)
        locale.textdomain(app_id)
    except AttributeError as e:
        # Python built without gettext support does not have
        # bindtextdomain() and textdomain().
        gettext.bindtextdomain(app_id, locale_dir)
        gettext.textdomain(app_id)