Search code examples
pythongettext

How to set python gettext translations to source language


I have this piece of code for translations:

translations = gettext.translation(
    locale_language,
    localedir="gui/locale",
    languages=[locale_language]
)
translations.install()

Where locale_language can be "en" (source language (msgids)) or "es" (translated language (msgstrs))

I need to have en.mo and es.mo to switch languages correctly, but since the source language is English, en.mo is not filled. I have to do it this way to run previous code when language changes.

I was wondering if there is a better way to do this, i.e., without an empty en.mo (also above code would requiere an if checking if locale_language == "en", but that is not the problem).


Solution

  • There is a never ending discussion whether the msgid should be the actual English string, or rather a "key" which will need to be translated to English as to any other language: look for instance at this question. And of course if you use key strings the en.mo file needs to be complete just like any other.

    If however you want to use actual English strings as msgids then you may simply avoid to install a translation for English, and perhaps define a dummy _() function like

    def _(msg):
        return msg
    

    This way you won't need a en.mo at all.