覆盖默认的 Django 翻译
我有一个这样的模板:
{% trans "Log out" %}
Django 自动将其翻译为西班牙语 Terminar sesión。不过我想将其翻译为Cerrar sesión。
我尝试将此文字添加到 .po 文件中,但是在编译消息时收到一条错误消息,指出此文字重复。
有没有办法更改/覆盖默认的 Django 翻译?
谢谢。
I have a template with this:
{% trans "Log out" %}
This is translated automatically by Django to Spanish as Terminar sesión. However I would like to translate it as Cerrar sesión.
I have tried to add this literal to the .po file, however I get an error saying this literal is duplicated when I compile the messages.
Is there a way to change/override default Django translations?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用:
在您的应用程序文件夹中创建一个文件,该文件将保存需要覆盖翻译的 django 消息,例如
django_standard_messages.py
在 django lib 文件夹或中
django.po
文件找到需要覆盖的消息(字符串),例如django.forms/fields.py
有消息_(u"This field is必需的。”)
我们想要以不同的方式翻译成德语django_standard_messages 中将其以不同方式翻译为德语
.py
添加所有此类消息,如下所示:makemessages
、compilemessages
) - makemessages 将在您的应用程序 .po 文件中添加添加的 django 标准消息,找到它们并翻译,运行compilemessages 来更新 .mo 文件背后的逻辑:(我认为;)) - 当
ugettext
函数搜索一条消息(字符串)的翻译时,有几个需要搜索的.po
/.mo
文件。 使用第一个匹配项。因此,如果我们的本地应用程序.po
/.mo
按该顺序位于第一个,我们的翻译将覆盖所有其他应用程序(例如 django 默认值)。当您需要翻译所有或大部分 django 默认消息时,另
一种可能性(我没有尝试过)是复制默认消息django
.po
文件放在我们的语言环境或其他一些特殊文件夹中,并修复翻译并在LOCALE_PATHS
djangosettings
文件中注册文件夹(如果是新的)作为第一个条目 列表。背后的逻辑:与上一节中提到的非常相似。
This is what worked for me:
create a file in your app folder which will hold django messages for which translations need to be overridden, e.g.
django_standard_messages.py
in django lib folder or in
django.po
files find the message (string) that needs to be overridden, e.g.django.forms/fields.py
has message_(u"This field is required.")
which we want to translate to german differentlyin
django_standard_messages.py
add all such messages like this:makemessages
,compilemessages
) - makemessages will add added django standard messages in your application .po file, find them and translate, run compilemessages to update .mo fileThe logic behind: (I think ;) ) - when
ugettext
function searches translation for one message (string), there are several.po
/.mo
files that needs to be searched through. The first match is used. So, if our local app.po
/.mo
is first in that order, our translations will override all other (e.g. django default).Alternative
When you need to translate all or most of django default messages, the other possibility (which I didn't tried) is to copy default django
.po
file in our locale or some other special folder, and fix translations and register the folder (if new) inLOCALE_PATHS
djangosettings
file as first entry in the list.The logic behind: is the very similar as noted in previous section.
根据 Robert Lujo 的回答,他的替代方案完全有效。而且 IMO 更简单(仅将覆盖的语言环境保留在特殊的 .po 文件中)。步骤如下:
向 LOCALE_PATHS Django 设置添加额外路径。
LOCALE_PATHS = (
# 默认的,makemessages 命令将在其中生成文件
os.path.join(BASE_DIR, 'myproject', 'locale'),
# 我们新的扩展区域设置目录
os.path.join(BASE_DIR, 'myproject', 'locale_extra'),
)
找到要翻译的原始 Django(或第 3 方)字符串
msgstr "最近的操作"
msgstr "最后的操作"
Based on Robert Lujo answer, his alternative is totally working. And IMO simpler (keep the overriden locales in a special .po file only). Here are the steps:
Add an extra path to the LOCALE_PATHS Django settings.
find the original Django (or 3rd party) string to be translated
最简单的方法是收集 django.contrib.admin locale 文件夹中找到的 .po 文件并重新编译它(您可以使用 POEdit 来执行此操作)。
您还可以通过将 django.contrib.admin 模板放在项目模板文件夹中(例如:yourproject/templates/admin/change_form.html)然后从项目根运行 makemessages 来覆盖 django.contrib.admin 模板(尽管 django 1.4 不再支持此操作) alpha 如果我是正确的)
编辑:罗伯特卢霍的答案是干净的方法
The easiest way is to collect the .po file found in the django.contrib.admin locale folder and re-compiling it (you can use POEdit for doing so).
You could also override the django.contrib.admin templates by putting them in your projects templates folder (for example: yourproject/templates/admin/change_form.html) then running makemessages from the project root (although this is no longer supported for django 1.4 alpha if i'm correct)
edit: Robert Lujo's answer is the clean method
这是我们部署的另一个解决方案。它涉及猴子修补
DjangoTranslation
类的_add_installed_apps_translations
方法,以优先考虑项目应用程序的翻译而不是 Django 应用程序的翻译。然后在主应用程序的
.ready()
方法中,调用patchDjangoTranslation
:主要更改是这些行:
原始内容是:
而不是解释应用程序的翻译按照它们在 INSTALLED_APPS 设置中出现的顺序,此块输出将项目应用程序置于 Django 应用程序之前的应用程序列表。由于这只在确定要使用的翻译时发生,因此它不会影响代码的任何其他部分,并且不需要进行其他更改。
它适用于 Django 版本 1.11 至 2.2。
This is another solution we deployed. It involved monkey patching the
_add_installed_apps_translations
method of theDjangoTranslation
class to prioritize the translations of the project apps over the translations of the Django apps.Then in the
.ready()
method of your main app, callpatchDjangoTranslation
:The main change are these lines:
The original are:
Instead of interpreting the translations of the apps in the order they appear in the
INSTALLED_APPS
setting, this block outputs the list of apps placing the project apps before the Django apps. Since this only happens when determining the translation to use, it doesn't affect any other part of the code and no other changes are necessary.It works on Django version 1.11 up to 2.2.