Django内化 - 改变当前语言不起作用

时间:2017-01-04 20:01:21

标签: django localization gettext

我想本地化我的REST应用程序,并阅读doc。好像我已经相应地做了所有事情,但是在我的例外情况下无法获得的信息在客户端以俄语显示。

观点:

@api_view(['POST'])
def foo(request):
    if serialized.is_valid():
        activation_key = hashlib.sha1(str(random.random())).hexdigest()[:5]
        email = request.data['email']
        user_language = 'ru'
        translation.activate(user_language)
        request.session[translation.LANGUAGE_SESSION_KEY] = user_language
        try:
            # SMTPRecipientsRefused error generating code
        except SMTPRecipientsRefused:
            return Response({'msg': _('Invalid email address'),
                             'object': None}, status=status.HTTP_400_BAD_REQUEST)
     # ...

这就是我为解决本地化所做的工作。

  1. 为Windows设置gettext,从this source
  2. 下载相应的文件
  3. 执行django-admin compilemessages命令并成功生成.po个文件
  4. 打开生成的.po文件并填写必要的翻译
  5. 编辑中间件以包含所有必要元素
  6. .po文件如下所示:

    # SOME DESCRIPTIVE TITLE.
    # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
    # This file is distributed under the same license as the PACKAGE package.
    # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    #
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSION\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2017-01-04 19:49+0400\n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    "Language-Team: LANGUAGE <LL@li.org>\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=4; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
    "%10<=4 && (n%100<12 || n%100>14) ? 1 : n%10==0 || (n%10>=5 && n%10<=9) || (n"
    "%100>=11 && n%100<=14)? 2 : 3);\n"
    
    #: .\rest\views.py:46
    msgid "Invalid email address"
    msgstr "Некорректный почтовый адрес"
    

    中间件:

    MIDDLEWARE_CLASSES = [
         #...
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
    ]
    

    我在这里缺少什么帮助?

0 个答案:

没有答案