Git 在 Django 中翻译文件时的命令

Git command when translating files in Django

我在 Django 中已有一个应用程序。 我想在页面上添加翻译。 在页面上我有:

{% trans 'Projects'%}

我在.po文件中添加了:

#: templates/staff/site.html: 200
msgid "Projects"
msgid "Projekty"

然后执行命令:

django-admin.py compilemessages -l pl

执行此命令后,出现错误:

CommandError: This Should Be Run script from the Django Git checkout or your project or app tree, or with the settings Specified module.

错误包含答案,您可能是 运行 来自 任何地方 的脚本,所以它不知道要编译哪些文件。 运行 来自项目目录的命令或指定设置,你应该没问题。

$ python manage.py compilemessages --settings nsp.settings

CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.

我确实在项目根文件夹中时遇到此错误。 问题是,我 运行 这个命令没有先 python manage.py makemessages

错误消息具有误导性。

如果您使用 docker 个容器来构建和部署您的应用程序,您应该复制文件夹:

conf/

从你的 django 项目的根文件夹。 使用 conf 文件夹,您应该看到即:

processing file django.po in /gamma/conf/locale/en/LC_MESSAGES
processing file django.po in /gamma/conf/locale/es/LC_MESSAGES
processing file django.po in /gamma/conf/locale/pt_BR/

如果没有 conf 文件夹,您应该会看到这样一条毫无头绪的消息:

CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.

如果您还没有在您的设置文件中设置 LOCALE_PATHS,您需要这样做:

import os
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]

错误消息说它无法在预期的位置找到翻译文件。检查所有设置是否正确:

  • LOCALE_PATHS 在您的 settings.py
  • 中定义
  • 文件存在于上面定义的文件夹中(由 运行ning python manage.py makemessages 创建)
    • 实际上,即使只有一个空的区域设置文件夹,错误也会消失
  • compilemessages命令来自项目根文件夹运行