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?
我用谷歌搜索了很长时间,但没有结果。我是Django的初学者,所以我不知道它的所有特性。但是这个问题对客户来说是很重要的。
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ů
- 0 vyrobků
- 1 výrobek
- 1 vyrobek
- 2-4 výrobky
- 2 - 4 vyrobky
- 5- výrobků
- 5 - vyrobků
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):
在其他地方,我成功地使用ungettext。但是,我不知道,如何在Meta中计算。我认为这是抽象的(但似乎毫无用处):
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
源代码来自django国际化:在为多元化标记字符串时,计数器不可用
Maybe, at the end would be fine to show language definition (tried to add/remove %s from msgid
):
也许,在结束时显示语言定义(试图从msgid中添加/删除%s)是可以的:
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:
请更新,请确保,我正在使用以下在。po文件:
"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...
再一次,除了管理模型之外的任何地方,它都在工作。问题不在于如何运行多重多元化,而在于如何在管理(例如新的抽象模型等)中改变任何东西来运行它……
2 个解决方案
#1
5
You need to put in your .po file:
你需要输入。po文件:
"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:
然后,在模板中使用经过有效计数器的复数形式。Gettext有所有需要的信息:
- 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
- Django传递msg_id作为复数和计数器
#2
1
After getting deep into Django sources, this is not possible to do it in admin usecases without overriding many of functions.
在深入研究Django源代码之后,在管理usecases中不重写许多函数是不可能的。
#1
5
You need to put in your .po file:
你需要输入。po文件:
"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:
然后,在模板中使用经过有效计数器的复数形式。Gettext有所有需要的信息:
- 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
- Django传递msg_id作为复数和计数器
#2
1
After getting deep into Django sources, this is not possible to do it in admin usecases without overriding many of functions.
在深入研究Django源代码之后,在管理usecases中不重写许多函数是不可能的。