def process_request(self, request): prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if self._is_lang_change(request): # Blank out the locale so that we can set a new one. Remove lang # from the query params so we don't have an infinite loop. prefixer.locale = '' new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), request.GET[k]) for k in request.GET) query.pop('lang') return HttpResponsePermanentRedirect(urlparams(new_path, **query)) if full_path != request.path: query_string = request.META.get('QUERY_STRING', '') full_path = urllib.quote(full_path.encode('utf-8')) if query_string: full_path = '%s?%s' % (full_path, query_string) response = HttpResponsePermanentRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = urlresolvers.split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.locale = prefixer.locale tower.activate(prefixer.locale)
def process_request(self, request): prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if self._is_lang_change(request): # Blank out the locale so that we can set a new one. Remove lang # from the query params so we don't have an infinite loop. prefixer.locale = '' new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), request.GET[k]) for k in request.GET) query.pop('lang') return HttpResponsePermanentRedirect(urlparams(new_path, **query)) if full_path != request.path: query_string = request.META.get('QUERY_STRING', '') full_path = urllib.quote(full_path.encode('utf-8')) if query_string: full_path = '%s?%s' % (full_path, query_string) response = HttpResponsePermanentRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = urlresolvers.split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.locale = prefixer.locale translation.activate(prefixer.locale)
def request(self, **request): """Make a request, ensuring it has a locale and a user agent.""" # Fall back to defaults as in the superclass's implementation: path = request.get("PATH_INFO", self.defaults.get("PATH_INFO", "/")) locale, shortened = split_path(path) if not locale: request["PATH_INFO"] = "/%s/%s" % (settings.LANGUAGE_CODE, shortened) if "HTTP_USER_AGENT" not in request and self.user_agent: request["HTTP_USER_AGENT"] = self.user_agent return super(LocalizingClient, self).request(**request)
def request(self, **request): """Make a request, ensuring it has a locale and a user agent.""" # Fall back to defaults as in the superclass's implementation: path = request.get('PATH_INFO', self.defaults.get('PATH_INFO', '/')) locale, shortened = split_path(path) if not locale and not is_supported_nonlocale(path): request['PATH_INFO'] = '/%s/%s' % (settings.LANGUAGE_CODE, shortened) if 'HTTP_USER_AGENT' not in request and self.user_agent: request['HTTP_USER_AGENT'] = self.user_agent return super(LocalizingClient, self).request(**request)