Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify language chooser #328

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions admin_interface/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from admin_interface import settings

settings.check_installed_apps()
settings.check_locale_middleware()

Check warning on line 14 in admin_interface/apps.py

View check run for this annotation

Codecov / codecov/patch

admin_interface/apps.py#L14

Added line #L14 was not covered by tests
12 changes: 12 additions & 0 deletions admin_interface/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@

def check_installed_apps():
check_installed_app("colorfield")


def check_locale_middleware():
if not settings.USE_I18N or len(settings.LANGUAGES) < 2:

Check warning on line 33 in admin_interface/settings.py

View check run for this annotation

Codecov / codecov/patch

admin_interface/settings.py#L33

Added line #L33 was not covered by tests
# no i18n or less than 2 languages
return

Check warning on line 35 in admin_interface/settings.py

View check run for this annotation

Codecov / codecov/patch

admin_interface/settings.py#L35

Added line #L35 was not covered by tests

if "django.middleware.locale.LocaleMiddleware" not in settings.MIDDLEWARE:
raise ImproperlyConfigured(

Check warning on line 38 in admin_interface/settings.py

View check run for this annotation

Codecov / codecov/patch

admin_interface/settings.py#L37-L38

Added lines #L37 - L38 were not covered by tests
"'django.middleware.locale.LocaleMiddleware' is required, "
"add it to settings.MIDDLEWARE."
)
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
Loading