python gettext: specify locale in _()
Question: I am looking fo a way to set the language on the fly when requesting a
translation for a string in gettext. I'll explain why :

I have a multithreaded bot that respond to users by text on multiple servers,
thus needing to reply in different languages. The
[documentation](http://www.enseignement.polytechnique.fr/informatique/INF478/docs/Python3/library/gettext.html#changing-
languages-on-the-fly "doc") of gettext states that, to change locale while
running, you should do the following :

    
    
    import gettext # first, import gettext
    
    lang1 = gettext.translation('myapplication', languages=['en']) # Load every translations
    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()
    

But, this does not apply in my case, as the bot is multithreaded :

Imagine the 2 following snippets are running at the same time :

    
    
    import time
    import gettext 
    lang1 = gettext.translation('myapplication', languages=['fr'])
    lang1.install()
    message(_("Loading a dummy task")) # This should be in french, and it will
    time.sleep(10)
    message(_("Finished loading")) # This should be in french too, but it wont :'(
    

and

    
    
    import time
    import gettext 
    lang = gettext.translation('myapplication', languages=['en'])
    time.sleep(3) # Not requested on the same time
    lang.install()
    message(_("Loading a dummy task")) # This should be in english, and it will
    time.sleep(10)
    message(_("Finished loading")) # This should be in english too, and it will
    

You can see that messages sometimes are translated in the wrong locale. But,
if I could do something like `_("string", lang="FR")`, the problem would
disappear !

Have I missed something, or I'm using the wrong module to do the task... I'm
using python3


Answer: The following simple example shows how to use a separate process for each
translator:

    
    
    import gettext
    import multiprocessing
    import time
    
    def translation_function(language):
        try:
            lang = gettext.translation('simple', localedir='locale', languages=[language])
            lang.install()
            while True:
                print(_("Running translator"), ": %s" % language)
                time.sleep(1.0)
        except KeyboardInterrupt:
            pass
    
    if __name__ == '__main__':
        thread_list = list()
        try:
            for lang in ['en', 'fr', 'de']:
                t = multiprocessing.Process(target=translation_function, args=(lang,))
                t.daemon = True
                t.start()
                thread_list.append(t)
            while True:
                time.sleep(1.0)
        except KeyboardInterrupt:
            for t in thread_list:
                t.join()
    

The output looks like this:

    
    
    Running translator : en
    Traducteur en cours d’exécution : fr
    Laufenden Übersetzer : de
    Running translator : en
    Traducteur en cours d’exécution : fr
    Laufenden Übersetzer : de
    

When I tried this using threads, I only got an English translation. You could
create individual threads in each process to handle connections. You probably
do not want to create a new process for each connection.

