Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django i18n not working

A python newbie here. I wanna my website support English and Chinese. So I just follow django book, chapter 19 internationalization. But it seems doesn't work for me, string I hope to be displayed as chinese, still english there. My code and settin is as following.

[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 app views.py, I forcely set language code as 'zh-cn' in index

def index( request ):
    response= render_to_response( 'index.htm' )
    response.set_cookie('django_language','zh-cn')
    return response

then I'd hope annother page that will be loaded after index.htm, will display a chinese string.

Annother page is renderred by upload.html

{% load i18n %}
<html>
<head>
{% block head %}

{% endblock %}
</head>
<body>
{% block body %}
<h1>{% trans 'Upload Demo' %}</h1>
{% endblock %}
</body>
</html>

After then, I do

django-admin.py makemessages -l zh-cn -e htm

in my django project folder, and I got django.po at locale/zh-cn/LC_MESSAGES/django.po which content is 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 <[email protected]>\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 "上传文件"

Thereafter, I call following command to compile message django-admin.py compilemessages

I got django.mo file at some folder with django.po

Fistly I access the index page, then I access another page, which has 'Upload Demo' string id. Actually I still see english string there.

And tried debug via printing language code, find that language has been set correctly.

context = RequestContext(request) print context translation.activate('zh-cn')

Lastly, I use

gettext locale/zh-cn/LC_MESSAGES/django.mo "Upload Demo"

really got 'Upload Demo'. So I think problem is here. But why this happen? I really confused. Can any body help me.

Deeply appreciated any comment or help.


gettext locale/zh-cn/LC_MESSAGES/django.mo "Upload Demo"

I think I made a mistake. Above command return a string that is same as string you typed as string ID rather than translated string. In above command, it is "Upload Demo", That is if your change "Upload Demo" in above command as "bla bla", you will "bla bla".

like image 382
Leo Hwang Avatar asked May 10 '12 07:05

Leo Hwang


People also ask

How do I enable i18n in Django?

By default, Django is geared up to handle 18n. If you navigate to your project’s settings.py, you will notice that the variable USE_I18N is set to True. Even if your project does not use i18n, this can remain enabled in Django as it causes no overhead to your overall project.

How do I change the default language in Django?

Also, you can set the default language using the variable LANGUAGE_CODE in the same settings.py file. Next, you can set up the languages that you plan to support in your Django application: You can also set the variable LANGUAGE_BIDI, which decides the direction in which the text is read.

How do I defer translation in Django?

An interesting feature of Django is the ability to defer translation until it is absolutely necessary. Django has a pre-defined function, gettext_lazy (), which translates strings lazily. The translation takes place when the application needs to use the value of a string, and not when the view is called up.

Can you do translation and localization in Django?

In this article, you'll learn how to easily do translation and localization in Django, the popular Python web framework. Read on! Join the DZone community and get the full member experience. Learn how to easily do translation and localization in Django, the popular Python web framework.


2 Answers

Maybe it's too late, but I bet your problem probably was due to missing LOCALE_PATHS tuple in your project's settings.

After an hour of pulling my hair out, I solved the same problem by simply adding this to the settings.py file in my project:

LOCALE_PATHS = (
    '/home/myuser/Projects/Some_project_root/My_django_project_root/locale',
)

And this is how that directory looks:

locale/
├── de
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
├── en
│   └── LC_MESSAGES
│       ├── django.mo
│       └── django.po
└── es
    └── LC_MESSAGES
        ├── django.mo
        └── django.po
like image 147
Diego Schulz Avatar answered Sep 21 '22 22:09

Diego Schulz


Your codeblocks are a bit messy so it is quite hard to read it all. But you might want to start with your .mo file. It contains a #, fuzzy annotation. Fuzzy means that the build script was not sure about the translation and therefore requires attention of the translator (=you). Start by checking all #, fuzzy marked translations. If the translation is correct (or after you have corrected the wrong translation) remove the #, fuzzy annotation. Next run compile messages again. This could fix your problem.

#, 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 \n" 
"Language-Team: LANGUAGE \n" 
"Language: \n" 
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

See also: django fuzzy string translation not showing up

Friendly regards, Wout

like image 23
Wouter Klein Heerenbrink Avatar answered Sep 21 '22 22:09

Wouter Klein Heerenbrink