Skip to content

Commit

Permalink
fix: Fix invalid ngettext usage in Throttled exception.
Browse files Browse the repository at this point in the history
Format should be called after ngettext.
If you have overridden `extra_detail_singular` or `extra_detail_plural`,
you should now replace them with a single `def extra_detail` override.

Refs: https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#pluralization
  • Loading branch information
last-partizan committed Nov 18, 2024
1 parent d3dd45b commit 76cc34a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions rest_framework/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,24 @@ def __init__(self, media_type, detail=None, code=None):
class Throttled(APIException):
status_code = status.HTTP_429_TOO_MANY_REQUESTS
default_detail = _('Request was throttled.')
extra_detail_singular = _('Expected available in {wait} second.')
extra_detail_plural = _('Expected available in {wait} seconds.')
default_code = 'throttled'

def __init__(self, wait=None, detail=None, code=None):
if detail is None:
detail = force_str(self.default_detail)
if wait is not None:
wait = math.ceil(wait)
detail = ' '.join((
detail,
force_str(ngettext(self.extra_detail_singular.format(wait=wait),
self.extra_detail_plural.format(wait=wait),
wait))))
detail = " ".join((detail, force_str(self.extra_detail(wait))))
self.wait = wait
super().__init__(detail, code)

def extra_detail(self, wait):
return ngettext(
'Expected available in {wait} second.',
'Expected available in {wait} seconds.',
wait,
).format(wait=wait)


def server_error(request, *args, **kwargs):
"""
Expand Down

0 comments on commit 76cc34a

Please sign in to comment.