【问题标题】:Internationalize the attributes of an object国际化对象的属性
【发布时间】:2011-06-05 06:31:13
【问题描述】:

我的对象有一个称为状态的属性。状态可以有两种状态:openedclose。这个属性的值应该被翻译。我尝试通过两种方式做到这一点:

<td> {% trans object.status %} </td>

<td>
    {% blocktrans with object.status as status %} {{ status }} {% endblocktrans %}
</td> 

但没有结果。在 django.po 文件中,我有一个条目 %(status)s。 Django 如何知道如何翻译状态?

【问题讨论】:

    标签: python django templates internationalization django-templates


    【解决方案1】:

    如果状态只能有两种状态,则应在模型定义中使用choices 属性。然后可以标记选择的值以进行翻译:

    STATUS_CHOICES = (
        ('open', _('open')),
        ('closed', _('closed'))
    )
    
    class MyModel(models.Model):
        status = models.CharField(max_length=10, choices=STATUS_CHOICES)
    

    在模板中,使用get_status_display 方法:

    <td> {{ object.get_status_display }} </td>
    

    【讨论】:

      【解决方案2】:

      检查是否将这 4 行放入 po:

      msgid "opend"
      msgstr "Your translated word"
      
      msgid "closed"
      msgstr "your trans word"
      

      然后运行:

      manage.py compiletranslation
      

      因为当您使用现有的 .mo 文件编辑 .po 文件时,更改无效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        • 1970-01-01
        • 2017-09-24
        • 1970-01-01
        • 2014-01-04
        相关资源
        最近更新 更多