django makemessages 什么都不做

django makemessages does nothing

当我 运行 django-admin makemessages -l en 没有任何反应,也没有创建 po 文件。它只说 processing locale en

这是我的文件夹结构

/myproject
    myapp/
    locale/
    media/
    static/
    templates/
    db.sqlite
    manage.py
    settings.py
    urls.py
    wsgi.py

settings.py

import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_user_agents.middleware.UserAgentMiddleware',
]

ROOT_URLCONF = 'urls'

WSGI_APPLICATION = 'wsgi.application'

LANGUAGE_CODE = 'fr'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

您可能应该将您的应用添加为“myapp.apps.MyappConfig”,而不仅仅是在您的 settings.py INSTALLED_APPS 中添加“myapp”,并将您的应用添加到 LOCALE_PATHS

LOCALE_PATHS = ( 
os.path.join(BASE_DIR, "locale"),
os.path.join(BASE_DIR, "yourapp/locale"), 

)

我发现如果任何模板中都没有引用翻译,makemessages 不会创建 po 文件

我希望 makemessages 创建一个空的 po 文件,我可以继续编辑它,但它不能那样工作。至少有一个模板必须对第一次创建的文件进行翻译。

{% load i18n %}
....
{% trans 'Welcome' %}
python manage.py makemessages -l en
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-11-08 18:58+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: .\templates\index.html:4
msgid "Welcome"
msgstr ""

我希望这对遇到同样问题的任何人都有用