Opened 3 weeks ago

Last modified 5 hours ago

#36142 assigned Cleanup/optimization

django.shortcuts.get_object_or_404 does not support translation for error messages (i18n)

Reported by: Abror Izzatullaev Owned by: Joel Burns
Component: Internationalization Version: 5.2
Severity: Normal Keywords: i18 Internationalization
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no
Pull Requests:How to create a pull request

Description (last modified by Claude Paroz)

class WheatProvidingViewSet(viewsets.ModelViewSet):
    queryset = WheatProviding.objects.all()
    serializer_class = WheatProvidingSerializer

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()

The code above demonstrates a Django REST Framework (DRF) ModelViewSet. When a non-existent ID is provided, the destroy method raises a 404 Not Found error. However, the error message is not translated.

The root cause of this issue is that ModelViewSet relies on Django’s get_object_or_404 function, which does not support internationalization (i18n) for its error messages.

Could you consider adding support for translations in the get_object_or_404 function? This enhancement would allow DRF to natively support localized error messages, improving its usability in multilingual applications.

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (3)

comment:1 by Claude Paroz, 3 weeks ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted
Version: 5.15.2

I think that localizing that message makes sense as it is potentially user-facing, as the use case in the ticket description shows.

comment:2 by Joel Burns, 9 days ago

Owner: set to Joel Burns
Status: newassigned

I want to research and come up with a solution.

Last edited 7 days ago by Joel Burns (previous) (diff)

comment:3 by Joel Burns, 5 hours ago

Hi, I'm looking for feedback,

So I think the best way to accomplish this would be to first use:

from django.utils.translation import gettext as _

(the text to be translated needs to be marked with _)

from django.http import HttpResponse
from django.utils.translation import gettext as _
def my_view(request):
output = _("Welcome to my site.")
return HttpResponse(output)

the tagged messages are added to a message file with a .po extension

indicate django.middleware.locale.LocaleMiddleware this must be added to activate translation features

https://docs.djangoproject.com/en/5.1/topics/i18n/translation/

I'm assuming this file is where this needs to be applied, https://github.com/django/django/blob/stable/5.1.x/django/shortcuts.py#L65

specifically to the get_object_or_404 or get_list_or_404 methods to translate the output?

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