Open sandino opened 8 years ago
A quick and dirty way to do it is to create your own widget based on ModelSelect2
:
class Select2RuWidgetMixin(Select2WidgetMixin):
class Media:
"""Automatically include static files for the admin."""
css = {
'all': (
'autocomplete_light/vendor/select2/dist/css/select2.css',
'autocomplete_light/select2.css',
)
}
js = (
'autocomplete_light/jquery.init.js',
'autocomplete_light/autocomplete.init.js',
'autocomplete_light/vendor/select2/dist/js/select2.full.js',
'autocomplete_light/select2.js',
# Provide an additional i18 js.
'autocomplete_light/vendor/select2/dist/js/i18n/ru.js',
)
def build_attrs(self, *args, **kwargs):
attrs = super(Select2RuWidgetMixin, self).build_attrs(*args, **kwargs)
attrs.setdefault('data-locale', 'ru')
return attrs
class Select2Ru(Select2RuWidgetMixin, Select):
pass
class Select2RuMultiple(Select2RuWidgetMixin, SelectMultiple):
pass
class ModelSelect2Ru(QuerySetSelectMixin,
Select2RuWidgetMixin,
forms.Select):
pass
class ModelSelect2RuMultiple(QuerySetSelectMixin,
Select2RuWidgetMixin,
forms.SelectMultiple):
pass
Making it fully i18zable is a bit more complicated. You should include all i18n JS files to Media.js
. Then in build_attrs
you can get current locale from Django and set the data-locale
attribute to it.
@gagarski Thanks for your help!
But I wonder then why would DAL have that i18n files in its code if they are not supposed to be used out of the box without going dirty ways.
But I wonder then why would DAL have that i18n files in its code if they are not supposed to be used out of the box without going dirty ways.
I18n come from Select2 repo which is used in DAL as a submodule.
@jpic, is it possible to add all i18n js files to media and data-language
attribute based on current locale to select2 widgets? Adding only current locale i18n file might be a better way, but it seems to be impossible in Django (class Media
is static, and there is no way to have per-instance media attribute for widget).
Adding only current locale i18n file might be a better way, but it seems to be impossible in Django (class Media is static, and there is no way to have per-instance media attribute for widget)
You can definitely have dynamic media on forms and widgets.
https://docs.djangoproject.com/en/1.10/topics/forms/media/#media-as-a-dynamic-property
Not necessarily saying we need it here, or even that it's the best idea. But it's possible.
Cool!
BTW, Select2 themes can also be supported following the same pattern (dynamically adding theme CSS + setting data-theme
HTML-attribute of select
)
Nice, looking forward to see a patch for this B)
@luzfcb , I had to patch your patch to get it working for Russian ('ru-ru' in Django setttings). See my comment there.
@Pavel-Y if you want to rebase @luzfcb's commits, fix tests it breaks and add your fix we'll merge it.
Hello, I use django-autocomplete-light==3.1.8 and it works fine. I also would like to make use of i18n features for Select2 widget but didn't find init script to set the language.
Could you please tell me how to set the language up?