Django翻译选择字段 [英] Django translating choice fields

查看:74
本文介绍了Django翻译选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的模型:

  class Payment(models.Model):类Status(IntEnum):开= 0余额= 2封闭= 1status = models.IntegerField(choices = enum_to_choices(Status),default = 0,verbose_name = _("Status")) 

我正在使用一个枚举来表示我的选择,并在我的应用程序的其他部分中使用它们.我将这些代码转换为元组,以在选择字段中使用以下代码:

来自django.utils.translation的

 作为_导入ugettext_lazydef enum_to_choices(枚举):x =元组([(x.value,_(x.name.replace("_",")))对于枚举中的x))返回x 

代码的转换部分有效,我可以将这些字段用作选择,但是翻译不起作用,它不会显示在我的翻译文件中.如果我使用诸如"open" 之类的静态字符串将参数更改为uggettext_lazy,它将显示.

这是怎么回事?

解决方案

在我看来,这与 makemessages 命令有关,该命令由于某种原因而与非静态字符串不兼容.

我无法详细说明原因,但这是解决问题的方法:

您实际上必须手动在翻译文件中创建字符串(django.po):

 #:.\ polls \ models.py:enum_to_choicesmsgstr打开"msgstr输出"msgstr关闭"msgstrfermé"msgstr平衡"msgstr"solde" 

别忘了 django-admin编译消息,并且应该出现翻译后的字符串!
这不是理想的,特别是对于长枚举,但总比没有好.

如果这里的某人知道 makemessages (及其使用的 xgettext 程序)的内部工作原理并有解释,请让我们知道^^


其他解决方案:使用建议的选择结构

另一种解决方案,如果您绝对不需要将选择包含在枚举中,则使用选择结构

然后在 django-admin makemessages 上,字符串将被添加到翻译文件中.

您可以像枚举一样轻松地获得选择的价值:

来自.models的

 导入付款new_payment = Payment.objects.create(status = Payment.OPEN) 

I've got some models like this:

class Payment(models.Model):
    class Status(IntEnum):
        open = 0
        balance = 2
        closed = 1
    status = models.IntegerField(choices=enum_to_choices(Status), default=0, verbose_name=_("Status"))

I'm using a enum to denote my choices and to use them throughout other parts of my application. I'm converting these to tuples to use in choice fields with the following code:

from django.utils.translation import ugettext_lazy as _
def enum_to_choices(enum):
    x = tuple([(x.value, _(x.name.replace("_", " "))) for x in enum])
    return x

The conversion part of the code works, i can use these fields as choices but the translation doesn't work, it doesn't show up in my translation files. If i change the parameter to uggettext_lazy with a static string like "open" it does show up.

What's going on here?

解决方案

It seems to me that this is related to the makemessages command, which for some reason struggles with non-static strings.

I couldn't elaborate on the why, but here's how you can solve the problem:

You actually have to manually create the strings in the translation files (django.po):

#: .\polls\models.py:enum_to_choices

msgid "open"
msgstr "ouvert"

msgid "closed"
msgstr "fermé"

msgid "balance"
msgstr "solde"

Don't forget to django-admin compilemessages, and translated strings should appear!
This is not ideal, especially for long enums, but better than nothing.

If someone here knows the inner workings of makemessages (and the xgettext program it uses) and has an explanation, please let us know ^^


Other solution: use recommended choices structure

Another solution, if you don't absolutely need the choices to be in enums, is using the choices structure as shown in the documentation:

from django.utils.translation import ugettext_lazy as _

class Payment(models.Model):
    OPEN = 0
    CLOSED = 1
    BALANCE = 2
    STATUS_CHOICES = (
        (OPEN, _('open')),
        (CLOSED, _('closed')),
        (BALANCE, _('balance')),
    )
    status = models.IntegerField(choices=STATUS_CHOICES, default=OPEN, verbose_name=_("Status"))

And then on django-admin makemessages the strings will be added to the translation files.

You can easily get the value of a choice, as you would with an enum :

from .models import Payment
new_payment = Payment.objects.create(status=Payment.OPEN)

这篇关于Django翻译选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆