def process_request(self, request): prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if full_path != request.path: query_string = request.META.get('QUERY_STRING', '') full_path = urllib.parse.quote(full_path.encode('utf-8')) if query_string: full_path = '?'.join( [full_path, unquote(query_string, errors='ignore')]) 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 or settings.LANGUAGE_CODE)
def process_request(self, request): prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) full_path = prefixer.fix(prefixer.shortened_path) 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 = '?'.join( [full_path, force_text(query_string, errors='ignore')]) 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 or settings.LANGUAGE_CODE)
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 = '?'.join( [full_path, force_text(query_string, errors='ignore')]) 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 or settings.LANGUAGE_CODE)
def process_request(self, request): prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if not (request.path in settings.SUPPORTED_LOCALE_IGNORE or full_path == request.path): query_string = request.META.get("QUERY_STRING", "") full_path = urllib.parse.quote(full_path.encode("utf-8")) if query_string: full_path = "?".join( [full_path, unquote(query_string, errors="ignore")]) 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 or settings.LANGUAGE_CODE)
def test_activate_deactivate(self): """Should activate and deactivate languages""" self.assertEqual(translation.get_language(), settings.LANGUAGE_CODE) translation.activate('de') self.assertEqual(translation.get_language(), 'de') self.assertNotEqual(translation.get_language(), settings.LANGUAGE_CODE) translation.deactivate() self.assertEqual(translation.get_language(), settings.LANGUAGE_CODE)
def activate(self, locale): """Context manager that temporarily activates a locale.""" old_prefix = get_url_prefix() old_locale = translation.get_language() rf = RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale,)))) translation.activate(locale) yield set_url_prefix(old_prefix) translation.activate(old_locale)
def activate(self, locale): """Context manager that temporarily activates a locale.""" old_prefix = get_url_prefix() old_locale = translation.get_language() rf = RequestFactory() set_url_prefix(Prefixer(rf.get('/%s/' % (locale, )))) translation.activate(locale) yield set_url_prefix(old_prefix) translation.activate(old_locale)
def test_get_language_bidi(self): """Should return true if the base lang (before the dash) is "he", "ar", "fa", or "ur". List of left-to-right languages are in Django global settings and very unlikely to change. """ translation.activate('en-GB') self.assertFalse(translation.get_language_bidi()) translation.activate('de') self.assertFalse(translation.get_language_bidi()) translation.activate('ar') self.assertTrue(translation.get_language_bidi()) translation.activate('ur-PK') self.assertTrue(translation.get_language_bidi())
def test_ftl_view_util_active_locale(self): """Should use activated locale if not provided""" translation.activate("fr") assert fluent.ftl("fluent-title") == "Title in French"
def test_get_language(self): # nothing set, should return default lang self.assertEqual(translation.get_language(), settings.LANGUAGE_CODE) translation.activate('non-default') self.assertEqual(translation.get_language(), 'non-default')
def setUp(self): translation.activate('en-US') self.factory = RequestFactory()
def setUp(self): translation.activate("en-US") self.factory = RequestFactory() translation.activate("en-US")