管理页面的 Django 国际化 - 翻译模型名称和属性 [英] Django internationalization for admin pages - translate model name and attributes

查看:14
本文介绍了管理页面的 Django 国际化 - 翻译模型名称和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 的国际化非常好(基于 gettext,LocaleMiddleware),但是翻译模型名称和管理页面属性的正确方法是什么?我在文档中没有找到任何关于此的内容:

Django's internationalization is very nice (gettext based, LocaleMiddleware), but what is the proper way to translate the model name and the attributes for admin pages? I did not find anything about this in the documentation:

我想要Выберите заказ для изменения"而不是Выберите order для изменения".请注意,订单"未翻译.

I would like to have "Выберите заказ для изменения" instead of "Выберите order для изменения". Note, the 'order' is not translated.

首先,我定义了一个模型,在settings.py中激活USE_I18N = True,运行django-admin makemessages -l ru.默认情况下不为模型名称和属性创建条目.

First, I defined a model, activated USE_I18N = True in settings.py, run django-admin makemessages -l ru. No entries are created by default for model names and attributes.

在我找到的 Django 源代码中grepping:

Grepping in the Django source code I found:

$ ack "Select %s to change"
contrib/admin/views/main.py
70:        self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s to change') % force_unicode(self.opts.verbose_name))

所以 verbose_name 元属性似乎在这里发挥了一些作用.尝试使用它:

So the verbose_name meta property seems to play some role here. Tried to use it:

class Order(models.Model):
    subject = models.CharField(max_length=150)
    description = models.TextField()
    class Meta:
        verbose_name = _('order')

现在更新的 po 文件包含可以翻译的 msgid 'order'.所以我把翻译放进去.不幸的是,运行管理页面显示Выберите order для изменения"的相同组合.

Now the updated po file contains msgid 'order' that can be translated. So I put the translation in. Unfortunately running the admin pages show the same mix of "Выберите order для изменения".

我目前使用的是 Django 1.1.1.有人可以指出我的相关文件吗?因为google不能.;-) 同时,我将深入研究 django 源代码...

I'm currently using Django 1.1.1. Could somebody point me to the relevant documentation? Because google can not. ;-) In the mean time I'll dig deeper into the django source code...

推荐答案

Django 文档中未提及的重要事项:

Important things not mentioned in the Django documentation:

  • 运行 django-admin compilemessages,例如作为构建的一部分过程.谢谢 stevejalim!
  • 将 django 的 ugettext_lazy() 应用于模型名称(Meta 类和 verbose_name)
  • 属性(模型字段verbose_name)名称也可以用ugettext_lazy()
  • 翻译
  • 在模型元数据中使用惰性翻译,否则翻译在加载模型类和用户设置时发生,尤其是浏览器设置,将不予考虑
  • 我对属性名称使用了一些范围,例如分离型号名称和带有管道的属性名称.相同的约定用于红宝石获取文本.背景:已翻译的标题"或名称"等属性名称根据上下文,在大多数语言中有所不同.例子'Book|title' -> 'Titel' 或 'Buchtitel' 德语.但'Chapter|title' 将被翻译为 'Überschrift'.
  • run django-admin compilemessages, e.g. as a part of your build process. Thanks stevejalim!
  • apply django's ugettext_lazy() to model names ( Meta class and verbose_name )
  • attribute (model field verbose_name) names can also be translated with ugettext_lazy()
  • use lazy translation in your model metadata, otherwise the translation happens while loading the model classes and the settings of your users, especially the browser settings, will not be taken into account
  • I use some scoping for attribute names, e.g. separating the model name and attribute names with a pipe. The same convention is used in ruby-gettext. Background: attribute names like 'title' or 'name' translated differently in the most languages depending on context. Example 'Book|title' -> 'Titel' or 'Buchtitel' in German. But 'Chapter|title' would be translated as 'Überschrift'.

使用上述原则的示例:

from django.utils.translation import ugettext_lazy as _
class Order(models.Model):
    subject = models.CharField(max_length=150, verbose_name = _('Order|subject'))
    description = models.TextField(            verbose_name = _('Order|description'))
    class Meta:
        verbose_name = _('order')
        verbose_name_plural = _('orders')

或者有没有更好的方法来翻译模型和管理页面?

Or is there a better way to translate the model and admin pages?

无论哪种方式,我们都应该增强 Django 文档并填补空白!

Either way we should enhance the Django documentation and fill the gap!

这篇关于管理页面的 Django 国际化 - 翻译模型名称和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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