From 8eb56930915f8022aa27ec29ab5be7d21764608c Mon Sep 17 00:00:00 2001 From: Cleiton Lima Date: Mon, 20 Sep 2021 15:56:54 -0300 Subject: [PATCH] Fixed #33070 -- Fixed loading translations with language subtags in admin's Select2 widget. --- django/contrib/admin/widgets.py | 5 +++-- tests/admin_widgets/test_autocomplete_widget.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index aeb74773ac..f1002cac6c 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -388,6 +388,7 @@ class AutocompleteMixin: self.db = using self.choices = choices self.attrs = {} if attrs is None else attrs.copy() + self.i18n_name = SELECT2_TRANSLATIONS.get(get_language()) def get_url(self): return reverse(self.url_name % self.admin_site.name) @@ -413,6 +414,7 @@ class AutocompleteMixin: 'data-theme': 'admin-autocomplete', 'data-allow-clear': json.dumps(not self.is_required), 'data-placeholder': '', # Allows clearing of the input. + 'lang': self.i18n_name, 'class': attrs['class'] + (' ' if attrs['class'] else '') + 'admin-autocomplete', }) return attrs @@ -449,8 +451,7 @@ class AutocompleteMixin: @property def media(self): extra = '' if settings.DEBUG else '.min' - i18n_name = SELECT2_TRANSLATIONS.get(get_language()) - i18n_file = ('admin/js/vendor/select2/i18n/%s.js' % i18n_name,) if i18n_name else () + i18n_file = ('admin/js/vendor/select2/i18n/%s.js' % self.i18n_name,) if self.i18n_name else () return forms.Media( js=( 'admin/js/vendor/jquery/jquery%s.js' % extra, diff --git a/tests/admin_widgets/test_autocomplete_widget.py b/tests/admin_widgets/test_autocomplete_widget.py index 279acfe615..a1c6e0ad7b 100644 --- a/tests/admin_widgets/test_autocomplete_widget.py +++ b/tests/admin_widgets/test_autocomplete_widget.py @@ -72,7 +72,8 @@ class AutocompleteMixinTests(TestCase): 'data-app-label': 'admin_widgets', 'data-field-name': 'band', 'data-model-name': 'album', - 'data-placeholder': '' + 'data-placeholder': '', + 'lang': 'en', }) def test_build_attrs_no_custom_class(self):