Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django i18n find supported languages

I'm determining a user's language preference through some third party service he's also registered at. This service provides me with a locale code (e.g. en_US). If I do not have the corresponding language code in settings.LANGUAGES, does Django provide some integrated (simple) way to determine the best fallback choise from settings.languages (e.g. en-gb).

Of course I know I could do a couple of string comparisons of the locale code etc by myself, just being curious if there's a handier solution!

like image 858
Bernhard Vallant Avatar asked Apr 04 '11 12:04

Bernhard Vallant


1 Answers

You can see the code Django uses to determine the language based on a request here. Unfortunately there doesn't seem to be a handy utility function for what you're doing. The logic used is trivial however -- just lop the end off if the sub-language isn't supported and search for the main language as a fallback. This wouldn't get you from en-us to en-gb however.

Incidentally, the to_locale and to_language functions in that file might be of interest to you.

like image 152
DrMeers Avatar answered Oct 19 '22 12:10

DrMeers