ruby/jruby script/console character encoding

There are many nagging ways in which discover this problem.

1. My database is utf8-uncode-ci encoding/collation but I still see french/chinese characters as ? (question marks).

2. The encoding is utf-8 in my database.yml

3. I believed rails does utf-8 encoding by default.

4. I can’t print the foreign characters in my script/console

5. The browser is sending the characters fine. Where the hell the characters got converted to question marks.


The steps to see this encoding issue in action ..

$LANG=en_US.iso88591 jruby script/console

>> puts ‘登录个人中心’


And the solution

$LANG=en_US.utf8 jruby script/console


Now that you know the solution… here is more information.

I found some old Redhat, Cent OS linux versions have the system character map set to US ANSI which is ISO-8859-1

You can see the linux system encoding by typing

$locale charmap

The summary is, the rails (2.3.18/jruby 1.7) picks up the system character encoding by default. Fix the system encoding (set it to UTF-8) and no ?s anywhere. Happy French and happy Chinese.

PS: Btw, I don’t know what those chinese characters mean.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s