[I18n-sig] How to detect language?

Werner F. Bruhin werner.bruhin at free.fr
Tue May 24 12:03:15 CEST 2005


I don't like that the software guesses at what I want, so in my 
application there is a preference setting for the language.

This means that even if my OS is English I can run my app (to about 99%) 
in French or German or whatever.

But I can also see the other side that you do want to either default to 
the system setting or just take it over.

I think it is the first two you are interested in, e.g. fr_FR means 
French French dialect were fr_CH would mean French Swiss dialect.

See also:
http://www.iso.org/iso/fr/prods-services/popstds/languagecodes.html
http://www.faqs.org/rfcs/rfc1766.html

And in all the gettext related links.


philopensource at hotmail.com wrote:
> 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 think the first part is always lower case, but I see you got this below.
> I have this example:
> 
> Is it a good way to work?
If you want to auto detect I think yes, in my case I feed a preference 
setting to SetLang.

See you
Werner
> 
> 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