如何使用django.views.i18n.set_language()函数? [英] How to use django.views.i18n.set_language() function?

查看:206
本文介绍了如何使用django.views.i18n.set_language()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一个教程 in Django网站,但我不明白如何使用set_language()函数。例如,我有一个演示如下:

I read a tutorial in Django site but I don't understand how to use the set_language() function. For example, I have a demo follow as:

index.html

{% trans "Hello World." %}

<form action="/i18n/setlang/" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="/next/page/" />
<select name="language">
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}">{{ language.name_local }} ({{ language.code}})</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>

urls.py

urlpatterns = patterns('',
    (r'^i18n/', include('django.conf.urls.i18n')),
)

views.py

What need I write to display the languages, which were chosen from user in this file?

谢谢,

Thinh

推荐答案

使用您正在使用的代码,您不需要编写自己的视图。该表单将向/ i18n / setlang /发送POST请求,其语言代码和(可选)重定向到(下一个)页面作为参数。

With the code you are using, you don't need to write your own views. The form will make a POST request to /i18n/setlang/, with the language code and (optional) the redirect-to (next) page as parameters.

django视图执行以下操作(从django文档)

The django view does the following (from the django documentation)

Django在POST数据中查找下一个参数。
- 如果不存在或为空,Django会尝试引荐来源网址中的网址。
- 如果这是空的 - 说如果用户的浏览器禁止该标题 - 那么用户将被重定向到/(站点根目录)作为后备。

Django looks for a next parameter in the POST data. - If that doesn't exist, or is empty, Django tries the URL in the Referrer header. - If that's empty -- say, if a user's browser suppresses that header -- then the user will be - redirected to / (the site root) as a fallback.

因此,在提交表单后,用户将被重定向,而django视图将根据提交的内容设置该用户的语言。

So in essence, the user will be redirected after submitting the form, and the django view will set the language for that user according to what was submitted.

希望这有帮助,

Hoff

这篇关于如何使用django.views.i18n.set_language()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆