Django网站有2种语言 [英] Django site with 2 languages

查看:126
本文介绍了Django网站有2种语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个有2种语言的网站,一种默认网站,一种是我的母语和一种可选的英语。我打算让我的域名如此:

I want to develop a site with 2 languages, a default one, my native language and an optional English. I plan to have my domains as such:

www.mydomain.com/tr/
www.mydomain.com/en/

默认情况下,一旦用户输入mydomain.com。他们将被重定向到 / tr / 版本,并选择通过顶部菜单进入/ en /。这是我的问题。

By default, once a user enter mydomain.com. they will be redirected to /tr/ version and select to go to the /en/ if they want via a top menu. And here is my question.

什么是最好的Django方式来维护这两种语言,请注意,我不想自动翻译,但我想保持两者的文本

What is the best Django way to maintain both languages, please note that I don't want automatic translation but I want to maintain texts for both languages myself.

谢谢

推荐答案

正如我所见,你这里有两个主要选项:

As I see it, you have two main options here:

(1)您可以将两个单独的网站副本作为不同的Django应用程序,只需将您的 urlconf 指向这些应用程序 - 所以 url(r'^ / en /' ,include(myproject.en))将在您的urlconf中指向您的英文应用程序,另一种用于其他语言。这将涉及为两个应用程序维护不同的urlconfs和不同的html模板等,如果您有兴趣使URL本身也反映不同的语言(例如,西班牙语/ pagina / unovs英文 / page / one)。

(1) You can keep two separate copies of the site as different Django apps and simply have your urlconf point to those apps-- so url(r'^/en/', include(myproject.en)) would be in your urlconf to point to your English app, and the other for the other language. This would involve maintaining different sets of urlconfs and different html templates, etc for the two apps, which could be useful if you're interested in having the URLs themselves also reflect the different languages (eg Spanish "/pagina/uno" vs English "/page/one").

(2)您可以使用 Django会话,然后让模板从该cookie提供您喜欢的文本的相应版本。代码可以是:

(2) You record the language preference in a cookie (which you should really do anyway) using Django sessions, and then have the template deliver the appropriate version of the text however you like from that cookie. The code for this could be:

# views.py

# default to your native language
request.session['lang'] = 'tr'

# someone clicks the link to change to English
def switch_to_English_link(request):
    request.session['lang'] = 'en'

然后在模板中收集这些信息, d使用:

And then in the templates, to gather this information, you'd use:

<!-- my_django_template.html -->
<div>
  <span>
     {% if request.session.lang == "en" %}
        This is my text in English!
     {% else %}
        Şimdi benim sitede Türk var!
     {% endif %}
  </span>
</div>

这篇关于Django网站有2种语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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