Could someone please be kind to explain to me how the pluralize logic
works in django?
a.) pluralize template filter
b.) Meta.verbose_name_plural
As default, I am expecting the pluralize to convert "country" to
"countries". For exapmle, this doesn't happen in admin so I define the
verbose_name_plural in the Meta inner class.
Its straightforward to implement a function that can seemlessly handle
this correctly as default, even if it was to read the pluralization
rules from a rules.<language_code> files or something equivalent.
Michael Newman
On May 4, 2008, at 1:16 PM, "alen....@gmail.com"
Then you haven't looked too hard. Pluralization comes up regularly,
both in the context of the pluralize filter, and the
verbose_name/verbose_name_plural option. And if you're calling
pluralization a simple task, you _really_ haven't done your research.
The short version is that pluralization is _hard_. Automatic
pluralization, which seems to be what you want, is an effectively
impossible task, especially in English. English doesn't have regular
rules for pluralization for anything but trivial cases. About the only
way to do it effectively is to have a dictionary of all possible
plurals - and then you hit the religious wars over whether the plural
of octopus is octopus, octopuses, or octopodes.
Even if you could get English sorted out, then you get the i18n
problem. A few eastern European languages have some very interesting
pluralization rules which further complicates the dream of an
automatic solution.
So, we have settled with the a naive solution, manually assisted, with
template helpers that cover 95% of cases. "Add an s" works for a good
majority of cases - if that isn't correct, you can manually correct it
in verbose_name_plural. The template filter can handle all the english
cases (worst case being different extensions for 1 and many). This
doesn't cover the needs of the previously mentioned eastern European
languages, but accommodating those languages while keeping the simple
case simple is almost impossible, so users of those languages can
write a custom template tag if they need one.
Yours,
Russ Magee %-)
Thank you for your replies.
Just to confirm, I am not referring to a perfect automatic pluralize
solution. I am very well aware that a silver bullet solution would be
very difficult, if even possible.
I will try make myself more clear now. What I am specifically
referring to is the 'verbose_name_plural' of the model.
This is what I have in mind:
1. Django initializes (contributes) the 'verbose_name_plural' on model
class with a call to the pluralize logic to attempt find the relevant
plural for the given singular.
2. If need be, developer overrides the 'verbose_name_plural' in the
model to define specialized plural.
You might want to check the date on that thread before you pull it in
for moral support. November 2005 was in the 'pre-magic-removal' era of
Django - the discussion that is going on there was the start of some
fundamental changes to the way you compose queries - changes that are
now fully integrated into Django.
Yours,
Russ Magee %-)
If you felt my comment was below the belt, I apologize. It wasn't
intended as such.
I did look at the thread, and I stand by my point. The discussion you
reference was from a time when pluralization was important for
different reasons. Pre-magic removal (the time from which that thread
comes), the plural name was used as part of the API - i.e., you didn't
ask for Entry.objects.all(), you said entries.get_list(). As a result,
the pluralization process was an important part of the API, so getting
pluralization right was important. The magic removal process turned
pluralization into almost entirely a presentation issue. The
requirements of pluralization in the API are quite different to those
at the presentation layer.
There is a side discussion in the thread about adding pluralize as a
class method. I suspect that this was also on the table for
pre-magic-removal reasons - back in the day, there were limitations on
your ability to associate methods with models. Since the magic has
been removed, this is no longer an issue - if you want to put a
pluralize() method on your models to assist with rendering, you are
free to do so. Regardless, the context for that discussion is quite
different to the context of this current discussion.
Yours,
Russ Magee %-)