Python newbie. I want my site to support English and Chinese. So I just follow the book of django, the internationalization of chapter 19. But it doesn't seem to work for me, the line I hope to show as Chinese is still English. My code and settings are as follows.
[settings.py]
LANGUAGE_CODE = 'zh-cn' USE_I18N = True USE_L10N = True LANGUAGES = ( ('en', 'English'), ('zh-cn', 'Chinese') ) TEMPLATE_CONTEXT_PROCESSORS = { 'django.core.context_processors.i18n', } MIDDLEWARE_CLASSES = ( 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', )
In my views.py application, I pre-set the language code as "zh-cn" in the index
def index( request ): response= render_to_response( 'index.htm' ) response.set_cookie('django_language','zh-cn') return response
then I hope that the page that will be loaded after index.htm displays the Chinese line.
The Annuar page is displayed in the upload.html file.
{% load i18n %} <html> <head> {% block head %} {% endblock %} </head> <body> {% block body %} <h1>{% trans 'Upload Demo' %}</h1> {% endblock %} </body> </html>
After that i do
django-admin.py makemessages -l zh-cn -e htm
in my django project folder, and I got django.po at national_version / wf / LC_MESSAGES / django.po whose contents look like
#, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2012-05-10 18:33+0800\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" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: uploader/template/base.htm:10 msgid "Upload Demo" msgstr "δΈδΌ ζδ»Ά"
After that, I call the following command to compile the django-admin.py compilemessages message
I have a django.mo file in some folder with django.po
I go directly to the index page, then I get access to another page that has the line identifier "Download demo". In fact, I still see the English string.
And after trying debugging using the print language code, make sure that the language is installed correctly.
context = RequestContext (request) print context translation.activate ('zh-cn')
Finally i use
gettext locale/zh-cn/LC_MESSAGES/django.mo "Upload Demo"
really got a "Download Demo". So I think the problem is here. But why is this happening? I am really embarrassed. Can anyone help me out.
Deeply appreciated any comments or help.
gettext locale/zh-cn/LC_MESSAGES/django.mo "Upload Demo"
I think I made a mistake. The above command returns a string that matches the string entered as the string identifier, not the translated one. In the above command, this is βDownload demoβ, that is, if your change to βDownload demoβ in the command above βbla blaβ, you will be βbla blaβ.