django 切换语言不起作用(i18n)

django switch Language doesn't work (i18n)

我按照网上的教学。

为什么我总是翻译不成功?

请帮我看看我哪里漏了。

我的 {% trans "test" %} 从不翻译。

然后我在html中点击其他语言,它会转到http://127.0.0.1:8000/i18n/setlang/

我不知道哪里错了。

Django 版本 3.2

settings.py

MIDDLEWARE = [
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.i18n',
                ...
            ],
        },
    },
]

LANGUAGES = (
    ('en-us', 'English (United States)',),
    ('zh-tw', '繁體中文(台灣)')
)

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

LOCALE_PATHS = [Path(BASE_DIR, "locale")]

USE_TZ = True

projects.urls.py

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
]

urlpatterns += i18n_patterns(
    path('', include('App.urls', namespace='app')),
)

App.urls.py

app_name = 'App'

urlpatterns = [
    path('index/', views.I18_Test.as_view(), name='index'),
]

语言环境

en_us

#: templates/hello.html:12
msgid "test"
msgstr "EEE"

#: templates/hello.html:19
msgid "Language"
msgstr "En"

zh_tw

#: templates/hello.html:12
msgid "test"
msgstr "TTT"

#: templates/hello.html:19
msgid "Language"
msgstr "TW"

html

{% load i18n %}

<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>

{% trans "test" %}

<nav class="navbar navbar-expand-lg navbar-light navbar-suspend" id="base-navbar">
    <div class="container">
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="/" id="navbarDropdown" role="button"
                       data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{% trans "Language" %}</a>
                    <form action="{% url 'set_language' %}" method="post" id="lang_form">{% csrf_token %}
                        <input name="next" type="hidden" value="{{ redirect_to }}">
                        <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                            {% get_current_language as LANGUAGE_CODE %}
                            {% get_available_languages as LANGUAGES %}
                            {% get_language_info_list for LANGUAGES as languages %}
                            {% for language in languages %}
                            <li>
                                <button type="submit" name="language" value="{{ language.code }}"
                                        class="{% if language.code == LANGUAGE_CODE %}selected{% else%}demo-text-black{% endif %} dropdown-item">
                                    {{ language.name_local }}
                                </button>
                            </li>
                            {% endfor %}
                        </ul>
                    </form>
                </li>
            </ul>
        </div>
    </div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
</body>
</html>

zh-tw 是旧的 Django 版本。

新版本必须使用zh-hant