tomis
tomis

Reputation: 1971

Django - multiple pluralization in admin model

I have googled this for very long time but with no results. I`m beginner to Django so I don't know all features it have. But this problem is very important for client :-( Could you help me, please?

So, I have this model defined:

from django.utils.translation import ugettext_lazy as _

class Product(Model):
    #translation for model and set db table name
    class Meta:
        verbose_name = _('product')
        verbose_name_plural = _('products')
        ...

Now, because of czech language, I need these written in admin list:

Everywhere else, I'm using ungettext sucessfully. However, I don't know, how to get count in Meta. I have found this as abstract (but seems to be useless):

class Model(DjangoModel):

    class Meta:
        abstract = True

    def get_description(self):
        return ungettext(self.verbose_name, self.verbose_name_plural, self.count) % \
                   {'count':self.count, 'name':self.name}

Source is from django internationalization: counter is not available when marking strings for pluralization

Maybe, at the end would be fine to show language definition (tried to add/remove %s from msgid):

msgid "%s product"
msgid_plural "%s products"
msgstr[0] "%s 1 výrobek"
msgstr[1] "%s 2 výrobky"
msgstr[2] "%s 5 výrobků"

If you need more info for question, sure I will provide it.

Thank you a lot in advance.

UPDATE
Please, be sure, that I'm using following in the .po file:

"Plural-Forms: nplurals=3; plural=((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2);\n"

Once more, anywhere else but admin models, IT IS working. It's quetion not how to run multi pluralization in general, but how to change anything in admin (e.g. new abstract model etc.) to run it there...

Upvotes: 12

Views: 1230

Answers (2)

tomis
tomis

Reputation: 1971

After getting deep into Django sources, this is not possible to do it in admin usecases without overriding many of functions.

Upvotes: 3

lasizoillo
lasizoillo

Reputation: 104

You need to put in your .po file:

"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"

Then, in your template you use the plural form passing a valid counter. Gettext have all information needed:

  • It knows how many plurals there are
  • It knows how to calc the plural for a number
  • Django passes the msg_id for plural and a counter

Upvotes: 5

Related Questions