Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33070 closed Bug (fixed)

Select2 doesn't load translations with subtags.

Reported by: Cleiton de Lima Owned by: Cleiton de Lima
Component: contrib.admin Version: 3.2
Severity: Normal Keywords: select2
Cc: Johannes Maron, Claude Paroz, Cleiton de Lima Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For example, when using the setting LANGUAGE_CODE="pt-BR", the translation of select2 is not applied, the static file i18n is not found.
This is due to the fact that some languages are converted to lowercase. https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L366

Change History (14)

comment:1 by sarath ak, 3 years ago

Owner: changed from nobody to sarath ak
Status: newassigned

comment:2 by sarath ak, 3 years ago

Has patch: set

comment:3 by Mariusz Felisiak, 3 years ago

Has patch: unset
Resolution: invalid
Status: assignedclosed

LANGUAGE_CODE should be lowercased, so pt-br instead of pt-BR, see Definitions, LANGUAGE_CODE docs, and the list of LANGUAGES.

in reply to:  3 ; comment:4 by Cleiton de Lima, 3 years ago

Replying to Mariusz Felisiak:

LANGUAGE_CODE should be lowercased, so pt-br instead of pt-BR, see Definitions, LANGUAGE_CODE docs, and the list of LANGUAGES.

The translation of select2 only accepts pt-BR that way.
The file is found but the translation doesn't work. It only works when the tag html has
<html lang="pt-BR" dir="ltr" data-select2-id="14">...

Last edited 3 years ago by Cleiton de Lima (previous) (diff)

in reply to:  4 ; comment:5 by Mariusz Felisiak, 3 years ago

Cc: Johannes Maron Claude Paroz added; info@… removed
Easy pickings: unset
Resolution: invalid
Status: closednew
Summary: Error using select2 translation filesSelect2 doesn't load translations with subtags.
Triage Stage: UnreviewedAccepted

Replying to Cleiton de Lima:

Replying to Mariusz Felisiak:

LANGUAGE_CODE should be lowercased, so pt-br instead of pt-BR, see Definitions, LANGUAGE_CODE docs, and the list of LANGUAGES.

The translation of select2 only accepts pt-BR that way.
The file is found but the translation doesn't work. It only works when the tag html has
<html lang="pt-BR" dir="ltr" data-select2-id="14">...

Thanks, I didn't notice that Select2 loads translations based on LANG. Lowercase when searching for a file will help only for pt-BR but not for zh-hans, pt-br etc. We could probably add lang to the attrs, e.g.:

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,

What do you think? It works for me.

in reply to:  5 comment:6 by Cleiton de Lima, 3 years ago

It looks good to me too!

Replying to Mariusz Felisiak:

Replying to Cleiton de Lima:

Replying to Mariusz Felisiak:

LANGUAGE_CODE should be lowercased, so pt-br instead of pt-BR, see Definitions, LANGUAGE_CODE docs, and the list of LANGUAGES.

The translation of select2 only accepts pt-BR that way.
The file is found but the translation doesn't work. It only works when the tag html has
<html lang="pt-BR" dir="ltr" data-select2-id="14">...

Thanks, I didn't notice that Select2 loads translations based on LANG. Lowercase when searching for a file will help only for pt-BR but not for zh-hans, pt-br etc. We could probably add lang to the attrs, e.g.:

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,

What do you think? It works for me.

comment:7 by Cleiton de Lima, 3 years ago

Cc: Cleiton de Lima added

comment:8 by Mariusz Felisiak, 3 years ago

Cleiton, thanks for checking. Would you like to prepare a patch?

in reply to:  8 comment:9 by Cleiton de Lima, 3 years ago

Yes, I will.

Replying to Mariusz Felisiak:

Cleiton, thanks for checking. Would you like to prepare a patch?

comment:10 by Cleiton de Lima, 3 years ago

Owner: changed from sarath ak to Cleiton de Lima
Status: newassigned

comment:11 by Jacob Walls, 3 years ago

Has patch: set

comment:12 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 8eb56930:

Fixed #33070 -- Fixed loading translations with language subtags in admin's Select2 widget.

comment:14 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In a077f10:

[4.0.x] Fixed #33070 -- Fixed loading translations with language subtags in admin's Select2 widget.

Backport of 8eb56930915f8022aa27ec29ab5be7d21764608c from main

Note: See TracTickets for help on using tickets.
Back to Top