django翻译不工作虽然dgango_language cookie被设置

2g32fytz  于 2022-12-14  发布在  Go
关注(0)|答案(1)|浏览(105)

已经3天了,我已经厌倦了这样,下面是我的设置文件:

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/信用卡信息/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 <LL@li.org>\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" %}

当我检查我的浏览器cookies时djngo_language被设置为'fa',但它不能将单词“Hello”翻译成“al-how ":Browser cookies
我试了几百次manage.py makemessages --allmanage.py compilemessages。我试了我在互联网上找到的每一个解决方案,但都不起作用。然后我删除了我的项目,并将其所有代码移到一个新项目中,但那也不起作用。我甚至多次尝试用不同的方式重新安装gettext。我还尝试使用{% blocktrans %}{% endblocktrans %}而不是trans标记。我“我简直要发疯了
我以为 Django 会把“你好”翻译成“你好”

vojdkbi0

vojdkbi01#

你用的是什么版本的Django?trans HTML模板标签似乎不再使用了,在4.x文档中translate是示例中标签的名称。
我也会检查这个,以防它与Accept-Language HTTP头有任何关系。

相关问题