Django国际化

1.创建一个名为djangoi18n的project(在/root/work/目录下)

# django-admin.py startproject djangoi18n

2.在djangoi18n project目录下创建一个名为test2的Django App

#python manage.py startapp test2

3.修改djangoi18n/djangoi18n目录下的settings.py,将app:test2添加进去

# Application definition                                                                                                                           
                                                                                                                                                   
INSTALLED_APPS = (                                                                                                                                 
    'django.contrib.admin',                                                                                                                        
    'django.contrib.auth',                                                                                                                         
    'django.contrib.contenttypes',                                                                                                                 
    'django.contrib.sessions',                                                                                                                     
    'django.contrib.messages',                                                                                                                     
    'django.contrib.staticfiles',                                                                                                                  
    'test2',                                                                                                                                       
)   

4.在djangoi18n project目录下创建templates/test2/index.html文件(注意不要创建在app下,否则国际化将会失败,页面只显示英文)

{% load i18n %}                                                                                                                                    
<!DOCTYPE html>                                                                                                                                    
<html>                                                                                                                                             
  <head>                                                                                                                                           
    <meta charset='utf-8'/>                                                                                                                        
    <title>{%trans "welcome to my site" %}</title>                                                                                                 
    <script>                                                                                                                                       
      function selectdo(obj){                                                                                                                      
      str = "/i18n/setlang/";                                                                                                                      
      myform = document.getElementById('testform');                                                                                                
      myform.method = "POST",                                                                                                                      
      myform.action = str;                                                                                                                         
      myform.submit();                                                                                                                             
      }                                                                                                                                            
    </script>                                                                                                                                      
  </head>                                                                                                                                          
  <body>                                                                                                                                           
    {% get_available_languages as LANGUAGES %}                                                                                                     
    <form name="testform" id="testform" method="post">{% csrf_token %}                                                                             
      <select id="language" name="language" οnchange="selectdo(this)">                                                                             
        <!-- <input name="text" type="hidden" value="{{request.path}}"/> -->                                                                       
        <option value="1">{%trans "Languages" %}</option>                                                                                          
        {% for lang in LANGUAGES %}                                                                                                                
        <option value="{{lang.0}}">{{lang.1}}</option>                                                                                             
        {% endfor %}                                                                                                                               
      </select>                                                                                                                                    
    </form>                                                                                                                                        
    <p>{%trans "The first sentence is from the template index.html" %}</p>                                                                         
    {{code}}                                                                                                                                       
  </body>                                                                                                                                          
</html>          

5.修改settings.py,设置模板路径

TEMPLATES = [                                                                                                                                      
    {                                                                                                                                              
        'BACKEND': 'django.template.backends.django.DjangoTemplates',                                                                              
        'DIRS': ['/root/work/djangoi18n/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',                                                                             
            ],                                                                                                                                     
        },                                                                                                                                         
    },                                                                                                                                             
]          

6.修改djangoi18n/test2/views.py的代码

from django.shortcuts import render                                                                                                                
                                                                                                                                                   
# Create your views here.                                                                                                                          
from django.http import HttpResponse                                                                                                               
from django.shortcuts import render_to_response                                                                                                    
from django.template import RequestContext                                                                                                         
from django.utils.translation import ugettext_lazy as _                                                                                            
                                                                                                                                                   
def test2_view(request):                                                                                                                           
    code = _("The second sentence is from the Python Code.");                                                                                      
    responseContext = {                                                                                                                            
        'lang': request.LANGUAGE_CODE,                                                                                                             
        'code': code,                                                                                                                              
    }                                                                                                                                              
    resp = render_to_response('test2/index.html', responseContext, context_instance = RequestContext(request))                                     
    return resp                                                                                                                                    
          

7.更新djangoi18n/djangoi18n/urls.py

from django.conf.urls import include, url                                                                                                          
from django.conf.urls import *                                                                                                                     
from django.contrib import admin                                                                                                                   
from test2.views import *                                                                                                                          
                                                                                                                                                   
urlpatterns = [                                                                                                                                    
    url(r'^admin/', include(admin.site.urls)),                                                                                                     
    url(r'^test2_view$', test2_view),                                                                                                              
    url(r'^i18n/', include('django.conf.urls.i18n')),                                                                                              
]                                                                                                                                                  
       

8.更新djangoi18n/djangoi18n/settings.py,更新国际化相关设置

USE_I18N = True 


 ugettext = lambda s: s 

 LANGUAGES = ( 
    ('en-us', ugettext('English')), 
    ('zh-CN', ugettext('Chinese')), 
 ) 

 
 MIDDLEWARE_CLASSES = ( 
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.locale.LocaleMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
 )

9.用数据库保存session,在本实例中用到了 mysql test2 数据库中的 django_session 数据表。我们在project 目录下运行 Python manage.py syncdb 就能产生 django_session 了

# python manage.py syncdb

10.创建并更新语言文件,在project目录下创建locale文件,并更改django.po文件

# mkdir

# django-admin.py makemessages -l zh_CN

# emacs locale/zh_CN/LC_MESSAGES/django.po

#: djangoi18n/settings.py:93                                                                                                                                
msgid "English"                                                                                                                                             
msgstr "英语"                                                                                                                                               
                                                                                                                                                            
#: djangoi18n/settings.py:94                                                                                                                                
msgid "Chinese"                                                                                                                                             
msgstr "中文"                                                                                                                                               
                                                                                                                                                            
#: templates/test2/index.html:6                                                                                                                             
msgid "welcome to my site"                                                                                                                                  
msgstr "欢迎访问我的网站"                                                                                                                                   
                                                                                                                                                            
#: templates/test2/index.html:22                                                                                                                            
msgid "Languages"                                                                                                                                           
msgstr "语言"                                                                                                                                               
                                                                                                                                                            
#: templates/test2/index.html:28                                                                                                                            
msgid "The first sentence is from the template index.html"                                                                                                  
msgstr "第一句话来自index.html模板"                                                                                                                         
                                                                                                                                                            
#: test2/views.py:10                                                                                                                                        
msgid "The second sentence is from the Python Code."                                                                                                        
msgstr "第二句话来自python代码"    

11.编译信息文件

# django-admin.py compilemessages

12.在test2目录下创建locale目录,并执行django-admin.py makemessages -l zh_CN,将djangoi18n目录下的django.po文件,复制到test2/locale/... 目录下,然后编译信息文件

(注:这一步是自己试验出来的,因为按照参考的网站的步骤,无法实现中英文切换,只能显示英文)

13.启动服务

python manage.py runserver 0.0.0.0:8000


参考网站:点击打开链接













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值