管理页面的 Django 国际化 - 翻译模型名称和属性

发布于 2024-09-03 16:00:15 字数 1434 浏览 8 评论 0 原文

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

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

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

我在 Django 源代码中进行 Grepping 发现:

$ 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 元属性似乎在这里发挥了一些作用。尝试使用它:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独陪着我 2024-09-10 16:00:15

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

  • 运行 django-admincompilemessages,例如作为构建的一部分
    过程。谢谢史蒂夫贾林!
  • 将 django 的 ugettext_lazy() 应用于模型名称( Meta 类和 verbose_name
  • 属性(模型字段 verbose_name)名称可以也可以使用 ugettext_lazy() 进行翻译
  • 在模型元数据中使用惰性翻译,否则翻译
    加载模型类和用户设置时发生,
    特别是浏览器设置,不会被考虑在内
  • 我对属性名称使用一些范围,例如分隔模型名称
    和带有管道的属性名称。相同的约定用于
    ruby-gettext。背景:翻译后的“标题”或“名称”等属性名称
    根据上下文,大多数语言都有所不同。例子
    '书|标题' ->德语中的“Titel”或“Buchtitel”。但
    “章节|标题”将被翻译为“Ü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 文档并填补空白!

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!

你不是我要的菜∠ 2024-09-10 16:00:15

请参阅 https://automationpanda.com/2018/04/21/django-管理翻译/
它的作者在展示如何逐步掌握所有 django 翻译功能方面做出了出色的工作。对我来说这比官方文档好得多。

See https://automationpanda.com/2018/04/21/django-admin-translations/
It's author made an excellent work in showing how to master all django translation features step by step. It's much better than oficial documentation to me.

驱逐舰岛风号 2024-09-10 16:00:15

我也无法在 Django Admin 中翻译模型标签,因为 django.po 中有 #, fuzzy 如下所示:

# "django.po"

...

#: .\my_app1\models.py:12
#, fuzzy # <- Here
msgid "person"
msgstr "personne"

#: .\my_app1\models.py:13
#, fuzzy # <- Here
msgid "persons"
msgstr "personnes"

...

所以,我删除了 #, fuzzy 来自 django.po ,如下所示,然后我可以在 Django Admin 中翻译模型标签。 *您可以查看我的问题我的答案详细解释:

# "django.po"

...

#: .\my_app1\models.py:12
msgid "person"
msgstr "personne"

#: .\my_app1\models.py:13
msgid "persons"
msgstr "personnes"

...

另外,模糊进行了解释在文档中,如下所示:

模糊

该标志可以由 msgmerge 程序生成,也可以由翻译者自己插入。它表明 msgstr 字符串可能不再是正确的翻译。只有译者才能判断译文是否需要进一步修改,或者照原样可以接受。一旦对翻译感到满意,她就会删除这个模糊属性。 msgmerge 程序仅在模糊搜索后组合 msgid 和 msgstr 条目时插入此内容。请参阅模糊条目。

I also couldn't translate a model label in Django Admin because there are #, fuzzy in django.po as shown below:

# "django.po"

...

#: .\my_app1\models.py:12
#, fuzzy # <- Here
msgid "person"
msgstr "personne"

#: .\my_app1\models.py:13
#, fuzzy # <- Here
msgid "persons"
msgstr "personnes"

...

So, I removed #, fuzzy from django.po as shown below, then I could translate the model label in Django Admin. *You can see my question and my answer explaining it in detail:

# "django.po"

...

#: .\my_app1\models.py:12
msgid "person"
msgstr "personne"

#: .\my_app1\models.py:13
msgid "persons"
msgstr "personnes"

...

In addition, fuzzy is explained in the doc as shown below:

fuzzy

This flag can be generated by the msgmerge program or it can be inserted by the translator herself. It shows that the msgstr string might not be a correct translation (anymore). Only the translator can judge if the translation requires further modification, or is acceptable as is. Once satisfied with the translation, she then removes this fuzzy attribute. The msgmerge program inserts this when it combined the msgid and msgstr entries after fuzzy search only. See Fuzzy Entries.

黎夕旧梦 2024-09-10 16:00:15

我将 ugettext 与 _ 一起使用,并将 ugettext_lazy 与双 __ 一起使用。因此,makemessages 管理命令仅收集第一个消息。

要同时收集 ___,我们可以要求 gettext 工具收集除 _( ).为此,我们重写 makemessages 命令:https://docs.djangoproject.com/en/dev/topics/i18n/translation/#customizing-the-makemessages-command

# myapp/management/commands/makemessages.py
from django.core.management.commands import makemessages

class Command(makemessages.Command):
    self.stdout.write("----> Using our custom makemessages command to collect both _ and double __")
    xgettext_options = makemessages.Command.xgettext_options + ['--keyword=__']  # <-- added __

我在管理中的模型现在终于完全翻译了。

I use both ugettext with _ and ugettext_lazy with a double __. Consequently, the makemessages management command was only collecting the first ones.

To collect both _ and __ we can ask the gettext tool to collect more than the default strings enclosed in _( ). To do this, we override the makemessages command: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#customizing-the-makemessages-command

# myapp/management/commands/makemessages.py
from django.core.management.commands import makemessages

class Command(makemessages.Command):
    self.stdout.write("----> Using our custom makemessages command to collect both _ and double __")
    xgettext_options = makemessages.Command.xgettext_options + ['--keyword=__']  # <-- added __

My models in Admin are now finally fully translated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文