I tried to look at the python docs about making some i18n mechanism on a python project. Although I usually like python docs, this section didn't look intuitive to me, and I looked another resources. By looking these:
inventwithpython.com/blog/translate-your-python-3-program-with-the-gettext-module/
Python docs: localizing-your-application
I managed to do the source code I pasted below. First it was giving a domain error, but after adding the .mo files, now it doesn't work or show any error.
main.py
from gettext import translation
from gettext import gettext as _
lang1 = translation('poker', '/home/myUser/myProjects/pokerProject/resources/localedir', languages=['en'])
lang2 = translation('poker', '/home/myUser/myProjects/pokerProject/resources/localedir', languages=['es'])
lang2.install()
# Code with strings in this way
print(_('This should be translated'))
.mo file sample
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 2.0.1\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: main.py:26
msgid "This should be translated"
msgstr "Translated text"
Layout (pokerProject sub-tree)
.
├── martintc
│ ├── __init__.py
│ └── poker
│ ├── __init__.py
│ └── model
│ ├── diceset.py
│ ├── die.py
│ ├── errors.py
│ ├── __init__.py
│ ├── main.py
│ ├── poker.pot
│ └── utils.py
└── resources
└── localedir
├── en
│ └── LC_MESSAGES
│ ├── poker.mo
│ └── poker.po
└── es
└── LC_MESSAGES
├── poker.mo
└── poker.po
I found the problem and a provisional but not satisfactory solution.
My code had the line:
from gettext import gettext as _
and it was not needed. As a matter of fact, when I deleted this line, everything went perfectly.
I don't understand well why, but this solved it. I don't know if this will work when I will have another classes with i18n strings