I'm newbie using gettext library to translate text. I want to translate the text that there is in the next file:
dobles_message.py
def set_message(self, language):
t = gettext.translation("basketmetrics_i18n", "../i18n", fallback=True, languages=["'" + language + "'"])
t.install()
_ = t.gettext
self.message = _("mensaje_dd_1")
To do that,first I have create dobles_message.po with this instrucction:
xgettext -i dobles_message.py -o dobles_message.pot -d basketmetrics_i18n
Then, I have created these directory structure i18n/es/LC_MESSAGES and i18n/en/LC_MESSAGES and I have created the .po version for each languate with these instructions:
msginit -i dobles_message.pot -o ../i18n/es/LC_MESSAGES/basketmetrics_i18n.po -l es
msginit -i dobles_message.pot -o ../i18n/en/LC_MESSAGES/basketmetrics_i18n.po -l en
Thrid, I have change the charset and translate the text of each file. And fourth, I have created the .mo files from each .po file with this instruction in directory i18n/es/LC_MESSAGES and in directory i18n/en/LC_MESSAGES
msgfmt basketmetrics_i18n.po -o basketmetrics_i18n.mo
But, when I run my application instead of translate the text, muy application shows me the msgid.
These is how is my structure of files and directories:
And the content of my files are next ...
dobles_message.pot
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-10 13:22+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: dobles_message.py:15
msgid "mensaje_dd_1"
msgstr ""
basketmetrics_i18n.po (English version)
# English translations for PACKAGE package.
# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# José Carlos <josecarlos@asterix>, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-10 13:22+0200\n"
"PO-Revision-Date: 2018-10-10 13:31+0200\n"
"Last-Translator: Jos Carlos <josecarlos@asterix>\n"
"Language-Team: English\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: dobles_message.py:15
msgid "mensaje_dd_1"
msgstr "¡¡¡Hi ###twitter###!!! From www.basketmetrics.com we want to congrats to get your double double nº ###numero### this season. ¡¡¡Congratulations!!!"
basketmetrics_i18n.po (Spanish version)
# Spanish translations for PACKAGE package.
# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# José Carlos <josecarlos@asterix>, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-10-10 13:22+0200\n"
"PO-Revision-Date: 2018-10-10 13:28+0200\n"
"Last-Translator: Jos Carlos <josecarlos@asterix>\n"
"Language-Team: Spanish\n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=latin-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: dobles_message.py:15
msgid "mensaje_dd_1"
msgstr "¡¡¡Hola ###twitter###!!! Desde www.basketmetrics.com queremos felicitarte por haber conseguido tu doble doble nº ###numero### esta temporada. ¡¡¡Felicidades!!!"
What am I doing wrong? I'm using python3
Edit I:
In my code, I have remove the option "fallback=True" and I get this error:
In my first step I define the domain "basmetmetrics_i18n" and I've got the .po files created with that name. What happend?
Edit II:
Following the wise advice from @stovfl I have change my set_message function:
def set_message(self, language):
print("language: " + language)
gettext.bindtextdomain('basketmetrics_i18n', '../i18n')
gettext.textdomain('basketmetrics_i18n')
_ = gettext.gettext
self.message = _("mensaje_dd_1")
print("Mensaje traducido: " + self.message)
Right now, I haven't any error in my application but I'm still getting the msgid instead of the msgstr. I don't understand anything :(