V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fxxkgw
V2EX  ›  Django

Django1.8 i18n 中文转英文不起作用

  •  
  •   fxxkgw · 2018-12-13 20:13:28 +08:00 · 1921 次点击
    这是一个创建于 1932 天前的主题,其中的信息可能已经有所发展或是发生改变。

    部分返回数据要由中文转英文,使用了 django 的 i18n 模块。

    django:1.8
    python:2.7.5
    

    settings 里设置的地方有:

    MIDDLEWARE_CLASSES:

    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.locale.LocaleMiddleware',  # 新加 
    

    TEMPLATES: 'django.template.context_processors.i18n',

    LANGUAGES = ( ('en', 'English'), ('zh-hans', u'中文简体'), )

    LOCALE_PATHS = ( SITE_ROOT + '/locale', # SITE_ROOT 为项目路径 )

    LANGUAGE_CODE = 'en'

    USE_I18N = True

    USE_L10N = True

    然后使用 python manage.py makemessages --local=en 生成 po 文件,修改要转译的部分。如:

    msgid "世界"
    msgstr "world"
    

    最后,python manage.py compilemessages --local=en 生成 mo 文件。

    views.py 里添加:

    from django.utils.translation import gettext as _
    
    msg = _("世界")
    return HttpResponse(msg, status=200)
    

    浏览器设置为英文模式,但返回的还是 世界,并不是 world。。

    希望能得到一些指点,还需要什么信息我再添加。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1220 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 18:10 · PVG 02:10 · LAX 11:10 · JFK 14:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.