Django {% translate "Log out" %} 不按字面意思翻译

Django {% translate "Log out" %} does not follow translation literally

我正在对我的应用程序进行国际化,需要翻译字符串 "Log out"。但是,Django 并没有按照我的字面翻译,而是按照自己的方式翻译"Log out"

在我的具体示例中,我试图将 "Log out" 翻译成 "Esci",它的意大利语等价物。但是,当我渲染模板时,我得到了翻译 "Annulla accesso",它大致翻译成英文 "Undo access"

此外,我发现如果我使用 {% translate "Logout" %} 甚至 {% translate "Log Out" %},一切都会如我所料。

字符串 "Log out" 有什么我应该知道的特别之处吗?如何覆盖此行为?

这是我的 HTML:

{% load i18n %}

{% block body %}
    <h1>{% translate "Hello" %}, {{ user.username }}</h1>
    <ul>
        <li>{% translate "Currently logged in as" %}: {{ user.username }}</li>
        <li><a href="{% url 'users:logout' %}">{% translate "Log out" %}</a></li>
    </ul>
{% endblock %}

这是我的 .po 文件:

#: users/templates/users/user.html:9
msgid "Log out"
msgstr "Esci"

同样,尽管如此,在渲染时,"Log out" 被翻译为 "Annulla accesso"

[..] it looks for translations by following this algorithm regarding the order in which it examines the different file paths to load the compiled message files (.mo) and the precedence of multiple translations for the same literal:

  1. The directories listed in LOCALE_PATHS have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later.
  2. Then, it looks for and uses if it exists a locale directory in each of the installed apps listed in INSTALLED_APPS. The ones appearing first have higher precedence than the ones appearing later.
  3. Finally, the Django-provided base translation in django/conf/locale is used as a fallback.

https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#how-django-discovers-translations

显然,Django 管理应用程序在您的 INSTALLED_APPS 中以更高的优先级列出,并且其 Log out 本地化覆盖了您的本地化。更改已安装应用程序的顺序,或将您的应用程序的语言环境路径明确添加到 LOCALE_PATHS,以防前一种解决方案造成任何问题。