django-多语言和模板端语言之间的切换

发布于 2024-08-05 00:01:53 字数 173 浏览 8 评论 0 原文

我正在尝试使用 django-multilingual 并正确设置它。但我发现除了模板使用示例之外,django-multilingual 的一切都很清楚。

我刚开始使用django,我不知道,也许因为这个原因,我不知道如何在模板端切换语言。

您是否可以提供任何示例或任何有关此的“更多”明确来源/文档?

I'm trying to use django-multilingual and setup it properly. But what I found is that everything is clear for django-multilingual except a template usage example.

I just started to use django and I don't know, maybe because of this reason, I cannot figure out how to switch between languages on template side.

Is there any example that you can give or any 'more' clear source/documentation about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如狂蝶 2024-08-12 00:01:53

在 django 中切换语言环境是一个简单的帖子,执行此视图

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view

在模板中,您可以使用 request.LANGUAGE_CODE 访问语言值

switching locale in django is a simple post do this view

https://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view

in templates you can access the language value with request.LANGUAGE_CODE

吾性傲以野 2024-08-12 00:01:53

您可能还想尝试 django-localeurl 应用。它使用户能够切换将当前区域设置存储在 URL 中的区域设置。它还提供了几个有用的模板标签,用于切换和显示可用的区域设置。

You might also want to try django-localeurl app. It enables users to switch locales storing current locale in the URL. It also provides several useful template tags for switching and displaying available locales.

三岁铭 2024-08-12 00:01:53

您可以按照 i18n 切换器 ="nofollow noreferrer">set_language 重定向视图,但首先,您最好按照 我的答案,你可以看到我的问题我的回答解释了如何为 Django Admin 创建i18n 切换器。 *我使用Django 4.2.1

然后,将 path("i18n/", include("django.conf.urls.i18n")) 添加到 core/settings.py 中的 urlpatterns代码>如下所示。 *您不应在 i18n_patterns() 中包含 path("i18n/", include("django.conf.urls.i18n")) 才能根据 文档

# "core/settings.py"

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns(
    path('admin/', admin.site.urls),
    path("my_app1/", include('my_app1.urls')),
)

# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Here ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
urlpatterns += [
    path("i18n/", include("django.conf.urls.i18n"))
]

然后,将

...

添加到 templates/index.html 作为如下所示:

{% "templates/index.html" %}

{% load i18n %}

{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Here ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}">
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go">
</form>
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Here ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}

{% translate "Hello" %} {% trans "World" %}

现在,您可以将英语切换为法语,如下所示:

在此处输入图像描述

而且,您可以切换法语英文,如下所示:

在此处输入图像描述

You can create i18n switcher following The set_language redirect view but first, you better set up translation(English and French) following my answer and you can see my question and my answer explaining how to create i18n switcher for Django Admin. *I use Django 4.2.1.

Then, add path("i18n/", include("django.conf.urls.i18n")) to urlpatterns in core/settings.py as shown below. *You should not include path("i18n/", include("django.conf.urls.i18n")) in i18n_patterns() to work correctly according to the doc:

# "core/settings.py"

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns(
    path('admin/', admin.site.urls),
    path("my_app1/", include('my_app1.urls')),
)

# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Here ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
urlpatterns += [
    path("i18n/", include("django.conf.urls.i18n"))
]

Then, add <form action="{% url 'set_language' %}" ...>...</form> to templates/index.html as shown below:

{% "templates/index.html" %}

{% load i18n %}

{% ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Here ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ %}
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}">
    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go">
</form>
{% ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Here ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ %}

{% translate "Hello" %} {% trans "World" %}

Now, you can switch English to French as shown below:

enter image description here

And, you can switch French to English as shown below:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文