让Django管理员使用翻译的字段名称

时间:2022-09-12 08:52:32

I'm doing a localization of a Django app.

我正在做一个Django应用程序的本地化。

The front-end website works fine and the Django admin site picks up the selected language as well.

前端网站运行良好,Django管理站点也可以选择所选语言。

But it only applies the language settings in some places and uses the default English versions of field and column names, even though these have been translated. Why? How can I make it use the translated names for column and field names in the admin interface?

但它仅在某些地方应用语言设置,并使用字段和列名称的默认英语版本,即使这些已被翻译。为什么?如何让它在管理界面中使用列和字段名称的翻译名称?

Example:

例:

class Order(models.Model):
    OPTIONS = ( (0, _("Bank transfer") ), (1, _("Cash on delivery") ), )

    user = models.ForeignKey(User, name=_("User") )
    payment = models.IntegerField(choices=self.OPTIONS, name=_("Payment"))

For which I get:

我得到了:

  1. Translated standard admin texts such as "Welcome" and "Logout" at the top
  2. 翻译标准管理文本,例如顶部的“欢迎”和“注销”
  3. Translated SELECT options for the payment type
  4. 已转换的付款类型的SELECT选项
  5. NOT translated column names and form labels for the fields ("User", "Payment")
  6. 未翻译字段的列名和表单标签(“用户”,“付款”)

I'm using Django 1.0.2. The texts that are not getting translated did appear in the locale files along with those that work.

我正在使用Django 1.0.2。未被翻译的文本确实出现在区域设置文件中以及有效的文本中。

Sub-question: is it possible to localize the app names?

子问题:是否可以本地化应用程序名称?

3 个解决方案

#1


15  

It turned out I was setting a translated version for name instead of verbose_name.

事实证明我正在为name而不是verbose_name设置翻译版本。

This works:

这有效:

class Order(models.Model):
    OPTIONS = ( (0, _("Bank transfer") ), (1, _("Cash on delivery") ), )

    user = models.ForeignKey(User, verbose_name=_("User") )
    payment = models.IntegerField(choices=self.OPTIONS, verbose_name=_("Payment"))

#2


8  

TomA got the right answer.

TomA得到了正确的答案。

But Django now takes the first argument as the verbose name of a field, except for the ForeignKey, ManyToManyField and OneToOneField field types. So if you are lazy you can also write:

但Django现在将第一个参数作为字段的详细名称,但ForeignKey,ManyToManyField和OneToOneField字段类型除外。所以,如果你很懒,你也可以写:

payment = models.IntegerField(_("Payment"), choices=self.OPTIONS)

You still have to use a keyword argument for the ForeignKey example, though:

但是,您仍然必须为ForeignKey示例使用关键字参数:

user = models.ForeignKey(User, verbose_name=_("User"))

#3


0  

Are you perhaps using a custom ModelForm for this model (in admin.py)? You'll need to add a gettext-ed value for the label of the fields you override.

您是否正在为此模型使用自定义ModelForm(在admin.py中)?您需要为覆盖的字段的标签添加gettext-ed值。

Localizing app names is not possible, as of Django 1.0 - not sure of 1.1.

从Django 1.0开始,无法本地化应用程序名称 - 不确定1.1。

#1


15  

It turned out I was setting a translated version for name instead of verbose_name.

事实证明我正在为name而不是verbose_name设置翻译版本。

This works:

这有效:

class Order(models.Model):
    OPTIONS = ( (0, _("Bank transfer") ), (1, _("Cash on delivery") ), )

    user = models.ForeignKey(User, verbose_name=_("User") )
    payment = models.IntegerField(choices=self.OPTIONS, verbose_name=_("Payment"))

#2


8  

TomA got the right answer.

TomA得到了正确的答案。

But Django now takes the first argument as the verbose name of a field, except for the ForeignKey, ManyToManyField and OneToOneField field types. So if you are lazy you can also write:

但Django现在将第一个参数作为字段的详细名称,但ForeignKey,ManyToManyField和OneToOneField字段类型除外。所以,如果你很懒,你也可以写:

payment = models.IntegerField(_("Payment"), choices=self.OPTIONS)

You still have to use a keyword argument for the ForeignKey example, though:

但是,您仍然必须为ForeignKey示例使用关键字参数:

user = models.ForeignKey(User, verbose_name=_("User"))

#3


0  

Are you perhaps using a custom ModelForm for this model (in admin.py)? You'll need to add a gettext-ed value for the label of the fields you override.

您是否正在为此模型使用自定义ModelForm(在admin.py中)?您需要为覆盖的字段的标签添加gettext-ed值。

Localizing app names is not possible, as of Django 1.0 - not sure of 1.1.

从Django 1.0开始,无法本地化应用程序名称 - 不确定1.1。