From 12175f00e9a7c481b0b35d68d3531a224d59545c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bahattin=20=C3=87ini=C3=A7?= Date: Fri, 16 Aug 2024 13:16:16 +0300 Subject: [PATCH] fix: Add gettextSafe function to handle missing gettext in Django admin --- .../admin/js/django_admin_list_filter.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/dalf/static/admin/js/django_admin_list_filter.js b/src/dalf/static/admin/js/django_admin_list_filter.js index 90c6e29..cf6b19d 100644 --- a/src/dalf/static/admin/js/django_admin_list_filter.js +++ b/src/dalf/static/admin/js/django_admin_list_filter.js @@ -73,11 +73,27 @@ } return [fieldQueryParam, queryParams]; } - + + function getTextSafe(text) { + /** + * Safely retrieves the translated text using gettext if available. + * Django doesn't always load the admin:jsi18n URL, for instance, when + * has_delete_permission is set to false. In these cases, the gettext + * function may be unavailable. + * Reference: https://github.com/django/django/blob/main/django/contrib/admin/templates/admin/change_list.html#L10-L12 + * + */ + if (typeof gettext !== 'undefined') { + return gettext(text); + } else { + return text; + } + } + $(document).ready(function() { $('.django-admin-list-filter').select2({ allowClear: true, - placeholder: gettext("Filter") + placeholder: getTextSafe("Filter") }).on("select2:select", function(e){ var navURL = new URL(window.location.href); let [fieldQueryParam, queryParams] = getQueryParams(e, $(this).data("isChoicesFilter"));