def _get_locale_from_current_user(self, request): if self._use_session(request.user): locale = request.session.get("locale_id") return locale if is_supported(locale) else None else: locale = getattr(request.user, "locale_id", None) return locale if is_supported(locale) else None
def set_locale(request): """ Redirect to the referrer URL while setting the chosen language in the session. The new language needs to be specified in the request body as `new_locale`. Since this view changes how the user will see the rest of the site, it must only be accessed as a POST request. Based on `django.views.i18n.set_language` """ next = request.POST.get("next", "/") if not is_safe_url(url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure()): next = "/" response = HttpResponseRedirect(next) locale = request.POST.get("new_locale") if is_supported(locale): request.locale_id = locale # Save current locale in a cookie. set_language_cookie(response, locale) if request.user.is_authenticated: request.user.user_profile.locale_id = locale request.user.user_profile.save(update_fields=["locale_id"]) return response
def set_locale(request): """ Redirect to the referrer URL while setting the chosen language in the session. The new language needs to be specified in the request body as `new_locale`. Since this view changes how the user will see the rest of the site, it must only be accessed as a POST request. Based on `django.views.i18n.set_language` """ next = request.POST.get("next", "/") if not is_safe_url(url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure()): next = "/" locale = request.POST.get("new_locale") if is_supported(locale): request.session["locale_id"] = locale return HttpResponseRedirect(next)
def _get_locale_from_language_header(self) -> Optional[str]: # Logic adapted from django.utils.translation.real_trans.get_language_from_request for accept_lang, unused in parse_accept_lang_header( self.accept_language_header): if accept_lang == "*": break if not language_code_re.search(accept_lang): continue try: locale_id = get_supported_language_variant(accept_lang) except LookupError: continue if is_supported(locale_id): return locale_id else: continue return None
def _decide_locale(self, request): """ Search for the locale to use. We search in the following places, in order 1. In the current request attributes, so that the user can change it any time. This is meant for testing purposes and does not affect the preferences of logged-in users. 2. If the user is logged in and has ever set a locale preference, in the user's profile; otherwise, in the current session. 3. In the Accept-Language header sent by the browser 4. The default locale If the locale found at some step is not supported, we proceed to the next step If the user is not logged in or has never set a locale preference, the selected locale is saved in session """ locale = (self._get_locale_from_query(request) or self._get_locale_from_current_user(request) or self._get_locale_from_language_header(request)) return locale if is_supported(locale) else default_locale
def _only_if_supported(self, locale_id: Optional[str]) -> Optional[str]: return locale_id if is_supported(locale_id) else None
def _get_locale_from_query(self, request): locale = request.GET.get("locale") return locale if is_supported(locale) else None