Skip to content

Instantly share code, notes, and snippets.

@mythmon
Created October 30, 2012 23:53
Show Gist options
  • Save mythmon/3983894 to your computer and use it in GitHub Desktop.
Save mythmon/3983894 to your computer and use it in GitHub Desktop.
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