我正在嘗試使用i18n以不同語言呈現模板。我做了我能讀懂的所有內容,從設置語言代碼,創建和編譯翻譯文件(包括模板中的翻譯標記等),甚至通過{{LANGUAGE_CODE}}變量點,我的模板仍以英語呈現我打算渲染的正確(和不同)代碼。我錯過了什麼?django - 如何使翻譯工作?
模板:
{% 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 %}
觀點:
$ django-admin.py makemessages -l he-il -e html
$ django-admin.py compilemessages
settings.py:
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文件正確的翻譯填寫)
# 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',
)
剛纔遇到了這個有趣的行爲。 LANGUAGE設置應使用短劃線,但/ locale /中的路徑應使用下劃線。 – Andrew 2011-04-02 22:48:09
+1這就是它!這些互聯網上有許多關於這個問題的問題,並且你已經完成了。我剛剛將我的語言從fr-fr更名爲fr_fr,一切都變得生機勃勃! (在Django重啓後)。 – mkoistinen 2013-04-06 01:45:33
對我來說,我不得不使用:LANGUAGE_CODE ='en_GB',而在LANGUAGES =(('en-gb',ugettext('English')),...) – dalore 2013-10-31 12:34:04