Table of Contents
Hi all, I’m from China and have recently been studying TurboGears. I hope this will help people from countries where languages other than English are spoken.
If you want to use strings with foreign characters (non-ASCII) in your Python source file (e.g. error messages etc.), you have to add an encoding declaration #coding=utf-8 or # -*- coding: utf-8 -*- at the top of your Python source file. Let’s take controller.py for example:
+ #coding=utf-8 from turbogears import controllers, expose ...
You should also encode all Kid template files in UTF-8.
If a column might contain non UTF-8 characters, define it like this in model.py:
This is not necessary with SQLite.
If you want to use standard widgets but you find it doesn’t work with Chinese, Then you should:
- Add an encoding declaration at the top of your Python source file: #coding=utf-8
- Convert your Python source files to UTF-8.
- Add a u before any string that contains a Chinese character.
Try the 20 minutes wiki tutorial, run it and type something like this in your browser’s address bar.:
Press Enter. As the page does not exist, you will reach the Edit page. Input some text and then save.
Oops! Something wrong happened, but if you check the database you will see a new page has been added. In a serious application, watch out for such things.
If you are using MySQL on Windows, use MySQL-python.exe-1.2.2b1.win32-py2.4.exe or newer, or you will encounter big problems when you want to execute some SQL containing Unicode.
If you are using MySQL, change your dburi to this form:
The charset parameter tells MySQLdb what charset MySQL should use. Note that the sqlobject_encoding paramter is deprecated and should not be used any more.
Remarks on Tip 6:
When you’re using MySQL, you may need to use byXXX functions like this:
user = User.byName(u'UserName'.encode('gb2312'))
Unicode strings are not accepted. That’s why you need to include SQLObject in your dburi.
That’s definitely a bug of SQLObject, but it has been fixed in later versions.