@@ -76,9 +76,16 @@ class Select2Mixin:
7676
7777 empty_label = ""
7878
79+ def __init__ (self , * args , ** kwargs ):
80+ super ().__init__ (* args , ** kwargs )
81+ self .i18n_name = SELECT2_TRANSLATIONS .get (get_language ())
82+
7983 def build_attrs (self , base_attrs , extra_attrs = None ):
8084 """Add select2 data attributes."""
81- default_attrs = {"data-minimum-input-length" : 0 }
85+ default_attrs = {
86+ "lang" : self .i18n_name ,
87+ "data-minimum-input-length" : 0 ,
88+ }
8289 if self .is_required :
8390 default_attrs ["data-allow-clear" ] = "false"
8491 else :
@@ -100,32 +107,28 @@ def optgroups(self, name, value, attrs=None):
100107 self .choices = list (chain ([("" , "" )], self .choices ))
101108 return super ().optgroups (name , value , attrs = attrs )
102109
103- def _get_media (self ):
110+ @property
111+ def media (self ):
104112 """
105113 Construct Media as a dynamic property.
106114
107115 .. Note:: For more information visit
108116 https://docs.djangoproject.com/en/stable/topics/forms/media/#media-as-a-dynamic-property
109117 """
110- lang = get_language ()
111118 select2_js = (settings .SELECT2_JS ,) if settings .SELECT2_JS else ()
112119 select2_css = (settings .SELECT2_CSS ,) if settings .SELECT2_CSS else ()
113120
114- i18n_name = SELECT2_TRANSLATIONS .get (lang )
115- if i18n_name not in settings .SELECT2_I18N_AVAILABLE_LANGUAGES :
116- i18n_name = None
117-
118- i18n_file = (
119- ("%s/%s.js" % (settings .SELECT2_I18N_PATH , i18n_name ),) if i18n_name else ()
120- )
121+ i18n_file = ()
122+ if self .i18n_name in settings .SELECT2_I18N_AVAILABLE_LANGUAGES :
123+ i18n_file = (
124+ ("%s/%s.js" % (settings .SELECT2_I18N_PATH , self .i18n_name ),)
125+ )
121126
122127 return forms .Media (
123128 js = select2_js + i18n_file + ("django_select2/django_select2.js" ,),
124129 css = {"screen" : select2_css + ("django_select2/django_select2.css" ,)},
125130 )
126131
127- media = property (_get_media )
128-
129132
130133class Select2TagMixin :
131134 """Mixin to add select2 tag functionality."""
@@ -212,11 +215,7 @@ def __init__(self, attrs=None, choices=(), **kwargs):
212215 Value is a name of a field in a model (used in `queryset`).
213216
214217 """
215- self .choices = choices
216- if attrs is not None :
217- self .attrs = attrs .copy ()
218- else :
219- self .attrs = {}
218+ super ().__init__ (attrs , choices )
220219
221220 self .uuid = str (uuid .uuid4 ())
222221 self .field_id = signing .dumps (self .uuid )
0 commit comments