[I18n-sig] How to detect language?

philopensource@hotmail.com philopensource at hotmail.com
Tue May 24 10:50:27 CEST 2005


Hi list,
Thanks a lot with your help.

I have a last question before starting to write my code.
As python has lots of thing to simplify the work of the programmer, I 
don't know how to detect easily the language.

I found a way, but i must work a lot on character string and i must pay 
attention about small and capital letters.
I have this example:

Is it a good way to work?

domaine = 'test'
localeDir = os.path.join(os.getcwd(), 'locale')
gettext.install(domaine, localeDir, unicode=0)
langFr = gettext.translation(domaine, localeDir, languages=['fr'])
langDE = gettext.translation(domaine, localeDir, languages=['de'])
langEn = gettext.translation(domaine, localeDir, languages=['en'])

############### Detecting the language ################
l= locale.getdefaultlocale('LANG')[0] # i get "fr_FR"
l = l.split ( '_' ) # i [get fr,FR]
SetLang(l[0]) # then i select only the small letter for the language 
detected

locale.setlocale(locale.LC_ALL, '')

def SetLang(lang):
    if lang in ['fr']:
        langFr.install()
    elif lang in ['de']:
        langDe.install()
    else:
        langEn.install()




More information about the I18n-sig mailing list