Skip to content

Commit 7200dc7

Browse files
fix: Add gettextSafe function to handle missing gettext in Django admin (#8)
1 parent d258658 commit 7200dc7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/dalf/static/admin/js/django_admin_list_filter.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,27 @@
7373
}
7474
return [fieldQueryParam, queryParams];
7575
}
76-
76+
77+
function getTextSafe(text) {
78+
/**
79+
* Safely retrieves the translated text using gettext if available.
80+
* Django doesn't always load the admin:jsi18n URL, for instance, when
81+
* has_delete_permission is set to false. In these cases, the gettext
82+
* function may be unavailable.
83+
* Reference: https://github.com/django/django/blob/main/django/contrib/admin/templates/admin/change_list.html#L10-L12
84+
*
85+
*/
86+
if (typeof gettext !== 'undefined') {
87+
return gettext(text);
88+
} else {
89+
return text;
90+
}
91+
}
92+
7793
$(document).ready(function() {
7894
$('.django-admin-list-filter').select2({
7995
allowClear: true,
80-
placeholder: gettext("Filter")
96+
placeholder: getTextSafe("Filter")
8197
}).on("select2:select", function(e){
8298
var navURL = new URL(window.location.href);
8399
let [fieldQueryParam, queryParams] = getQueryParams(e, $(this).data("isChoicesFilter"));

0 commit comments

Comments
 (0)