#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 , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 3 years ago
Has patch: | set |
---|
follow-up: 4 comment:3 by , 3 years ago
Has patch: | unset |
---|---|
Resolution: | → invalid |
Status: | assigned → closed |
LANGUAGE_CODE
should be lowercased, so pt-br
instead of pt-BR
, see Definitions, LANGUAGE_CODE docs, and the list of LANGUAGES.
follow-up: 5 comment:4 by , 3 years ago
Replying to Mariusz Felisiak:
LANGUAGE_CODE
should be lowercased, sopt-br
instead ofpt-BR
, see Definitions, LANGUAGE_CODE docs, and the list of LANGUAGES.
The translation of select2 only accepts pt-BR that way.
https://github.com/django/django/blob/main/django/contrib/admin/static/admin/js/vendor/select2/i18n/pt-BR.js
https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L453
follow-up: 6 comment:5 by , 3 years ago
Cc: | added; removed |
---|---|
Easy pickings: | unset |
Resolution: | invalid |
Status: | closed → new |
Summary: | Error using select2 translation files → Select2 doesn't load translations with subtags. |
Triage Stage: | Unreviewed → Accepted |
Replying to Cleiton de Lima:
Replying to Mariusz Felisiak:
LANGUAGE_CODE
should be lowercased, sopt-br
instead ofpt-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:6 by , 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, sopt-br
instead ofpt-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 onLANG
. Lowercase when searching for a file will help only forpt-BR
but not forzh-hans
,pt-br
etc. We could probably addlang
to theattrs
, 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 , 3 years ago
Cc: | added |
---|
follow-up: 9 comment:8 by , 3 years ago
Cleiton, thanks for checking. Would you like to prepare a patch?
comment:9 by , 3 years ago
Yes, I will.
Replying to Mariusz Felisiak:
Cleiton, thanks for checking. Would you like to prepare a patch?
comment:10 by , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:12 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I fixed it
https://github.com/django/django/pull/14813