#21650 closed Cleanup/optimization (fixed)
Usage example in i18n docs is bad advice for plurals
Reported by: | Ned Batchelder | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
This example in the i18n docs:
Lets see a slightly more complex usage example:: from django.utils.translation import ungettext from myapp.models import Report count = Report.objects.count() if count == 1: name = Report._meta.verbose_name else: name = Report._meta.verbose_name_plural text = ungettext( 'There is %(count)d %(name)s available.', 'There are %(count)d %(name)s available.', count ) % { 'count': count, 'name': name }
Here we choose between two forms of name (singular and plural) based on whether the count is 1 or not. That is the rule in English, but not in other languages. The whole point of ungettext is to defer the logic that performs the mapping from number to text, since it depends on the language.
Unfortunately, I think the only solution is to use more stilted language:
text = ungettext( 'There is %(count)d %(name)s object available.', 'There are %(count)d %(name)s objects available.', count ) % { 'count': count, 'name': Report._meta.verbose_name, }
and ignore the verbose_name_plural altogether.
Attachments (2)
Change History (14)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 11 years ago
Just to clarify: this ticket is merely about the example in the docs. While the issue in #11688 is true (models' support for singular and plural is English-centric and simplistic), all I'm pointing out here is that the i18n docs could at least not fall into the same trap and suggest a comparison to 1 as the correct way to support plurals.
comment:3 by , 11 years ago
I suggest to simply remove that example in the docs, because if we remove the verbose_name/plural stuff in it, it doesn't bring anything new to the discussion compared to the previous example. Opinion?
comment:4 by , 11 years ago
My one counter to that might be: Change this example to an explanation of why it isn't a good idea:
Note that pluralization is complicated, and works differently in other languages. Comparing count to 1 isn't always the correct rule. This code looks sophisticated, but will produce the wrong results for other languages: .... Don't try to implement your own singular-or-plural logic, it won't be correct.
comment:5 by , 11 years ago
Description: | modified (diff) |
---|
I don't use i18n, but Ned's suggest seems reasonable to me. Claude, what do you think? My only question would be what modifications to make to the note that follows. Currently it says, "When using this technique", and my understanding is that we'd be changing things to say not to use this technique. I think the note still has value though as it's linked to from elsewhere.
comment:6 by , 11 years ago
Sure, but you know I'm not the most skilled writer for English documentation. Feel free to go ahead.
by , 11 years ago
Attachment: | 21650.diff added |
---|
comment:7 by , 11 years ago
Has patch: | set |
---|
comment:8 by , 11 years ago
Thanks, that looks good. However, I'd like to see somewhere the %(name)s object
/%(name)s objects
trick that Ned suggested. Even if it might produce suboptimal strings in English, it's often the only proper way to handle i18n in a right manner.
by , 11 years ago
Attachment: | 21650.2.diff added |
---|
comment:11 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
+1, the
count == 1
(and more generally theverbose_name_plural
concept, see also #11688) is totally English-centered.