{{{
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/21650>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* needs_tests: => 0
* stage: Unreviewed => Accepted
Comment:
+1, the `count == 1` (and more generally the `verbose_name_plural`
concept, see also #11688) is totally English-centered.
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:1>
Comment (by nedbatchelder):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:2>
Comment (by claudep):
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?
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:3>
Comment (by nedbatchelder):
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.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:4>
Old description:
New description:
[https://docs.djangoproject.com/en/dev/topics/i18n/translation/#pluralization
--
Comment (by timo):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:5>
Comment (by claudep):
Sure, but you know I'm not the most skilled writer for English
documentation. Feel free to go ahead.
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:6>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:7>
Comment (by claudep):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:8>
Comment (by claudep):
Fine for me. Ned?
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:9>
Comment (by nedbatchelder):
Looks great, thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:10>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"85270ef3f5bf96b556e697bc5a46bf925ec4be21"]:
{{{
#!CommitTicketReference repository=""
revision="85270ef3f5bf96b556e697bc5a46bf925ec4be21"
Fixed #21650 -- Corrected bad advice for plural translation.
Thanks nedbatchelder and claudep.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:11>
Comment (by Tim Graham <timograham@…>):
In [changeset:"3b79fbabcdfefa6c5734a9645be4ffee02e4a270"]:
{{{
#!CommitTicketReference repository=""
revision="3b79fbabcdfefa6c5734a9645be4ffee02e4a270"
[1.6.x] Fixed #21650 -- Corrected bad advice for plural translation.
Thanks nedbatchelder and claudep.
Backport of 85270ef3f5 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/21650#comment:12>