开发者

Django i18n find supported languages

开发者 https://www.devze.com 2023-02-22 10:51 出处:网络
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 l

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, do开发者_如何学运维es 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消