def set_language(request, locale): """Sets the new locale.""" try: locale = Locale.parse(locale) if not has_section(locale): raise UnknownLocaleError(str(locale)) except UnknownLocaleError: raise NotFound() next_url = request.get_localized_next_url(locale) request.locale = locale request.flash(_('The interface language was set to %s. You were also ' 'forwarded to the help section of that language.') % locale.display_name) return redirect(next_url or url_for('kb.overview', lang_code=locale))
def set_language(request, locale): """Sets the new locale.""" try: locale = Locale.parse(locale) if not has_section(locale): raise UnknownLocaleError(str(locale)) except UnknownLocaleError: raise NotFound() next_url = request.get_localized_next_url(locale) request.locale = locale request.flash( _('The interface language was set to %s. You were also ' 'forwarded to the help section of that language.') % locale.display_name) return redirect(next_url or url_for('kb.overview', lang_code=locale))
def prepare_api_request(request): """Prepares the request for API usage.""" request.in_api = True lang = request.args.get('lang') if lang is not None: if not has_section(lang): raise BadRequest(_(u'Unknown language')) request.locale = lang locale = request.args.get('locale') if locale is not None: try: locale = Locale.parse(locale) if not has_locale(locale): raise UnknownLocaleError() except UnknownLocaleError: raise BadRquest(_(u'Unknown locale')) request.view_lang = locale
def _get_locale(self): """The locale of the incoming request. If a locale is unsupported, the default english locale is used. If the locale is assigned it will be stored in the session so that that language changes are persistent. """ if self._locale is not None: return self._locale rv = self.session.get('locale') if rv is not None: rv = Locale.parse(rv) # we could trust the cookie here because it's signed, but we do not # because the configuration could have changed in the meantime. if not has_section(rv): rv = None if rv is None: rv = select_locale(self.accept_languages) self._locale = rv return rv
def __init__(self, environ): RequestBase.__init__(self, environ) before_request_init.emit() self.url_adapter = url_map.bind_to_environ(self.environ) self.view_lang = self.match_exception = None try: self.endpoint, self.view_arguments = self.url_adapter.match() view_lang = self.view_arguments.pop('lang_code', None) if view_lang is not None: try: self.view_lang = Locale.parse(view_lang) if not has_section(self.view_lang): raise UnknownLocaleError(str(self.view_lang)) except UnknownLocaleError: self.view_lang = None self.match_exception = NotFound() except HTTPException, e: self.endpoint = self.view_arguments = None self.match_exception = e