`
brucewei777
  • 浏览: 82949 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Django项目的国际化(i18n)

阅读更多
    最近在搞django项目的国际化,综合了多家的帖子,才算搞成功,所以,若有人发觉什么地方在别的地方也出现了,不要惊讶。
    创建一个i18ntest的项目,底下又建了个ajax的app。和许多文章说的一样,settings.py的设置一定要更改
引用

USE_I18N = True #本来就有,确保为True
'django.middleware.locale.LocaleMiddleware',#注意与别的中间件的顺序,要在AuthenticationMiddleware前面。


我本来在ajax底下建了locale目录,然后用django-admin.py makemessage -l zh_CN,产生.po文件,用poedit编辑,保存,得到.mo文件。启动服务,结果语言包就是应用不了,跟了一下,发现用的是django自带的语言包,而没用我编辑的。想想可能是路径有问题。

就把locale目录拷贝到i18ntest底下,一运行,呵呵,搞定。

期间还有别的错误,例如_()这个函数不识别,解决方法是安装三个包
引用

gettext-tools-0.17.zip
gettext-runtime-0.17.zip
libiconv-1.9.1.bin.woe32.zip

把他们解压缩到同一个目录下,并加入环境变量,再在views.py里加入一条
from django.utils.translation import ugettext as _


Django1.8.x在这方面有些变动,参考:
引用

After update django to 1.8.x, when run the project, one warning message is displayed:

/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py:146: RemovedInDjango19Warning: The use of the language code 'zh-cn' is deprecated. Please use the 'zh-hans' translation instead.

To fix this:

    First go to project setting file, change 'zh-cn' to 'zh-hans'
    Second, rename the 'zh-CN' folder in locale to 'zh-Hans'
    Run django-admin makemessages and django-admin compilemessages if necessary
    Go to your template, in the language toggle form, change language input value to zh-hans
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics