Django翻译不适用于html模板



运行Django 4.1的项目,设置如下:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.locale.LocaleMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
LANGUAGE_CODE = 'uk'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = (
os.path.join(BASE_DIR, '/locale'),
os.path.join(BASE_DIR, 'apps/hub/locale'),
)
LANGUAGES = (
('uk', _('Українська')),
('en', _('English')),
)

我运行makemessages和compilemessages,并获得具有以下内容的dajngo.po文件:以下两项来自html,语言更改后不会更改。

#: .templates_footer.html:14 .templates_header.html:12
msgid "Головна"
msgstr "Home"
#: .templates_footer.html:15 .templates_header.html:13
msgid "Про нас"
msgstr "About"

但下面这个项目确实有效:

#: .appshubviews.py:19
msgid "Выход"
msgstr "Exit"

除base.html以外的所有html文件都以{% load i18n %}开头,并带有标记{% translate "Головна" %}我错过什么了吗?

好吧,我已经解决了这个问题。问题出现在LOCALE_PATHS 中

LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),  # there was a redundant /
os.path.join(BASE_DIR, 'apps/hub/locale'),
)
print(LOCALE_PATHS) # showed me a wrong path

最新更新