applegrew / django-select2

This is a Django integration for Select2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Translations HeavySelect2MultipleWidget

nkay08 opened this issue · comments

Hello,
I am using the HeavySelect2MultipleWidget in my app for a MultipleChoiceField.
It works quite good, but I have some issues with translation. I am offering my site in english and german and the user can switch between those on the fly.
When viewing the site in german, the widget still shows the "Please enter 2 or more characters" in english. I looked at html source and it seems to correctly load http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/i18n/de.js for this language. But it does not actually translate the text.

For other javascript content I use django.views.i18n.JavaScriptCatalog .

Is there any other configuration I need to make for it to work?

I found the error. My html file had the attribute lang="en" set in the html tag.
<html lang="en" xmlns="http://www.w3.org/1999/html">
This seems to override the select2 language choice, and is obviously not a problem of django-select2, but rather select2.
Select2 inherits the language from the lang attribute of the parent.

Hello,
How did you manage to choose the correct http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/i18n/(language).js in the HTML file according to the selected language in your Django application?
Thank you!

It has been a long time, so I might not get this right.

If I remember correctly, you can get the language code within a template with

{% load i18n %}

{% get_current_language as LANGUAGE_CODE %}

Then you can use the LANGUAGE_CODE variable. I probably used this value like this lang="{{ LANGUAGE_CODE }}" in the html tag.
You might be able to leave this property empty to also get the correct language in Select2, I don't know.

Thank you for your fast reply!
Although this a good solution, I thought there might be a simpler one if there are a lot of templates using django-select2 (each inheriting from different templates). I think it might have something to do with I18N_PATH or settings.DJANGO_SELECT2_I18N configurations. However, I cannot figure it out!
https://django-select2.readthedocs.io/en/latest/django_select2.html#django_select2.conf.Select2Conf.I18N_PATH
Thank you anyways!