i18nによる日本語化

Rails2.2から様々な言語に対応できるように、国際化i18n(internationalizationの頭と後ろのiとn、中の文字数が18文字)が標準で搭載されました。
この機能によりラベル名やエラーメッセージなどの日本語化が簡単にできるようになりました。
なお、提供されたプログラムを特定の言語に対応させることを地域化L10N(localization)といい、Railsでもいままで様々な地域化の試みがなされていました。
引き続いてプロジェクトproj001を使って日本語化を説明します。

1.amatsuda-i18nのインストール

コマンド プロンプト

D:\Rails_Project\proj001>gem so -a http://gems.github.com
http://gems.gethub.com added to sources


D:\Rails_Project\proj001>gem i amatsuda-i18n_generators
Successfully installed locale-2.0.4
Successfully installed getext-2.0.4
Successfully installed amatsuda-i18n_generators-0.6.0
3 gem installed
Installing ri documentation for locale-2.0.4...
Installing ri documentation for gettext-2.0.4...
Installing RDoc documentation for locale-2.0.4...
Installing RDoc documentation for gettext-2.0.4...


2.i18nによる生成
  config/localesフォルダにja.ymlとtranslation_ja.ymlという日本語リソースファイルを生成します。

コマンド プロンプト

D:\Rails_Project\proj001>ruby script/generate i18n ja
debug updating environment.rb ...
debug fetching ja.yml fro rails-i18n repository...
exists config/locales
update config/environment.rb
create config/locales/ja.yml
debug 1 models found.
debug 0 translating activerecord.models.address ...
debug translating activerecord.attributes.address.namae ...
debug translating activerecord.attributes.address.yubin ...
debug translating activerecord.attributes.address.jusho ...
debug translating activerecord.attributes.address.denwa ...
debug took 1.172 secs to translate.
create config/locales/translation_ja.yml

3.config/locales/ja.ymlの内容確認

/config/locales/ja.yml

# Japanese translations for Ruby on Rails
# by Akira Matsuda (ronnie@dio.jp)
# AR error messages are basically taken from Ruby-GetText-Package. Thanks to Masao Mutoh.

ja:
date:
formats:
default: "%Y/%m/%d"
short: "%m/%d"
long: "%Y年%m月%d日(%a)"

day_names: [日曜日, 月曜日, 火曜日, 水曜日, 木曜日, 金曜日, 土曜日]
abbr_day_names: [日, 月, 火, 水, 木, 金, 土]

month_names: [~, 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月]
abbr_month_names: [~, 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月]

order: [:year, :month, :day]

time:
formats:
default: "%Y/%m/%d %H:%M:%S"
short: "%y/%m/%d %H:%M"
long: "%Y年%m月%d日(%a) %H時%M分%S秒 %Z"
am: "午前"
pm: "午後"

support:
array:
sentence_connector: "と"
skip_last_comma: true
words_connector: "と"
two_words_connector: "と"
last_word_connector: "と"

number:
format:
separator: "."
delimiter: ","
precision: 3

currency:
format:
format: "%n%u"
unit: "円"
separator: "."
delimiter: ","
precision: 0

percentage:
format:
delimiter: ""

precision:
format:
delimiter: ""

human:
format:
delimiter: ""
precision: 1
storage_units:
format: "%n%u"
units:
byte: "バイト"
kb: "キロバイト"
mb: "メガバイト"
gb: "ギガバイト"
tb: "テラバイト"

datetime:
distance_in_words:
half_a_minute: "30秒前後"
less_than_x_seconds:
one: "1秒以内"
other: "{{count}}秒以内"
x_seconds:
one: "1秒"
other: "{{count}}秒"
less_than_x_minutes:
one: "1分以内"
other: "{{count}}分以内"
x_minutes:
one: "1分"
other: "{{count}}分"
about_x_hours:
one: "約1時間"
other: "約{{count}}時間"
x_days:
one: "1日"
other: "{{count}}日"
about_x_months:
one: "約1ヶ月"
other: "約{{count}}ヶ月"
x_months:
one: "1ヶ月"
other: "{{count}}ヶ月"
about_x_years:
one: "約{{count}}年"
other: "約{{count}}年"

over_x_years:
one: "{{count}}年以上"
other: "{{count}}年以上"

activerecord:
errors:
format:
separator: ""
template:
header:
one: "{{model}}にエラーが発生しました。"
other: "{{model}}に{{count}}つのエラーが発生しました。"
body: "次の項目を確認してください。"

messages:
inclusion: "は一覧にありません。"
exclusion: "は予約されています。"
invalid: "は不正な値です。"
confirmation: "が一致しません。"
accepted: "を受諾してください。"
empty: "を入力してください。"
blank: "を入力してください。"
too_long: "は{{count}}文字以内で入力してください。"
too_short: "は{{count}}文字以上で入力してください。"
wrong_length: "は{{count}}文字で入力してください。"
taken: "はすでに存在します。"
not_a_number: "は数値で入力してください。"
greater_than: "は{{count}}より大きい値にしてください。"
greater_than_or_equal_to: "は{{count}}以上の値にしてください。"
equal_to: "は{{count}}にしてください。"
less_than: "は{{count}}より小さい値にしてください。"
less_than_or_equal_to: "は{{count}}以下の値にしてください。"
odd: "は奇数にしてください。"
even: "は偶数にしてください。"


4.動作確認
  WEBrickを再立ち上げして実行します。
  エラーメッセージが日本語化されました。

    

5.メッセージ内の項目名の日本語化
  メッセージは日本語化されたが、メッセージの中のテーブル名や項目名はひらがなや英字のままです。
  /config/locales/translation_ja.ymlを変更することで対応します。

変更前 /config/locales/translation_ja.yml

ja:
activerecord:
models:
address: "住所" #g

attributes:
address:
namae: "なまえ" #g
yubin: "ゆうびん" #g
jusho: "Jusho" #g
denwa: "でんわ" #g


変更後 /config/locales/translation_ja.yml

ja:
activerecord:
models:
address: "住所録"

attributes:
address:
namae: "名前"
yubin: "郵便番号"
jusho: "住所"
denwa: "電話"


6.動作確認
  エラーメッセージ内の項目名が日本語化されました。
    

7.ラベル部分の日本語化
(1) translation_ja.ymlの更新
  タイトルやラベルの部分は、まだ英字のままです。
  これに対応するには/config/locales/translation_ja.ymlに追加します。

/config/locales/translation_ja.yml

ja:
activerecord:
models:
address: 住所録
attributes:
address:
namae: 名前
yubin: 郵便
jusho: 住所
denwa: 電話

label:
Editing_address: 住所録の更新
Listing_addresses: 住所録一覧
New_address: 住所録への新規登録
Namae: 名前
Yubin: 郵便
Jusho: 住所
Denwa: 電話
Are_you_sure?: 削除してよろしいですか?
link:
New_address: 追加
Show: 表示
Edit: 編集
Destroy: 削除
Back: 前画面
button:
Create: 新規登録実行
Update: 更新実行


(2) ビュープログラムの修正
  次の各プログラムを修正します。
  /app/viws/addresses/edit.html.erb
  /app/viws/addresses/index.html.erb
  /app/viws/addresses/new.html.erb
  /app/viws/addresses/show.html.erb
  たとえば、該当箇所を<%=t(label.Yubin)%>と記述すると"郵便"という表示になります。

/app/views/addresses/index.html.erb

<br/>
<h1><%= t('label.Listing_addresses') %></h1>


<table>
 <tr>
  <th><%= t('label.Namae') %></th>
  <th><%= t('label.Yubin') %></th>
  <th><%= t('label.Jusho') %></th>
  <th><%= t('label.Denwa') %></th>
 </tr>


<% for address in @addresses %>
 <tr>
  <td><%=h address.namae %></td>
  <td><%=h address.yubin %></td>
  <td><%=h address.jusho %></td>
  <td><%=h address.denwa %></td>
  <td><%= link_to t('link.Show'), address %></td>
  <td><%= link_to t('link.Edit'), edit_address_path(address) %></td>
  <td><%= link_to t('link.Destroy'), address,
:confirm => t('label.Are_you_sure?'), :method => :delete %></td>
 </tr>
<% end %>
</table>


<div class="pagination_link">
 <%= will_paginate @addresses %>
</div>


<br />


<%= link_to t('link.New_address'), new_address_path %>

(3) 動作確認
(indexの画面)
    

(Newの画面)
    

8.Paginateの日本語化
(1) index.html.erbの修正
  indexの画面を見るとPaginateの部分だけが日本語化されていません。
  これを日本語化するにはwill-paginateの指定部分でオプションを付けることで解決します。

/app/views/addresses/index.html.erb

<br/>
<h1><%= t('label.Listing_addresses') %></h1>


<table>
 <tr>
  <th><%= t('label.Namae') %></th>
  <th><%= t('label.Yubin') %></th>
  <th><%= t('label.Jusho') %></th>
  <th><%= t('label.Denwa') %></th>
 </tr>


<% for address in @addresses %>
 <tr>
  <td><%=h address.namae %></td>
  <td><%=h address.yubin %></td>
  <td><%=h address.jusho %></td>
  <td><%=h address.denwa %></td>
  <td><%= link_to t('link.Show'), address %></td>
  <td><%= link_to t('link.Edit'), edit_address_path(address) %></td>
  <td><%= link_to t('link.Destroy'), address,
:confirm => t('label.Are_you_sure?'), :method => :delete %></td>
 </tr>
<% end %>
</table>


<div class="pagination_link">
 <%= will_paginate @addresses, :prev_label=>"&#171; 前", :next_label=>"次 &#187;" %>
</div>


<br />


<%= link_to t('link.New_address'), new_address_path %>

(2) 動作確認
paginateの部分が日本語化されました。