已经三天了,我已经厌倦了。 这是我的设置文件:
from pathlib import Path
from django.utils.translation import gettext_lazy as _
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-z$__b9aa(_3!czi7ssop9cr2lae^9)f^!_kq+y_n=+6u=ul!%q"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"mainapp",
]
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.locale.LocaleMiddleware',
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "soroushprojects.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ['templates'],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
'django.template.context_processors.i18n',
],
},
},
]
WSGI_APPLICATION = "soroushprojects.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = "en"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
LAGUAGES = (
('en', _('English')),
('fa', _('Persian')),
)
LOCALE_PTHS = [
BASE_DIR / 'locale',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
STATICFILES_DIRS = ['static']
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
如您所见,我尝试将“django.middleware.locale.LocaleMiddleware”添加到 MIDDLEWARE 并将“django.template.context_processors.i18n”添加到内容处理器。
网址:
from django.contrib import admin
from django.urls import path, include
from mainapp import views
urlpatterns = [
path('contact/', views.SendMessage.as_view()),
path("admin/", admin.site.urls),
path('about/', views.about),
path('articles/<int:id>/', views.ShowArticlesView.as_view()),
path('articles/', views.ArticlesListView.as_view()),
path('projects/<int:id>/', views.ShowProjectView.as_view()),
path('projects/', views.ProjectsListView.as_view()),
path('i18n/',include('django.conf.urls.i18n')),
path('', views.home),
]
浏览次数:
def home(request):
return render(request, 'home.html')
区域设置文件:
locale/
en/
LC_MASSAGES/
django.mo
django.po
fa/
LC_MESSAGES/
django.po
django.mo
fa/LC_MESSAGES/django.po:
# 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: 2022-12-07 00:03+0330\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: \n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: .\soroushprojects\settings.py:112
msgid "English"
msgstr "الگلیسی"
#: .\soroushprojects\settings.py:113
msgid "Persian"
msgstr "فارسی"
#: .\templates\home.html:6
msgid "Hello"
msgstr "سلام"
home.html:
{% load i18n %}
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
<input name="language" type="hidden" value="fa"/>
<input type="submit" value="change"/>
</form>
{% trans "Hello" %}
当我检查浏览器 cookie 时,djngo_language 设置为“fa”,但它不会将“Hello”一词翻译为“Săram”:
我尝试了
manage.py makemessages --all
和manage.py compilemessages
数百次。我尝试了在互联网上找到的所有解决方案,但没有一个有效。然后我删除了我的项目并将其所有代码移至一个新项目,但这也不起作用。我什至尝试以不同的方式多次重新安装 gettext。我还尝试使用 {% blocktrans %}{% endblocktrans %} 而不是 trans 标签。我简直要疯了
我希望 django 将“Hello”翻译为“Săram”
'django.middleware.locale.LocaleMiddleware'
应放置在 "django.contrib.sessions.middleware.SessionMiddleware"
和 "django.middleware.common.CommonMiddleware"
之间,因此您的 MIDDLEWARE
设置应如下所示:
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
'django.middleware.locale.LocaleMiddleware',
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]