Japanese support for Rails

I’ve got a database table (mysql 5.0) that contains Japanese text. The
table is set to sjis. I’ve ran the few scripts to get the basic app to
interact with the database for rails. However, all Japanese text comes
out as question marks ‘?’. English text comes out fine. I’ve tried all
the encoding on the web browser with no luck. I’ve even recreated the
table with utf8 as the default with no luck either. Using Webrick.

I’ve gone through the forum and apparently Rails’ support of nonACII
characters is sub par. But there’s got to be some way to get some
Japanese through? I’ve seen a clip where it works on a Japanese
computer.

-Anthony

You need to set the KCODE global
eg $KCODE = ‘u’ for utf-8, $KCODE = ‘s’ for sjis

We do this in environment.rb

You may also want to require ‘jcode’, this enhances some of the methods
in String so that they work properly with multibyte charsets.

Fred

Frederick C. wrote:

You need to set the KCODE global
eg $KCODE = ‘u’ for utf-8, $KCODE = ‘s’ for sjis

We do this in environment.rb

You may also want to require ‘jcode’, this enhances some of the methods
in String so that they work properly with multibyte charsets.

Fred

I tried adding $KCODE = ‘u’, but no luck. I tried $KCODE = ‘s’ as well.
Lastly I tried it with require ‘jocde’ added as well, but no go.
Hmmm…

-Anthony

Frederick C. wrote:

Oh, and also make sure that the browser is correctly determing the
content-encoding

Fred

Yeah, I tried all of the encoding settings in FF and IE with no luck.
I’ve killed WEBrick and started over everytime I modified
environment.rb.

-Anthony

Oh, and also make sure that the browser is correctly determing the
content-encoding

Fred

Anthony W. wrote:

Frederick C. wrote:

Oh, and also make sure that the browser is correctly determing the
content-encoding

Fred

Yeah, I tried all of the encoding settings in FF and IE with no luck.
I’ve killed WEBrick and started over everytime I modified
environment.rb.

-Anthony

I got it! I had to add

encoding: utf8

in each of the database stanzas to database.yml.

-Anthony

Between the various responses, people have got it right. The minimum
you need is:

  1. Your database/table to the right character set
  2. Your database connection to the right character set, by putting an
    “encoding: …” line in database.yml
  3. Make sure Rails is telling the browser the right character set, as
    per http://nubyonrails.com/articles/2006/02/01/rjs-and-content-type-
    header

I recommend you use utf8. Rails 1.2 will have a lot more tools for
handling utf8, and Rails already tells your browser to send utf8 by
default, so you can skip step 3. :slight_smile:

Cheers,

Pete Y.
http://aliencamel.com/

Hi,

Did you set “encoding:” to database.yml ?

(e.g.)
development:
adapter: mysql
database: depot_development
socket: /var/lib/mysql/mysql.sock
username: foo
password: bar
encoding: cp932

cp932 is a subset of shift jis on Windows.
“utf8” may be better with Rails.

And also you need to set Content-Type correctly.
http://nubyonrails.com/articles/2006/02/01/rjs-and-content-type-header

On Wed, 1 Nov 2006 17:18:31 +0100