我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用django.utils.translation.get_language_from_request()。
def create(cls, request=None, **kwargs): create_email = kwargs.pop("create_email", True) confirm_email = kwargs.pop("confirm_email", None) account = cls(**kwargs) if "language" not in kwargs: if request is None: account.language = settings.LANGUAGE_CODE else: account.language = translation.get_language_from_request(request, check_path=True) account.save() if create_email and account.user.email: kwargs = {"primary": True} if confirm_email is not None: kwargs["confirm"] = confirm_email EmailAddress.objects.add_email(account.user, account.user.email, **kwargs) return account
def get_global_context(request): """ Get the set of variables that are needed by default across views. """ platform_name = get_configuration_value("PLATFORM_NAME", settings.PLATFORM_NAME) return { 'LMS_SEGMENT_KEY': settings.LMS_SEGMENT_KEY, 'LANGUAGE_CODE': get_language_from_request(request), 'tagline': get_configuration_value( "ENTERPRISE_TAGLINE", getattr(settings, "ENTERPRISE_TAGLINE", '') # Remove the `getattr` when setting is upstreamed. ), 'platform_description': get_configuration_value( "PLATFORM_DESCRIPTION", getattr(settings, "PLATFORM_DESCRIPTION", '') # Remove `getattr` when variable is upstreamed. ), 'LMS_ROOT_URL': settings.LMS_ROOT_URL, 'platform_name': platform_name, 'header_logo_alt_text': _('{platform_name} home page').format(platform_name=platform_name), }
def add_logout_reason(request, response, reason): # Store the translated string in the cookie lang = translation.get_language_from_request(request) with translation.override(lang): reason = six.text_type(reason) if six.PY2: reason = reason.encode('utf-8') response.set_cookie('logout_reason', reason, max_age=10)
def process_request(self, request): language = translation.get_language_from_request( request, check_path=self.is_language_prefix_patterns_used) translation.activate(language) request.LANGUAGE_CODE = translation.get_language()
def get_language_from_request(request, check_path=False): """Try to get the user's preferred language by first checking the cookie and then by checking the HTTP language headers. If all fails, try fall back to default language. """ supported = dict(supported_langs()) for lang_getter in (get_lang_from_session, get_lang_from_cookie, get_lang_from_http_header): lang = lang_getter(request, supported) if lang is not None: return lang from django.conf import settings return settings.LANGUAGE_CODE
def hijack_translation(): """Sabotage Django's fascist linguistical regime.""" # Override functions that check if language is known to Django translation.check_for_language = lambda lang_code: True trans_real.check_for_language = lambda lang_code: True translation.get_language_from_request = get_language_from_request # Override django's inadequate bidi detection translation.get_language_bidi = get_language_bidi # We hijack gettext functions to install the safe variable formatting # override override_gettext(gettext)
def language(self): return get_language_from_request(self.request, check_path=True)
def __init__(self, request=None): self.user = AnonymousUser() self.timezone = settings.TIME_ZONE if request is None: self.language = settings.LANGUAGE_CODE else: self.language = translation.get_language_from_request(request, check_path=True)
def get_language_for_user(self, request): if request.user.is_authenticated(): try: account = Account.objects.get(user=request.user) return account.language except Account.DoesNotExist: pass return translation.get_language_from_request(request)
def process_request(self, request): check_path = self.is_language_prefix_patterns_used() language = translation.get_language_from_request( request, check_path=check_path) translation.activate(language) request.LANGUAGE_CODE = translation.get_language()
def process_request(self, request): language = translation.get_language_from_request(request) translation.activate(language) request.LANGUAGE_CODE = translation.get_language()
def process_request(self, request): urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF) i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf) language = translation.get_language_from_request(request, check_path=i18n_patterns_used) language_from_path = translation.get_language_from_path(request.path_info) if not language_from_path and i18n_patterns_used and not prefixed_default_language: language = settings.LANGUAGE_CODE translation.activate(language) request.LANGUAGE_CODE = translation.get_language()
def dispatch(self, request, *args, **kwargs): self.language_code = get_language_from_request(request, check_path=True) response = super(WizardViewMixin, self).dispatch( request, *args, **kwargs) return response
def __init__(self, request=None): self.user = AnonymousUser() self.timezone = settings.TIME_ZONE if request is not None: self.language = get_language_from_request(request) else: self.language = settings.LANGUAGE_CODE