我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.utils.translation.check_for_language()。
def change_locale(request): """ Redirect to a given url while changing the locale in the path The url and the locale code need to be specified in the request parameters. """ next = request.REQUEST.get('next', None) if not next: referrer = request.META.get('HTTP_REFERER', None) if referrer: next = urlsplit(referrer)[2] if not next: next = '/' _, path = utils.strip_path(next) if request.method == 'POST': locale = request.POST.get('locale', None) if locale and check_for_language(locale): if localeurl_settings.USE_SESSION: request.session['django_language'] = locale path = utils.locale_path(path, locale) response = http.HttpResponseRedirect(path) return response
def setlang(request): """ Sets a user's language preference and redirects to a given URL or, by default, back to the previous page. """ next = request.REQUEST.get('next') if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = redirect(next) lang_code = request.REQUEST.get('language', None) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session[LANGUAGE_SESSION_KEY] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, max_age=settings.LANGUAGE_COOKIE_AGE, path=settings.LANGUAGE_COOKIE_PATH, domain=settings.LANGUAGE_COOKIE_DOMAIN) return response
def handle(self, request, data): response = shortcuts.redirect(request.build_absolute_uri()) # Language lang_code = data['language'] if lang_code and translation.check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, expires=_one_year()) # Timezone request.session['django_timezone'] = pytz.timezone( data['timezone']).zone response.set_cookie('django_timezone', data['timezone'], expires=_one_year()) request.session['horizon_pagesize'] = data['pagesize'] response.set_cookie('horizon_pagesize', data['pagesize'], expires=_one_year()) request.session['instance_log_length'] = data['instance_log_length'] response.set_cookie('instance_log_length', data['instance_log_length'], expires=_one_year()) with translation.override(lang_code): messages.success(request, encoding.force_text(_("Settings saved."))) return response
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 set_language(request): """ Redirect to a given url while setting the chosen language in the session or cookie. The url and the language code need to be specified in the request parameters. Since this view changes how the user will see the rest of the site, it must only be accessed as a POST request. If called as a GET request, it will redirect to the page in the request (the 'next' parameter) without changing any state. """ next = request.POST.get('next', request.GET.get('next')) if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = http.HttpResponseRedirect(next) if request.method == 'POST': lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER) if lang_code and check_for_language(lang_code): next_trans = translate_url(next, lang_code) if next_trans != next: response = http.HttpResponseRedirect(next_trans) if hasattr(request, 'session'): request.session[LANGUAGE_SESSION_KEY] = lang_code # Always set cookie response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, max_age=settings.LANGUAGE_COOKIE_AGE, path=settings.LANGUAGE_COOKIE_PATH, domain=settings.LANGUAGE_COOKIE_DOMAIN) return response
def validate_language_id(self, language_id): """ Check that the language_id is supported by Kolibri """ if not check_for_language(language_id): raise serializers.ValidationError(_("Language is not supported by Kolibri")) return language_id