Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix invalid ngettext usage #9560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

last-partizan
Copy link

Description

Format should be called after ngettext, not before.

See example: https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#pluralization

Formatting changes is because i don't know how to properly format it with your previous style, and there's no included autoformat/or formatting linter. I just formatted it automatically with ruff-lsp. Feel free to revert fomatting changes to your style or explain me what to do.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please share the output of before and after this change?

@last-partizan
Copy link
Author

That's very good question, because it got me thinking... And turns out, I missed important detail.

In our production code, we override this class with

            extra = ngettext(
                "Try again later in about {wait} second",
                "Try again later in about {wait} seconds",
                wait,
            ).format(wait=wait)

To fix, invalid usage of "second[s]" in languages with three plural forms (like Ukrainian or Russian). I don't see translations for this string in current DRF, and English has only two plural forms, so there would be no changes at all.

Proper way to fix it, would be the same, and i'm pushing updated changes.

Expected changes in Ukrainian, after this change lands, after updating .po with makemessages and after translations added:

-"Спробуйте ще раз через 5 секунди"
+"Спробуйте ще раз через 5 секунд"

Because we have three plural forms:

  • (singular, 1) - "секунда", or in this case "секунду"
  • (plural 2-4) - "секунди"
  • (plural 5-9) - "секунд"

@last-partizan
Copy link
Author

@auvipy please take a look at this again.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is their anyway you can ensure that this won't create any regression, please?

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
@last-partizan
Copy link
Author

image

This code is covered by tests, and translation strings is unchanged, so everything should be fine.

We probably need to make sure changelog is clear about what's changed here. I have squashed commits and updated text, but if changelog is autogenerated from the first line of commit, you may need to move relevant instructions into the first line when Merging/Squashing.

Important part there: "If you have overridden extra_detail_singular or extra_detail_plural, you should now replace them with a single def extra_detail override.".

@auvipy
Copy link
Member

auvipy commented Nov 19, 2024

we will need documentation update then? and it seems to be an API change as well?

@auvipy auvipy requested a review from a team November 19, 2024 11:55
Copy link
Contributor

@peterthomassen peterthomassen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an API change, which the project prefers not to do at this stage.

It may be possible to juggle with some properties etc. to strictly extend the previous API without removing Throttled.extra_detail_* attributes, but I'm not sure that complexity would be a good idea.

rest_framework/exceptions.py Outdated Show resolved Hide resolved
@last-partizan
Copy link
Author

This is an API change, which the project prefers not to do at this stage.

By "at this stage", you mean "project is mature enough to not do API changes"? or some sort of "no API changes for minor releases"?

It may be possible to juggle with some properties etc. to strictly extend the previous API without removing Throttled.extra_detail_* attributes, but I'm not sure that complexity would be a good idea.

Not sure how. Even if we can use something like ngettext(self.extra_detail_plural ..., it will not work, because for ngettext to work properly it needs strings right there.

And, I don't see any docs mentioning extra_detail_ on class Throttled, so maybe this is not a public API...

@peterthomassen
Copy link
Contributor

By "at this stage", you mean "project is mature enough to not do API changes"? or some sort of "no API changes for minor releases"?

The former, see https://github.com/encode/django-rest-framework/blob/master/CONTRIBUTING.md and the note on https://www.django-rest-framework.org/community/contributing/.

And, I don't see any docs mentioning extra_detail_ on class Throttled, so maybe this is not a public API...

Many people consider all API public unless identifiers start with an underscore.

@last-partizan
Copy link
Author

That's a tricky situation, because it's a bug that cannot be fixed without changing public API.

Sure, it's minor bug, but so are the consequences of breaking this part of API.

We can ... maybe, add some checks in __setattr__ that raise errors when users trying to set extra_detail_singular or extra_detail_plural. But that's overkill for my taste.

Hmmmm, maybe add a check to rest_framework/checks.py, that ensures no subclasses of Throttled are setting those props?

But, from my point of view, best course of action here: just add a note about change in the changelog/release notes, so the change would be visible.

@peterthomassen
Copy link
Contributor

@tomchristie Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants