# Japanese support for Rails

**URL:** <https://www.ruby-forum.com/t/japanese-support-for-rails/74196>\
**Category:** Rails\
**Created:** [November 1, 2006, 11:33am UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196 "2006-11-01T11:33:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 1, 2006, 11:33am UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/1 "2006-11-01T11:33:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 1, 2006, 12:59pm UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/2 "2006-11-01T12:59:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 1, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/3 "2006-11-01T14:41:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 1, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/4 "2006-11-01T17:18:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 1, 2006, 3:25pm UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/5 "2006-11-01T15:25:50Z")

</div>

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

Fred

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 2, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/6 "2006-11-02T17:27:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/7 "2006-11-07T10:53:37Z")

</div>

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-](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. 🙂

Cheers,

Pete Y.  
[http://aliencamel.com/](http://aliencamel.com/)

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [November 7, 2006, 10:53am UTC](https://www.ruby-forum.com/t/japanese-support-for-rails/74196/8 "2006-11-07T10:53:26Z")

</div>

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](http://nubyonrails.com/articles/2006/02/01/rjs-and-content-type-header)

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