def process_request(self, request): if settings.APP_CONF.get('force_default_lang'): lang_code = request.session.get(LANGUAGE_SESSION_KEY) supported_lang_codes = get_languages() if lang_code not in supported_lang_codes: request.session[LANGUAGE_SESSION_KEY] = settings.LANGUAGE_CODE return super(LocaleMiddleware, self).process_request(request)
def _get_language_from_request(request, user): """从请求中获取需要同步到用户个人信息的语言 """ supported_lang_codes = get_languages() # session 有language,说明在登录页面有进行修改或设置,则需要同步到用户个人信息中 lang_code = request.session.get(translation.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 # 个人信息中已有language if user.language: return None # session 情况不满足同步到用户个人信息,且目前个人信息中无language设置 # 查询header头 accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '') for accept_lang, unused in parse_accept_lang_header(accept): if accept_lang == '*': break if not language_code_re.search(accept_lang): continue try: return get_supported_language_variant(accept_lang) except LookupError: continue # 使用settings默认设置 try: return get_supported_language_variant(settings.LANGUAGE_CODE) except LookupError: return settings.LANGUAGE_CODE
def get_language_from_request(request): """ Analyze 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. The URL path prefix will be checked for a language code. """ lang_code = get_language_from_path(request.path_info) if lang_code is not None: return lang_code supported_lang_codes = get_languages() 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 lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) try: return get_supported_language_variant(lang_code) except LookupError: return None
def process_request(self, request): if settings.APP_CONF.get("force_default_lang"): lang_code = request.session.get(LANGUAGE_SESSION_KEY) supported_lang_codes = get_languages() if lang_code not in supported_lang_codes: request.session[LANGUAGE_SESSION_KEY] = settings.LANGUAGE_CODE return super(LocaleMiddleware, self).process_request(request)
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
def get_language_from_request(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. If check_path is True, the URL path prefix will be checked for a language code, otherwise this is skipped for backwards compatibility. """ global DEVICE_LANGUAGE supported_lang_codes = get_languages() 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 lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME) 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 if not language_code_re.search(accept_lang): continue try: return get_supported_language_variant(accept_lang) except LookupError: continue try: if DEVICE_LANGUAGE is None: DEVICE_LANGUAGE = DeviceSettings.objects.get().language_id return get_supported_language_variant(DEVICE_LANGUAGE) except (DeviceSettings.DoesNotExist, LookupError): pass try: return get_supported_language_variant(settings.LANGUAGE_CODE) except LookupError: return settings.LANGUAGE_CODE
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
def get_language_from_querystring(request): language_code = request.GET.get('lang') language_codes = trans_real.get_languages() if language_code and language_code in language_codes: return language_code