def get_language_from_request_and_is_from_path(request):  # noqa complexity-16
    """
    Analyzes the request to find what language the user wants the system to
    show. Only languages listed in settings.LANGUAGES are taken into account.
    If the user requests a sublanguage where we have a main language, we send
    out the main language. It also returns a value to determine if the language code
    was derived from a language code in the URL, or inferred from some other source.
    :returns: tuple of language code, boolean. The former can be None if the url being
    requested does not require translation, otherwise it should be a language code
    from the values in settings.LANGUAGES. The boolean should indicate whether the
    language code was calculated by reading a language code from the requested URL.
    In the case that it was, True should be returned, in the case where the URL language
    code was not used or not present, False is returned.
    """

    try:
        # If this is not a view that needs to be translated, return None, and be done with it!
        if not getattr(resolve(request.path_info).func, "translated", False):
            return None, False
    except Resolver404:
        # If this is an unrecognized URL, it may be redirectable to a language prefixed
        # URL, so let the language code setting carry on from here.
        pass

    supported_lang_codes = get_languages()

    lang_code = get_language_from_path(request.path_info)
    if lang_code in supported_lang_codes and lang_code is not None:
        return lang_code, True

    if hasattr(request, "session"):
        lang_code = request.session.get(LANGUAGE_SESSION_KEY)
        if (
            lang_code in supported_lang_codes
            and lang_code is not None
            and check_for_language(lang_code)
        ):
            return lang_code, False

    lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)

    try:
        return get_supported_language_variant(lang_code), False
    except LookupError:
        pass

    device_language = get_device_language()

    if device_language is not None:
        return device_language, False

    headers_language = get_accept_headers_language(request)

    if headers_language is not None:
        return headers_language, False

    return get_settings_language(), False
Example #2
0
def get_language_from_request(request, check_path=False):
    """
    Replacement for django.utils.translation.get_language_from_request.
    The portion of code that is modified is identified below with a comment.
    """
    if check_path:
        lang_code = get_language_from_path(request.path_info)
        if lang_code is not None:
            return lang_code

    lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
    if lang_code is not None and lang_code in get_languages(
    ) and check_for_language(lang_code):
        return lang_code

    try:
        return get_supported_language_variant(lang_code)
    except LookupError:
        pass

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

        # Convert lowercase region to uppercase before attempting to find a variant.
        # This is the only portion of code that is modified from the core function.
        accept_lang = language_code_to_iso_3166(accept_lang)

        if not language_code_re.search(accept_lang):
            continue

        try:
            return get_supported_language_variant(accept_lang)
        except LookupError:
            continue

    try:
        return get_supported_language_variant(settings.LANGUAGE_CODE)
    except LookupError:
        return settings.LANGUAGE_CODE
Example #3
0
def get_language_from_request(request, check_path=False):
	"""
	This method is used as a replacement to the original django language 
    detection algorithm. It takes the db default language into 
    consideration and does not deal with the Accept-Language header.
    
    Analyzes the request to find what language the user wants the system to
    show. Only languages listed in settings.LANGUAGES are taken into account.
    If the user requests a sublanguage where we have a main language, we send
    out the main language.

    If check_path is True, the URL path prefix will be checked for a language
    code, otherwise this is skipped for backwards compatibility.
    """
	#retrieve list of supported languages
	supported = get_supported_languages()

	if check_path:
		lang_code = get_language_from_path(request.path_info, [settings.LANGUAGE_CODE].append(supported))
		if lang_code is not None:
			return lang_code

	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(settings.LANGUAGE_COOKIE_NAME)

	if lang_code and lang_code not in supported:
		lang_code = lang_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr

	if lang_code and lang_code in supported and check_for_language(lang_code):
		return lang_code

	#original Django middleware used to look for the Accept-Language 
	#HTTP header and extract the language. This is replaced in our
	#mechanism
	return get_default_language()