Django - multiple pluralization in admin model
Asked Answered
N

2

12

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:

  • 0 výrobků
  • 1 výrobek
  • 2-4 výrobky
  • 5- výrobků

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...

Nila answered 16/3, 2013 at 0:18 Comment(0)
N
2

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

Nila answered 24/3, 2013 at 14:32 Comment(0)
S
5

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
Smedley answered 19/3, 2013 at 2:15 Comment(1)
Hey, I wrote that everywhere else I'm using it sucessfully, only in admin models it's not working. I did update the post to be sure that everybody will see I have this text in .po file.Nila
N
2

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

Nila answered 24/3, 2013 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.