def get_language_from_request(request):
    global _accepted
    from django.conf import settings
    globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale')
    supported = dict(settings.LANGUAGES)

    if hasattr(request, 'session'):
        lang_code = request.session.get('django_language', None)
        if lang_code in supported and lang_code is not None and check_for_language(lang_code):
            return lang_code

    lang_code = request.COOKIES.get('django_language')
    if lang_code and lang_code in supported and check_for_language(lang_code):
        return lang_code

    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
    for lang, unused in parse_accept_lang_header(accept):
        if lang == '*':
            break

        # We have a very restricted form for our language files (no encoding
        # specifier, since they all must be UTF-8 and only one possible
        # language each time. So we avoid the overhead of gettext.find() and
        # look up the MO file manually.

        normalized = locale.locale_alias.get(to_locale(lang, True))
        if not normalized:
            continue

        # Remove the default encoding from locale_alias
        normalized = normalized.split('.')[0]

        if normalized in _accepted:
            # We've seen this locale before and have an MO file for it, so no
            # need to check again.
            return _accepted[normalized]

        for lang in (normalized, normalized.split('_')[0]):
            if lang not in supported:
                continue
            langfile = os.path.join(globalpath, lang, 'LC_MESSAGES',
                    'django.mo')
            if os.path.exists(langfile):
                _accepted[normalized] = lang
            return lang

    return settings.LANGUAGE_CODE
Analyzes the request to find what language the user wants the system to show.
dataset/ETHPy150Open AppScale/appscale/AppServer/lib/django-0.96/django/utils/translation/trans_real.py/get_language_from_request