如何将计算值的条目正确添加到 django 国际化消息文件? [英] How to properly add entries for computed values to the django internationalization messages file?

查看:13
本文介绍了如何将计算值的条目正确添加到 django 国际化消息文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 文档说明:

Django documentation states:

使用变量或计算值的注意事项,如前所述两个例子,是 Django 的翻译字符串检测实用程序,django-admin.py makemessages,将无法找到这些字符串.

The caveat with using variables or computed values, as in the previous two examples, is that Django's translation-string-detecting utility, django-admin.py makemessages, won't be able to find these strings.

这对我来说很好,我准备手动为已翻译变量的所有可能值提供翻译.但是该怎么做呢?

That is fine with me, I'm ready to provide translations for all possible values of the translated variable by hand. But how to do that?

假设我的模板代码中有这样的:

Let's say I have in my template code like this:

{% trans var %}

var 是从数据库中提取的,我知道它所有可能的值 - 假设可能的值是Alice"和Bob".

The var is extracted from the database, and I know all of the possible values of it - let's say the possible values are "Alice" and "Bob".

我认为我需要做的就是提供以下条目:

I thought all I need to do is provide entries like these:

msgid "Alice"
msgstr "Alicja"

在 django.po 文件中.不幸的是,每当我在那之后运行 djangoadmin makemessages 时,这些条目都会被注释掉:

in django.po file. Unfortunately, whenever i run djangoadmin makemessages after that, these entries are being commented out:

#~ msgid "Alice"
#~ msgstr "Alicja"

我做错了什么?我是否误解了转换计算值的想法?

What am I doing wrong? Have I misunderstood the idea of translating computed values?

推荐答案

我们目前也在解决这个问题.虽然我们做得不好,但我们确实有一个相当烦人的丑陋黑客来绕过它.

We're currently in the process of figuring this out as well. While we haven't done so properly, we do have a rather annoyingly ugly hack to get around it.

我们只需在代码中的某处定义一个虚拟"函数(例如您的 models.py 甚至 settings.py),然后用我们需要翻译的所有字符串填充它.

We simply define a "dummy" function somewhere in the code (for example your models.py or even settings.py) and fill it up with all the strings that we need to have a translation for.

from django.utils.translation import ugettext_lazy as _, pgettext

def dummy_for_makemessages():
    """
    This function allows manage makemessages to find the forecast types for translation.
    Removing this code causes makemessages to comment out those PO entries, so don't do that
    unless you find a better way to do this
    """
    pgettext('forecast type', 'some string')
    pgettext('forecast type', 'some other string')
    pgettext('forecast type', 'yet another string')
    pgettext('forecast type', 'etc')
    pgettext('forecast type', 'etc again')
    pgettext('forecast type', 'and again and again')

这个函数永远不会被调用,只是简单地定义它可以防止消息字符串被 makemessages 注释掉.

This function is never called but simply defining it prevents the message strings from getting commented out by makemessages.

不是最优雅的解决方案,但它确实有效.

Not the most elegant solution but it works.

这篇关于如何将计算值的条目正确添加到 django 国际化消息文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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