0

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

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)

我有点不知道如何将其国际化。有人有好主意吗?

更新:

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

4

1 回答 1

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}
于 2012-05-09T15:23:09.357 回答