Created
October 30, 2012 23:53
-
-
Save mythmon/3983894 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def detect_language(request): | |
""" | |
Pick a user's preferred language from their Accept-Language headers. | |
""" | |
accept = request.META.get('HTTP_ACCEPT_LANGUAGE') | |
if not accept: | |
return '' | |
ranked_languages = parse_accept_lang_header(accept) | |
for lang, q in ranked_languages: | |
locale = to_locale(lang).replace('_', '-') | |
if locale in product_details.languages: | |
return locale | |
shortened_locale = locale.split('-')[0] | |
if (shortened_locale != locale and | |
shortened_locale in product_details.languages): | |
return shortened_locale | |
# No dice. | |
return '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment