Django: How to add Chinese support to the application

Pavulon picture Pavulon · Oct 11, 2011 · Viewed 13.3k times · Source

I am trying to add a Chinese language to my application written in Django and I have a really hard time with that. I have spent half a day trying different approaches, no success.

My application supports few languages, this is part of settings.py file:

TIME_ZONE = 'Europe/Dublin'
LANGUAGE_CODE = 'en'

LOCALES = (
    #English
    ('en', u'English'),

    #Norwegian
    ('no', u'Norsk'),

    #Finish
    ('fi', u'Suomi'),

    #Simplified Chinese
    ('zh-CN', u'简体中文'),

    #Traditional Chinese
    ('zh-TW', u'繁體中文'),

    #Japanese
    ('ja', u'日本語'),
)

At the moment all (but Chinese) languages work perfectly. This is a content of locale directory:

$ ls locale/
en
fi
ja
no
zh_CN
zh_TW

In every directory I have LC_MESSAGES directory with *.mo and *.po files. *.po files are created by script written in Python, which converts *.ODS to a text file. *.mo files are created by python manage.py compilemessages command.

Language can be selected by user from the proper form in "Preferences" section in my application.

Django does not load Chinese translation. That is the problem. Both simplified and traditional does not work. I have tried different variations of language and locale codes in settings.py and in locale directory: zh-CN, zh-cn, zh_CN, zh_cn. No success.

Perhaps I made a simple mistake? I have added Polish language just for test and everything went fine. Basically I did the same. I have added ('pl', u'Polish') tuple to the settings.py and "locale/pl" with *.po and *.mo and LC_MESSAGES directory...

Do you know what might be wrong?

Answer

Derek Kwok picture Derek Kwok · Oct 11, 2011

You will need to use lower case for your locale language codes for it to work properly. i.e. use

LANGUAGES = (
    ('zh-cn', u'简体中文'), # instead of 'zh-CN'
    ('zh-tw', u'繁體中文'), # instead of 'zh-TW'
)

See the language codes specified in https://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py. You will see that it uses zh-tw instead of zh-TW.

Finally, the language directories storing the *.po and *.mo files in your locales folder needs to be named zh_CN and zh_TW respectively for the translations to work properly.