Django i18n find supported languages

I determine the preference of the user language through the third service, which he also registered. This service provides me with the locale code (e.g. en_US ). If I don't have the appropriate language code in settings.LANGUAGES , Django provides some integrated (easy) way to determine the best choice from settings.LANGUAGES (e.g. en-gb ).

Of course, I know that I could do a couple of lines of comparing the language code, etc., just be curious if there is a more efficient solution!

+8
python django internationalization locale
source share
1 answer

You can see the code used by Django to determine the language based on the request here . Unfortunately, there is no convenient utility function for what you do. However, the logic used is trivial - just complete the end if the sublanguage is not supported and is looking for the main language as a backup, This would not lead you from en-us to en-gb .

By the way, to_locale and to_language functions in this file may interest you.

+5
source share

All Articles