2012-05-09 31 views
0

添加多至Django的刺中的代碼應該使用ungettext(),其中第三個參數是計數器,以決定是否應當使用計數器不可用單數還是複數:Django的國際化:標識字符串時多元化

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) 

我有點卡住着如何國際化這一點。有人有一個好主意嗎?

更新:

我已經改變了例子,我的情況更匹配

回答

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} 
+0

嗯,不過'makemessage'無法以這種方式找到這些字符串... –

+0

@XunYang'makemessage'依賴於'xgettext',它直接掃描py文件以找到*'ungettext'*文字。您可以通過文件運行'xgettext'來查看是否收集到了消息。 – okm

+0

我修改了一個更接近匹配的例子,其中我使用了一個返回值來表示不同類型的'foo'。不知道直接掃描是否可以找到所有這些字符串:( –