Opened 10 years ago

Last modified 10 years ago

#21650 closed Cleanup/optimization

Usage example in i18n docs is bad advice for plurals — at Version 5

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 Tim Graham)

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.

Change History (5)

comment:1 by Claude Paroz, 10 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

+1, the count == 1 (and more generally the verbose_name_plural concept, see also #11688) is totally English-centered.

comment:2 by Ned Batchelder, 10 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 Claude Paroz, 10 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 Ned Batchelder, 10 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 Tim Graham, 10 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.

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