2011-06-16 01:29:10 +08:00
|
|
|
import os
|
|
|
|
|
2016-03-06 03:20:40 +08:00
|
|
|
from django.conf import settings
|
2011-06-16 01:29:10 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2019-09-27 01:06:35 +08:00
|
|
|
from django.http import HttpResponse, HttpResponsePermanentRedirect
|
2012-11-17 20:14:12 +08:00
|
|
|
from django.middleware.locale import LocaleMiddleware
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template import Context, Template
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-12-15 01:36:09 +08:00
|
|
|
from django.test.client import RequestFactory
|
2015-03-15 18:26:47 +08:00
|
|
|
from django.test.utils import override_script_prefix
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import clear_url_caches, reverse, translate_url
|
2011-06-16 01:29:10 +08:00
|
|
|
from django.utils import translation
|
|
|
|
|
|
|
|
|
2012-11-17 20:14:12 +08:00
|
|
|
class PermanentRedirectLocaleMiddleWare(LocaleMiddleware):
|
|
|
|
response_redirect_class = HttpResponsePermanentRedirect
|
|
|
|
|
|
|
|
|
2012-03-30 17:08:29 +08:00
|
|
|
@override_settings(
|
2011-06-16 01:29:10 +08:00
|
|
|
USE_I18N=True,
|
2015-01-22 00:55:57 +08:00
|
|
|
LOCALE_PATHS=[
|
2017-01-20 21:01:02 +08:00
|
|
|
os.path.join(os.path.dirname(__file__), "locale"),
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2013-02-24 21:43:45 +08:00
|
|
|
LANGUAGE_CODE="en-us",
|
2015-01-22 00:55:57 +08:00
|
|
|
LANGUAGES=[
|
2011-06-16 01:29:10 +08:00
|
|
|
("nl", "Dutch"),
|
|
|
|
("en", "English"),
|
|
|
|
("pt-br", "Brazilian Portuguese"),
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2011-06-16 01:29:10 +08:00
|
|
|
"django.middleware.locale.LocaleMiddleware",
|
|
|
|
"django.middleware.common.CommonMiddleware",
|
2022-02-04 03:24:19 +08:00
|
|
|
],
|
2014-04-05 14:04:46 +08:00
|
|
|
ROOT_URLCONF="i18n.patterns.urls.default",
|
2014-12-18 05:51:42 +08:00
|
|
|
TEMPLATES=[
|
2022-02-04 03:24:19 +08:00
|
|
|
{
|
2014-12-18 05:51:42 +08:00
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
2017-01-20 21:01:02 +08:00
|
|
|
"DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
|
2022-02-04 03:24:19 +08:00
|
|
|
"OPTIONS": {
|
2014-12-18 05:51:42 +08:00
|
|
|
"context_processors": [
|
|
|
|
"django.template.context_processors.i18n",
|
2022-02-04 03:24:19 +08:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2012-03-30 17:08:29 +08:00
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class URLTestCaseBase(SimpleTestCase):
|
2012-03-30 17:08:29 +08:00
|
|
|
"""
|
|
|
|
TestCase base-class for the URL tests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Make sure the cache is empty before we are doing our tests.
|
|
|
|
clear_url_caches()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Make sure we will leave an empty cache for other testcases.
|
|
|
|
clear_url_caches()
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
class URLPrefixTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Tests if the `i18n_patterns` is adding the prefix correctly.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_not_prefixed(self):
|
|
|
|
with translation.override("en"):
|
|
|
|
self.assertEqual(reverse("not-prefixed"), "/not-prefixed/")
|
2013-06-04 22:19:27 +08:00
|
|
|
self.assertEqual(
|
|
|
|
reverse("not-prefixed-included-url"), "/not-prefixed-include/foo/"
|
|
|
|
)
|
2011-06-16 01:29:10 +08:00
|
|
|
with translation.override("nl"):
|
|
|
|
self.assertEqual(reverse("not-prefixed"), "/not-prefixed/")
|
2013-06-04 22:19:27 +08:00
|
|
|
self.assertEqual(
|
|
|
|
reverse("not-prefixed-included-url"), "/not-prefixed-include/foo/"
|
|
|
|
)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
def test_prefixed(self):
|
|
|
|
with translation.override("en"):
|
|
|
|
self.assertEqual(reverse("prefixed"), "/en/prefixed/")
|
|
|
|
with translation.override("nl"):
|
|
|
|
self.assertEqual(reverse("prefixed"), "/nl/prefixed/")
|
2016-03-06 03:20:40 +08:00
|
|
|
with translation.override(None):
|
|
|
|
self.assertEqual(
|
|
|
|
reverse("prefixed"), "/%s/prefixed/" % settings.LANGUAGE_CODE
|
|
|
|
)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2013-02-26 20:19:18 +08:00
|
|
|
@override_settings(ROOT_URLCONF="i18n.patterns.urls.wrong")
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_invalid_prefix_use(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "Using i18n_patterns in an included URLconf is not allowed."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
reverse("account:register")
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="i18n.patterns.urls.disabled")
|
2011-06-16 01:29:10 +08:00
|
|
|
class URLDisabledTests(URLTestCaseBase):
|
|
|
|
@override_settings(USE_I18N=False)
|
|
|
|
def test_prefixed_i18n_disabled(self):
|
|
|
|
with translation.override("en"):
|
|
|
|
self.assertEqual(reverse("prefixed"), "/prefixed/")
|
|
|
|
with translation.override("nl"):
|
|
|
|
self.assertEqual(reverse("prefixed"), "/prefixed/")
|
|
|
|
|
|
|
|
|
2015-12-15 01:36:09 +08:00
|
|
|
class RequestURLConfTests(SimpleTestCase):
|
|
|
|
@override_settings(ROOT_URLCONF="i18n.patterns.urls.path_unused")
|
|
|
|
def test_request_urlconf_considered(self):
|
|
|
|
request = RequestFactory().get("/nl/")
|
|
|
|
request.urlconf = "i18n.patterns.urls.default"
|
2019-09-27 01:06:35 +08:00
|
|
|
middleware = LocaleMiddleware(lambda req: HttpResponse())
|
2015-12-15 01:36:09 +08:00
|
|
|
with translation.override("nl"):
|
|
|
|
middleware.process_request(request)
|
|
|
|
self.assertEqual(request.LANGUAGE_CODE, "nl")
|
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="i18n.patterns.urls.path_unused")
|
2012-02-18 21:37:30 +08:00
|
|
|
class PathUnusedTests(URLTestCaseBase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
If no i18n_patterns is used in root URLconfs, then no language activation
|
|
|
|
activation happens based on url prefix.
|
2012-02-18 21:37:30 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_no_lang_activate(self):
|
|
|
|
response = self.client.get("/nl/foo/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["content-language"], "en")
|
2012-02-18 21:37:30 +08:00
|
|
|
self.assertEqual(response.context["LANGUAGE_CODE"], "en")
|
|
|
|
|
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
class URLTranslationTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Tests if the pattern-strings are translated correctly (within the
|
|
|
|
`i18n_patterns` and the normal `patterns` function).
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_no_prefix_translated(self):
|
|
|
|
with translation.override("en"):
|
|
|
|
self.assertEqual(reverse("no-prefix-translated"), "/translated/")
|
|
|
|
self.assertEqual(
|
|
|
|
reverse("no-prefix-translated-slug", kwargs={"slug": "yeah"}),
|
|
|
|
"/translated/yeah/",
|
|
|
|
)
|
|
|
|
|
|
|
|
with translation.override("nl"):
|
|
|
|
self.assertEqual(reverse("no-prefix-translated"), "/vertaald/")
|
|
|
|
self.assertEqual(
|
|
|
|
reverse("no-prefix-translated-slug", kwargs={"slug": "yeah"}),
|
|
|
|
"/vertaald/yeah/",
|
|
|
|
)
|
|
|
|
|
|
|
|
with translation.override("pt-br"):
|
|
|
|
self.assertEqual(reverse("no-prefix-translated"), "/traduzidos/")
|
|
|
|
self.assertEqual(
|
|
|
|
reverse("no-prefix-translated-slug", kwargs={"slug": "yeah"}),
|
|
|
|
"/traduzidos/yeah/",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_users_url(self):
|
|
|
|
with translation.override("en"):
|
|
|
|
self.assertEqual(reverse("users"), "/en/users/")
|
|
|
|
|
|
|
|
with translation.override("nl"):
|
|
|
|
self.assertEqual(reverse("users"), "/nl/gebruikers/")
|
2013-01-12 04:27:51 +08:00
|
|
|
self.assertEqual(reverse("prefixed_xml"), "/nl/prefixed.xml")
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
with translation.override("pt-br"):
|
|
|
|
self.assertEqual(reverse("users"), "/pt-br/usuarios/")
|
|
|
|
|
2015-03-12 16:32:29 +08:00
|
|
|
def test_translate_url_utility(self):
|
|
|
|
with translation.override("en"):
|
2017-02-03 09:43:21 +08:00
|
|
|
self.assertEqual(
|
|
|
|
translate_url("/en/nonexistent/", "nl"), "/en/nonexistent/"
|
|
|
|
)
|
2015-03-12 16:32:29 +08:00
|
|
|
self.assertEqual(translate_url("/en/users/", "nl"), "/nl/gebruikers/")
|
|
|
|
# Namespaced URL
|
2017-09-14 20:26:38 +08:00
|
|
|
self.assertEqual(
|
|
|
|
translate_url("/en/account/register/", "nl"), "/nl/profiel/registreren/"
|
|
|
|
)
|
2016-10-21 01:29:04 +08:00
|
|
|
# path() URL pattern
|
|
|
|
self.assertEqual(
|
|
|
|
translate_url("/en/account/register-as-path/", "nl"),
|
|
|
|
"/nl/profiel/registreren-als-pad/",
|
|
|
|
)
|
2015-03-12 16:32:29 +08:00
|
|
|
self.assertEqual(translation.get_language(), "en")
|
2019-06-21 23:37:41 +08:00
|
|
|
# URL with parameters.
|
2019-04-25 04:51:47 +08:00
|
|
|
self.assertEqual(
|
|
|
|
translate_url("/en/with-arguments/regular-argument/", "nl"),
|
|
|
|
"/nl/with-arguments/regular-argument/",
|
|
|
|
)
|
2019-06-21 23:37:41 +08:00
|
|
|
self.assertEqual(
|
|
|
|
translate_url(
|
|
|
|
"/en/with-arguments/regular-argument/optional.html", "nl"
|
|
|
|
),
|
|
|
|
"/nl/with-arguments/regular-argument/optional.html",
|
|
|
|
)
|
2015-03-12 16:32:29 +08:00
|
|
|
|
|
|
|
with translation.override("nl"):
|
|
|
|
self.assertEqual(translate_url("/nl/gebruikers/", "en"), "/en/users/")
|
|
|
|
self.assertEqual(translation.get_language(), "nl")
|
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
class URLNamespaceTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Tests if the translations are still working within namespaces.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_account_register(self):
|
|
|
|
with translation.override("en"):
|
|
|
|
self.assertEqual(reverse("account:register"), "/en/account/register/")
|
2016-10-21 01:29:04 +08:00
|
|
|
self.assertEqual(
|
|
|
|
reverse("account:register-as-path"), "/en/account/register-as-path/"
|
|
|
|
)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
with translation.override("nl"):
|
2017-09-14 20:26:38 +08:00
|
|
|
self.assertEqual(reverse("account:register"), "/nl/profiel/registreren/")
|
2016-10-21 01:29:04 +08:00
|
|
|
self.assertEqual(
|
|
|
|
reverse("account:register-as-path"), "/nl/profiel/registreren-als-pad/"
|
|
|
|
)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
class URLRedirectTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Tests if the user gets redirected to the right URL when there is no
|
|
|
|
language-prefix in the request URL.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_no_prefix_response(self):
|
|
|
|
response = self.client.get("/not-prefixed/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_en_redirect(self):
|
|
|
|
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="en")
|
2012-03-02 19:07:36 +08:00
|
|
|
self.assertRedirects(response, "/en/account/register/")
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
response = self.client.get(response.headers["location"])
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_en_redirect_wrong_url(self):
|
2017-09-14 20:26:38 +08:00
|
|
|
response = self.client.get("/profiel/registreren/", HTTP_ACCEPT_LANGUAGE="en")
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_nl_redirect(self):
|
2017-09-14 20:26:38 +08:00
|
|
|
response = self.client.get("/profiel/registreren/", HTTP_ACCEPT_LANGUAGE="nl")
|
|
|
|
self.assertRedirects(response, "/nl/profiel/registreren/")
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
response = self.client.get(response.headers["location"])
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_nl_redirect_wrong_url(self):
|
|
|
|
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="nl")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_pt_br_redirect(self):
|
|
|
|
response = self.client.get("/conta/registre-se/", HTTP_ACCEPT_LANGUAGE="pt-br")
|
2012-03-02 19:07:36 +08:00
|
|
|
self.assertRedirects(response, "/pt-br/conta/registre-se/")
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
response = self.client.get(response.headers["location"])
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2013-02-24 21:43:45 +08:00
|
|
|
def test_pl_pl_redirect(self):
|
|
|
|
# language from outside of the supported LANGUAGES list
|
|
|
|
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="pl-pl")
|
|
|
|
self.assertRedirects(response, "/en/account/register/")
|
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
response = self.client.get(response.headers["location"])
|
2013-02-24 21:43:45 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2012-11-17 20:14:12 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2012-11-17 20:14:12 +08:00
|
|
|
"i18n.patterns.tests.PermanentRedirectLocaleMiddleWare",
|
|
|
|
"django.middleware.common.CommonMiddleware",
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2012-11-17 20:14:12 +08:00
|
|
|
)
|
|
|
|
def test_custom_redirect_class(self):
|
|
|
|
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="en")
|
|
|
|
self.assertRedirects(response, "/en/account/register/", 301)
|
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2013-02-24 02:41:33 +08:00
|
|
|
class URLVaryAcceptLanguageTests(URLTestCaseBase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
'Accept-Language' is not added to the Vary header when using prefixed URLs.
|
2013-02-24 02:41:33 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-02-24 02:41:33 +08:00
|
|
|
def test_no_prefix_response(self):
|
|
|
|
response = self.client.get("/not-prefixed/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.get("Vary"), "Accept-Language")
|
|
|
|
|
|
|
|
def test_en_redirect(self):
|
2021-05-19 19:19:23 +08:00
|
|
|
"""
|
|
|
|
The redirect to a prefixed URL depends on 'Accept-Language' and
|
|
|
|
'Cookie', but once prefixed no header is set.
|
|
|
|
"""
|
2013-02-24 02:41:33 +08:00
|
|
|
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="en")
|
|
|
|
self.assertRedirects(response, "/en/account/register/")
|
2021-05-19 19:19:23 +08:00
|
|
|
self.assertEqual(response.get("Vary"), "Accept-Language, Cookie")
|
2013-02-24 02:41:33 +08:00
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
response = self.client.get(response.headers["location"])
|
2013-02-24 02:41:33 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertFalse(response.get("Vary"))
|
|
|
|
|
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
class URLRedirectWithoutTrailingSlashTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Tests the redirect when the requested URL doesn't end with a slash
|
|
|
|
(`settings.APPEND_SLASH=True`).
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_not_prefixed_redirect(self):
|
|
|
|
response = self.client.get("/not-prefixed", HTTP_ACCEPT_LANGUAGE="en")
|
2012-03-02 19:07:36 +08:00
|
|
|
self.assertRedirects(response, "/not-prefixed/", 301)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
def test_en_redirect(self):
|
2012-03-03 06:35:26 +08:00
|
|
|
response = self.client.get(
|
|
|
|
"/account/register", HTTP_ACCEPT_LANGUAGE="en", follow=True
|
|
|
|
)
|
2015-03-23 03:04:31 +08:00
|
|
|
# We only want one redirect, bypassing CommonMiddleware
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(response.redirect_chain, [("/en/account/register/", 302)])
|
2012-03-03 06:35:26 +08:00
|
|
|
self.assertRedirects(response, "/en/account/register/", 302)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2013-01-12 04:27:51 +08:00
|
|
|
response = self.client.get(
|
|
|
|
"/prefixed.xml", HTTP_ACCEPT_LANGUAGE="en", follow=True
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, "/en/prefixed.xml", 302)
|
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
class URLRedirectWithoutTrailingSlashSettingTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Tests the redirect when the requested URL doesn't end with a slash
|
|
|
|
(`settings.APPEND_SLASH=False`).
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
@override_settings(APPEND_SLASH=False)
|
|
|
|
def test_not_prefixed_redirect(self):
|
|
|
|
response = self.client.get("/not-prefixed", HTTP_ACCEPT_LANGUAGE="en")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
@override_settings(APPEND_SLASH=False)
|
|
|
|
def test_en_redirect(self):
|
2012-03-02 19:07:36 +08:00
|
|
|
response = self.client.get(
|
|
|
|
"/account/register-without-slash", HTTP_ACCEPT_LANGUAGE="en"
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, "/en/account/register-without-slash", 302)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
2020-07-14 19:32:24 +08:00
|
|
|
response = self.client.get(response.headers["location"])
|
2012-03-02 19:07:36 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2011-06-16 01:29:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
class URLResponseTests(URLTestCaseBase):
|
2018-02-21 22:30:56 +08:00
|
|
|
"""Tests if the response has the correct language code."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-06-16 01:29:10 +08:00
|
|
|
def test_not_prefixed_with_prefix(self):
|
|
|
|
response = self.client.get("/en/not-prefixed/")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_en_url(self):
|
|
|
|
response = self.client.get("/en/account/register/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["content-language"], "en")
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.context["LANGUAGE_CODE"], "en")
|
|
|
|
|
|
|
|
def test_nl_url(self):
|
2017-09-14 20:26:38 +08:00
|
|
|
response = self.client.get("/nl/profiel/registreren/")
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["content-language"], "nl")
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.context["LANGUAGE_CODE"], "nl")
|
|
|
|
|
|
|
|
def test_wrong_en_prefix(self):
|
2017-09-14 20:26:38 +08:00
|
|
|
response = self.client.get("/en/profiel/registreren/")
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_wrong_nl_prefix(self):
|
|
|
|
response = self.client.get("/nl/account/register/")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_pt_br_url(self):
|
|
|
|
response = self.client.get("/pt-br/conta/registre-se/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["content-language"], "pt-br")
|
2011-06-16 01:29:10 +08:00
|
|
|
self.assertEqual(response.context["LANGUAGE_CODE"], "pt-br")
|
2011-07-04 17:52:06 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
def test_en_path(self):
|
|
|
|
response = self.client.get("/en/account/register-as-path/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["content-language"], "en")
|
2016-10-21 01:29:04 +08:00
|
|
|
self.assertEqual(response.context["LANGUAGE_CODE"], "en")
|
|
|
|
|
|
|
|
def test_nl_path(self):
|
|
|
|
response = self.client.get("/nl/profiel/registreren-als-pad/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["content-language"], "nl")
|
2016-10-21 01:29:04 +08:00
|
|
|
self.assertEqual(response.context["LANGUAGE_CODE"], "nl")
|
|
|
|
|
2011-07-04 17:52:06 +08:00
|
|
|
|
2014-08-14 21:32:50 +08:00
|
|
|
class URLRedirectWithScriptAliasTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
#21579 - LocaleMiddleware should respect the script prefix.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2014-08-14 21:32:50 +08:00
|
|
|
def test_language_prefix_with_script_prefix(self):
|
2015-03-15 18:26:47 +08:00
|
|
|
prefix = "/script_prefix"
|
|
|
|
with override_script_prefix(prefix):
|
|
|
|
response = self.client.get(
|
|
|
|
"/prefixed/", HTTP_ACCEPT_LANGUAGE="en", SCRIPT_NAME=prefix
|
|
|
|
)
|
|
|
|
self.assertRedirects(
|
|
|
|
response, "%s/en/prefixed/" % prefix, target_status_code=404
|
|
|
|
)
|
2014-08-14 21:32:50 +08:00
|
|
|
|
|
|
|
|
2011-07-04 17:52:06 +08:00
|
|
|
class URLTagTests(URLTestCaseBase):
|
|
|
|
"""
|
|
|
|
Test if the language tag works.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-07-04 17:52:06 +08:00
|
|
|
def test_strings_only(self):
|
|
|
|
t = Template(
|
|
|
|
"""{% load i18n %}
|
2012-04-25 03:55:52 +08:00
|
|
|
{% language 'nl' %}{% url 'no-prefix-translated' %}{% endlanguage %}
|
|
|
|
{% language 'pt-br' %}{% url 'no-prefix-translated' %}{% endlanguage %}"""
|
|
|
|
)
|
2011-07-04 17:52:06 +08:00
|
|
|
self.assertEqual(
|
|
|
|
t.render(Context({})).strip().split(), ["/vertaald/", "/traduzidos/"]
|
2012-06-08 00:08:47 +08:00
|
|
|
)
|
2011-07-04 17:52:06 +08:00
|
|
|
|
|
|
|
def test_context(self):
|
2013-10-27 03:15:03 +08:00
|
|
|
ctx = Context({"lang1": "nl", "lang2": "pt-br"})
|
2011-07-04 17:52:06 +08:00
|
|
|
tpl = Template(
|
|
|
|
"""{% load i18n %}
|
2012-04-25 03:55:52 +08:00
|
|
|
{% language lang1 %}{% url 'no-prefix-translated' %}{% endlanguage %}
|
|
|
|
{% language lang2 %}{% url 'no-prefix-translated' %}{% endlanguage %}"""
|
|
|
|
)
|
2011-07-04 17:52:06 +08:00
|
|
|
self.assertEqual(
|
|
|
|
tpl.render(ctx).strip().split(), ["/vertaald/", "/traduzidos/"]
|
2012-06-08 00:08:47 +08:00
|
|
|
)
|
2011-07-04 17:52:06 +08:00
|
|
|
|
|
|
|
def test_args(self):
|
|
|
|
tpl = Template(
|
|
|
|
"""
|
|
|
|
{% load i18n %}
|
2012-04-25 03:55:52 +08:00
|
|
|
{% language 'nl' %}
|
|
|
|
{% url 'no-prefix-translated-slug' 'apo' %}{% endlanguage %}
|
|
|
|
{% language 'pt-br' %}
|
|
|
|
{% url 'no-prefix-translated-slug' 'apo' %}{% endlanguage %}
|
|
|
|
"""
|
|
|
|
)
|
2011-07-04 17:52:06 +08:00
|
|
|
self.assertEqual(
|
|
|
|
tpl.render(Context({})).strip().split(),
|
2012-06-08 00:08:47 +08:00
|
|
|
["/vertaald/apo/", "/traduzidos/apo/"],
|
|
|
|
)
|
2011-07-04 17:52:06 +08:00
|
|
|
|
|
|
|
def test_kwargs(self):
|
|
|
|
tpl = Template(
|
|
|
|
"""
|
|
|
|
{% load i18n %}
|
|
|
|
{% language 'nl' %}
|
|
|
|
{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}
|
|
|
|
{% language 'pt-br' %}
|
|
|
|
{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
tpl.render(Context({})).strip().split(),
|
2012-06-08 00:08:47 +08:00
|
|
|
["/vertaald/apo/", "/traduzidos/apo/"],
|
|
|
|
)
|