fxxkgw
V2EX  ›  Django

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

  •  
  •   fxxkgw · Dec 13, 2018 · 2568 views
    This topic created in 2740 days ago, the information mentioned may be changed or developed.

    部分返回数据要由中文转英文,使用了 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。。

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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2941 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 91ms · UTC 13:01 · PVG 21:01 · LAX 06:01 · JFK 09:01
    ♥ Do have faith in what you're doing.