I'm trying to internationalize my python application but I have a small problem : The messages to print are in english by default (in my python code they are in engilsh) and I generated the french translation in a file fr.mo situated in : myAppName/src/locale/fr_Fr/LC_Messages/fr.mo and now I don't know which code to insert in my files .py to traduce to french my messages only when my variable langVar='fr' and not when the system default Language is french that means when the user chooses english ==> lanVAr='en' I want my application to print the messages in my code without any traduction .. Is it possible ?
my python code :
main_app.py:
import logging
import app
import i18n
_ = i18n.language.ugettext
def my_function(langVar):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
logging.info(_("Start My application "))
app.my_app_function()
#...
logging.info(_("End My application"))
if __name__ == '__main__':
langVar = input("Enter your language : ")
my_function(langVar)
app.py:
import logging
import i18n
_ = i18n.language.ugettext
def my_app_function():
logging.info(_("start my_app_function"))
#...
logging.error("Unexpected error !!")
#...
logging.info(_("end my_app_function"))
i18n.py:
import os, sys
import locale
import gettext
LOCALE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
lc, encoding = locale.getdefaultlocale()
if lc:
languages = [lc]
mo_location = LOCALE_DIR
gettext.install(True, localedir=None, unicode=1)
gettext.find("fr", mo_location)
gettext.textdomain ("fr")
gettext.bind_textdomain_codeset("fr", "UTF-8")
language = gettext.translation("fr", mo_location, languages , fallback=True)
the problem is with this code I wrote the traduction is done when the system default language is french I want it to be done when the user enter "fr"
Does anyone have any idea how to do this? Thanks