From 89ed50238a3f6bd7a1675506c3acbf8785d85006 Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Mon, 18 Jul 2022 16:56:50 -0700 Subject: [PATCH] Fix #11890: Append slash at locale root URL --- lib/l10n_utils/__init__.py | 5 ++++- lib/l10n_utils/tests/test_base.py | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/l10n_utils/__init__.py b/lib/l10n_utils/__init__.py index ba2804a09a..0c9d362df9 100644 --- a/lib/l10n_utils/__init__.py +++ b/lib/l10n_utils/__init__.py @@ -166,7 +166,10 @@ def render(request, template, context=None, ftl_files=None, activation_files=Non # Does that path's locale match the request's locale? if locale in translations: - if locale != request.path.lstrip("/").partition("/")[0]: + # Redirect to the locale if: + # - The URL is the root path but is missing the trailing slash OR + # - The locale isn't in current prefix in the URL. + if request.path == f"/{locale}" or locale != request.path.lstrip("/").partition("/")[0]: return redirect_to_locale(request, locale) else: return redirect_to_best_locale(request, translations) diff --git a/lib/l10n_utils/tests/test_base.py b/lib/l10n_utils/tests/test_base.py index aef7bae2c4..d9230a2845 100644 --- a/lib/l10n_utils/tests/test_base.py +++ b/lib/l10n_utils/tests/test_base.py @@ -62,6 +62,8 @@ class TestRender(TestCase): self._test("/firefox/new/", template, "", "", 404, active_locales=locales) # Test that a locale+path and no accept language header returns 200 as long as the locales are supported. + self._test("/en-US", template, "", "", 302, "/en-US/", active_locales=locales) + self._test("/en-US/", template, "", "", 200, active_locales=locales) self._test("/en-US/firefox/new/", template, "", "", 200, active_locales=locales) self._test("/en-GB/firefox/new/", template, "", "", 200, active_locales=locales) self._test("/fr/firefox/new/", template, "", "", 200, active_locales=locales)