django - 如何使翻译工作? [英] django - how to make translation work?

查看:109
本文介绍了django - 如何使翻译工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用i18n以不同的语言呈现模板。我做了一切我可以看到的,从设置语言代码,创建和编译翻译文件,包括模板中的翻译标签和所有这些,我的模板仍然呈现英文,即使通过{{LANGUAGE_CODE}}变量指向我想要呈现的正确(和不同的)代码。我缺少什么?

I'm trying to render a template in a different language using i18n. I did everything I could read about, from setting the language code, creating and compiling translation files, including the translation tags in the template and all that, and my template still renders in English, even through the {{ LANGUAGE_CODE }} variable points to the correct (and different) code I intended to render. What am I missing?

模板:

{% extends "base.html" %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_current_language_bidi as LANGUAGE_BIDI %}
{% block title %}{% trans "translation test" %}{% endblock %}
{% block content %}
<div id="some-text">
  {% trans "some translated text goes here" %}
  {% blocktrans %}
  <ol>
    <li>here are some</li>
    <li>items that should be</li>
    <li>translated as well</li>
  </ol>
  {% endblocktrans %}
  <ul>
      <li>The current language is <b>{{ LANGUAGE_CODE }}</b></li>
      {% if LANGUAGE_BIDI %}
        <li>The current language is bidirectional</li>
      {% else %}
        <li>The current language is <b>not</b> bidirectional</li>
      {% endif %}
      <li>Available languages are:
        <ul>
        {% for lang in LANGUAGES %}
          <li>{{ lang.1}}</li>
        {% endfor %}
        </ul>
      </li>
  </ul>
</div>
{% endblock %}

查看:

from django.shortcuts import render_to_response
from django.template import RequestContext
from pdb import set_trace as debugger
def check(request):
    return render_to_response('index.html', context_instance=RequestContext(request)

命令行(我确实填写了.po文件中的正确翻译):

command line (I did fill in the correct translations in .po files):

$ django-admin.py makemessages -l he-il -e html
$ django-admin.py compilemessages

settings.py:

settings.py:

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'he-il'

gettext = lambda s: s
LANGUAGES = (
    ('he-il', gettext('Hebrew')),
    ('en-us', gettext('English')),
)

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.i18n",
)

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


推荐答案

我的方式是使用django在其自己的翻译文件中使用的确切语言代码(而不是通过settings.py中提供的链接),假设这种语言是支持的(如果不是很复杂,因为你必须提供自己的翻译文件到django)。

The way I went about it is by using the exact language code that django uses in it's own translation files (and not by the link provided inside settings.py), assuming this language is supported (if not things get complicated, since you have to provide your own translation files to django as well).

我发现这个代码去$ DJANGO_DIR / conf / locale并查看该文件夹的名称(对我来说,它在/usr/local/lib/python2.6/dist-packages/dj ango / conf / locale,但可能会因操作系统而异)。

I found this code by going to $DJANGO_DIR/conf/locale and looking at the folder's name (for me it was at /usr/local/lib/python2.6/dist-packages/django/conf/locale, but it may differ depending on OS and such).

这篇关于django - 如何使翻译工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆