django国际化:标记字符串以进行复数时,计数器不可用

时间:2012-05-09 13:59:44

标签: django internationalization

在代码中向django stings添加复数应使用ungettext(),其中第3个参数是计数器,以决定它是应该使用单数还是复数:

text = ungettext(
        'There is %(count)d %(name)s available.',
        'There are %(count)d %(name)s available.',
        count
) % {
    'count': count,
    'name': name
}

调用ungettext()时,参数作为计数器应该可用。但在我的情况下,字符串和计数器是分开的,因此无法在正确的位置提供计数器:

class foo(bar):
    description="There is %(count)d %(names)s available."

class foo_2(bar):
    description="%(count)d rabbits jump over the %(names)s."

# More 'foo' type classes....

class foo_model(Model):
    count=IntegerField()
    name=CharField()
    klass=CharField()
    #model definition...

    def _get_self_class(self):
        if self.klass=='foo':
            return foo
        elif self.klass=='foo_2':
            return foo_2

    def get_description(self):
            return self._get_self_class.description%(self.count,self.name)

我对如何将其国际化感到困惑。有人有个好主意吗?

更新

我已将示例更改为与我的情况更接近的匹配

1 个答案:

答案 0 :(得分:0)

您需要在某处构建ungettext,例如

class Foo(Bar):
    description = "There is %(count)d %(names)s available."
    description_plural = "There are %(count)d %(names)s available."

class FooModel(Model):
    count = IntegerField()
    name = CharField()
    # model definition...

    def get_description(self):
        return ungettext(Foo.description, Foo.description_plural, self.count) % \
                   {'count':self.count, 'name':self.name}