0

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

user3328690
  • 115
  • 2
  • 8
  • If you could intercept the output (http://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python), you could run in through a translator (if language !=en) http://www.catonmat.net/blog/python-library-for-google-translate/. – Jeff Mar 15 '14 at 13:38

1 Answers1

1

To handle units ect: locale.setlocale(locale.LC_ALL, 'fr_FR')

This shows how to switch language:

import gettext

lang1 = gettext.translation('myapplication', languages=['en'])
lang2 = gettext.translation('myapplication', languages=['fr'])
lang3 = gettext.translation('myapplication', languages=['de'])

# start by using language1
lang1.install()

# ... time goes by, user selects language 2
lang2.install()

# ... more time goes by, user selects language 3
lang3.install()
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Thanks for the answer .. I don't understand you where should I write this code and how can I use my variable to change the language? @steve Barnes – user3328690 Mar 15 '14 at 14:01
  • Early in your code specify the available languages and soon after the user selects/your variable is set call `langselected.install()` – Steve Barnes Mar 15 '14 at 22:46