Skip to content

Commit

Permalink
Simplify language chooser
Browse files Browse the repository at this point in the history
- Allows usage of i18n_patterns with prefix_default_language=False
- Change templatetag from simple tag to an inclusion tag
- Reduces complexity by relying on Django's behavior in the
  set_language view: it will translate any url passed as 'next'.
  This behavior has been present since Django 1.9.
- Remove individual forms for each language

Fixes fabiocaccamo#327

Reference: django/django@aa5ab11
  • Loading branch information
julianwachholz committed Oct 20, 2023
1 parent 0f69ccb commit 06758e9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 121 deletions.
3 changes: 1 addition & 2 deletions admin_interface/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ <h1 id="site-name">
{{ block.super }}
{% get_admin_interface_theme as theme %}
{% if theme.language_chooser_active %}
{% get_admin_interface_languages as languages %}
{% include "admin_interface/language_chooser.html" %}
{% admin_interface_language_chooser %}
{% endif %}
{% endblock %}
19 changes: 8 additions & 11 deletions admin_interface/templates/admin_interface/language_chooser.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{% load admin_interface_tags %}

{% if languages %}
{% if set_language_url %}
<div class="language-chooser {% if theme.language_chooser_control == 'minimal-select' %}minimal {% endif %}">
{% for language in languages %}
<form class="language-chooser-hidden-form" id="language-chooser-hidden-form-{{ language.code }}" action="{{ language.activation_url }}" method="POST">
<form class="language-chooser-select-form" action="{{ set_language_url }}" method="POST">
{% csrf_token %}
<input name="language" type="hidden" value="{{ language.code }}">
</form>
{% endfor %}
<form class="language-chooser-select-form">
{% csrf_token %}
<select name="language" onchange="document.getElementById(String('language-chooser-hidden-form-' + this.value)).submit();">
{% for language in languages %}
<option value="{{ language.code }}" {% if language.active %}selected{% endif %}>{% if theme.language_chooser_display == 'code' %}{{ language.code|upper }}{% elif theme.language_chooser_display == 'name' %}{{ language.name }}{% endif %}</option>
<input type="hidden" name="next" value="{{ next }}">
<select name="language" onchange="this.form.submit();">
{% for code, language in LANGUAGES %}
<option value="{{ code }}" {% if code == LANGUAGE_CODE %}selected{% endif %}>
{% if theme.language_chooser_display == 'code' %}{{ code|upper }}{% elif theme.language_chooser_display == 'name' %}{{ language }}{% endif %}
</option>
{% endfor %}
</select>
</form>
Expand Down
30 changes: 9 additions & 21 deletions admin_interface/templatetags/admin_interface_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,33 @@
register = template.Library()


@register.simple_tag(takes_context=True)
def get_admin_interface_languages(context):
@register.inclusion_tag("admin_interface/language_chooser.html", takes_context=True)
def admin_interface_language_chooser(context):
if not settings.USE_I18N:
# i18n disabled
return None
if len(settings.LANGUAGES) < 2:
# less than 2 languages
return None
try:
set_language_url = reverse("set_language")
context["set_language_url"] = reverse("set_language")
except NoReverseMatch:
# ImproperlyConfigured - must include i18n urls:
# urlpatterns += [url(r'^i18n/', include('django.conf.urls.i18n')),]
return None
request = context.get("request", None)
if not request:
return None
context["LANGUAGES"] = settings.LANGUAGES

full_path = request.get_full_path()
admin_nolang_url = re.sub(r"^\/([\w]{2})([\-\_]{1}[\w]{2,4})?\/", "/", full_path)
if admin_nolang_url == full_path:
# ImproperlyConfigured - must include admin urls using i18n_patterns:
# from django.conf.urls.i18n import i18n_patterns
# urlpatterns += i18n_patterns(url(r'^admin/', admin.site.urls))
return None
langs_data = []

default_lang_code = settings.LANGUAGE_CODE
current_lang_code = translation.get_language() or default_lang_code
for language in settings.LANGUAGES:
lang_code = language[0].lower()
lang_name = language[1].title()
lang_data = {
"code": lang_code,
"name": lang_name,
"default": lang_code == default_lang_code,
"active": lang_code == current_lang_code,
"activation_url": f"{set_language_url}?next=/{lang_code}{admin_nolang_url}",
}
langs_data.append(lang_data)
return langs_data
context["LANGUAGE_CODE"] = current_lang_code
context["next"] = admin_nolang_url
return context


@register.simple_tag()
Expand Down
116 changes: 29 additions & 87 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,118 +22,60 @@ def tearDown(self):
def __render_template(self, string, context=None):
return Template(string).render(Context(context or {}))

def test_get_admin_interface_languages(self):
def test_admin_interface_language_chooser(self):
context = Context({"request": self.request_factory.get("/en/admin/")})
languages = templatetags.get_admin_interface_languages(context)
context = templatetags.admin_interface_language_chooser(context)
languages = context["LANGUAGES"]
expected_languages = [
{
"code": "de",
"name": "Deutsch",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/de/admin/",
},
{
"code": "en",
"name": "English",
"default": True,
"active": True,
"activation_url": "/i18n/setlang/?next=/en/admin/",
},
{
"code": "es",
"name": "Español",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/es/admin/",
},
{
"code": "fa",
"name": "Farsi",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/fa/admin/",
},
{
"code": "fr",
"name": "Français",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/fr/admin/",
},
{
"code": "it",
"name": "Italiano",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/it/admin/",
},
{
"code": "pl",
"name": "Polski",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/pl/admin/",
},
{
"code": "pt-BR",
"name": "Português",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/pt-br/admin/",
},
{
"code": "ru",
"name": "Русский",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/ru/admin/",
},
{
"code": "tr",
"name": "Türk",
"default": False,
"active": False,
"activation_url": "/i18n/setlang/?next=/tr/admin/",
},
("de", "Deutsch"),
("en", "English"),
("es", "Español"),
("fa", "Farsi"),
("fr", "Français"),
("it", "Italiano"),
("pl", "Polski"),
("pt-BR", "Português"),
("ru", "Русский"),
("tr", "Türk"),
]
self.assertEqual(len(languages), len(expected_languages))
self.assertEqual(languages[0], expected_languages[0])
self.assertEqual(languages[1], expected_languages[1])
self.assertEqual(context["next"], "/admin/")

@override_settings(
USE_I18N=False,
)
def test_get_admin_interface_languages_with_i18n_disabled(self):
def test_admin_interface_language_chooser_with_i18n_disabled(self):
context = Context({"request": self.request_factory.get("/en/admin/")})
languages = templatetags.get_admin_interface_languages(context)
self.assertEqual(languages, None)
tag_context = templatetags.admin_interface_language_chooser(context)
self.assertEqual(tag_context, None)

@override_settings(
ROOT_URLCONF="tests.urls_without_i18n_patterns",
)
def test_get_admin_interface_languages_without_i18n_url_patterns(self):
def test_admin_interface_language_chooser_without_i18n_url_patterns(self):
context = Context({"request": self.request_factory.get("/en/admin/")})
languages = templatetags.get_admin_interface_languages(context)
self.assertEqual(languages, None)
tag_context = templatetags.admin_interface_language_chooser(context)
self.assertEqual(tag_context, None)

@override_settings(
LANGUAGES=(("en", "English"),),
)
def test_get_admin_interface_languages_without_multiple_languages(self):
def test_admin_interface_language_chooser_without_multiple_languages(self):
context = Context({"request": self.request_factory.get("/en/admin/")})
languages = templatetags.get_admin_interface_languages(context)
self.assertEqual(languages, None)
tag_context = templatetags.admin_interface_language_chooser(context)
self.assertEqual(tag_context, None)

def test_get_admin_interface_languages_without_request(self):
def test_admin_interface_language_chooser_without_request(self):
context = Context({})
languages = templatetags.get_admin_interface_languages(context)
self.assertEqual(languages, None)
tag_context = templatetags.admin_interface_language_chooser(context)
self.assertEqual(tag_context, None)

def test_get_admin_interface_languages_without_language_prefix_in_url(self):
def test_admin_interface_language_chooser_without_language_prefix_in_url(self):
context = Context({"request": self.request_factory.get("/admin/")})
languages = templatetags.get_admin_interface_languages(context)
self.assertEqual(languages, None)
tag_context = templatetags.admin_interface_language_chooser(context)
self.assertEqual(tag_context["next"], "/admin/")

def test_get_theme(self):
Theme.objects.all().delete()
Expand Down

0 comments on commit 06758e9

Please sign in to comment.