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

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

问题描述

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





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



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



在Django源代码中,我发现:



<$ $ code $ $ ack选择%s来更改
contrib / admin / views / main.py
70:self.title =(self.is_popup和ugettext('选择%s')%force_unicode(self.opts.verbose_name)或ugettext('选择%s更改')%force_unicode(self.opts.verbose_name))

所以 verbose_name 元属性似乎在这里起了一些作用。试图使用它:

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

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



我正在使用Django 1.1.1。有可能有人指出相关文件吗?因为谷歌不能。 ;-)同时我会深入讨论django源代码...

解决方案

Django文档:




  • 运行 django-admin compilemessages ,例如作为您构建
    进程的一部分。谢谢stevejalim!

  • 将django的 ugettext_lazy()应用于模型名称( Meta 类别和 verbose_name

  • 属性(模型字段 verbose_name )名称也可以使用 ugettext_lazy()

  • 在您的模型元数据中使用懒惰翻译,否则翻译
    在加载模型时发生类别和用户的设置,
    ,特别是浏览器设置,将不被考虑。

  • 我使用一些属性名称的范围,例如使用管道分隔模型名称
    和属性名称。在
    ruby​​-gettext中使用相同的约定。背景:根据上下文,大多数语言中的属性名称,如title或name翻译为
    不同。示例
    'Book | title' - >'Titel'或'Buchtitel'用德语。但是
    'Chapter | title'将被翻译为'Ü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')

或是有更好的方式来翻译模型和管理页面?



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


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:

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

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.

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))

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')

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 для изменения".

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...

解决方案

Important things not mentioned in the Django documentation:

  • 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'.

Example using above principles:

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?

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

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

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