i18n_generatorsで楽チン日本語化

ActiveHeartを使おうとぐぐってみたら、2009-02-15 - 大人になったら肺呼吸にて非推奨とのこと。そこからhttp://ukstudio.jp/2008/11/rails_22_i18n/をたどって、http://blog.dio.jp/2008/11/22/japanizing-rails-2-2-by-i18n-generatorsにてRails-2.2のi18n対応のお勉強をしました。

お手本環境はおそらくLinuxですが(sudoやってるから)、こっちはマシン台数&CPUパワーの制約でWindowsです。orz

Cygwinでやればよかったかもとちょっと後悔。orz

【追記】
i18n_generator - Rubyとか Illustratorとか SFとか折紙とかに詳しい解説があります。Ruby On Rails 国際標準化機能 I18n その2にも解説があります。

まず、i18n_generatorsをインストール。

[C:\src]
$ gem so -a http://gems.github.com
http://gems.github.com added to sources

[C:\src]
$ gem i amatsuda-i18n_generators
Successfully installed locale-2.0.4
Successfully installed gettext-2.0.4
Successfully installed amatsuda-i18n_generators-0.6.0
3 gems 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...

普通にプロジェクトを作成して、change directory。

[C:\src]
$ rails test090824d
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      【中略】
      create  log/test.log

[C:\src]
$ cd test090824d

ここで、i18n_generatorsでアプリケーションの雛形を作成。

[C:\src\test090824d]
$ ruby script\generate i18n_scaffold user first_name:string last_name:string birthday:dateblood_type:integer zip_code:string address1:string address2:string
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      【中略】
      create    db/migrate
      create    db/migrate/20090824083020_create_users.rb

SQLiteのDBを作成。

[C:\src\test090824d]
$ rake db:migrate
(in C:/src/test090824d)
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.2180s
==  CreateUsers: migrated (0.2180s) ===========================================

日本語のリソースを生成。勝手に翻訳してくれる!!!*1

[C:\src\test090824d]
$ ruby script\generate i18n ja
       debug  updating environment.rb ...
       debug  fetching ja.yml from rails-i18n repository...
      exists  config/locales
      update  config/environment.rb
      create  config/locales/ja.yml
       debug  1 models found.
       debug  27 translation keys found in views.
       debug  translating activerecord.models.user ...
       debug  translating activerecord.attributes.user.last_name ...
       debug  translating activerecord.attributes.user.first_name ...
       debug  translating activerecord.attributes.user.birthday ...
       debug  translating activerecord.attributes.user.zip_code ...
       debug  translating activerecord.attributes.user.blood_type ...
       debug  translating activerecord.attributes.user.address1 ...
       debug  translating activerecord.attributes.user.address2 ...
       debug  translating railties.scaffold.editing ...
       debug  translating railties.scaffold.show ...
       debug  translating railties.scaffold.back ...
       debug  translating railties.scaffold.new ...
       debug  translating railties.scaffold.listing ...
       debug  translating railties.scaffold.edit ...
failed to translate "user" into "ja" language.
failed to translate "last_name" into "ja" language.
       debug  took 217.953 secs to translate.
      create  config/locales/translation_ja.yml

翻訳されたtranslation_ja.ymlを覗いてみる。全自動だからイマイチな訳も当然含まれるが、ここまでやってくれるのかとメチャクチャ感動!!!

ja: 
  activerecord: 
    models: 
      user: "user"  #g

    attributes: 
      user: 
        first_name: "名前"  #g
        last_name: "last_name"  #g
        birthday: "誕生日"  #g
        blood_type: "血液型"  #g
        zip_code: "郵便番号"  #g
        address1: "アドレス"  #g
        address2: "住所2"  #g

  railties: 
    scaffold: 
      editing: "編集"  #g
      show: "見せる"  #g
      back: "後ろの"  #g
      listing: "一覧"  #g
      new: "新しい"  #g
      edit: "編集"  #g

とりあえず、直す。

ja: 
  activerecord: 
    models: 
      user: "ユーザ"  #g

    attributes: 
      user: 
        first_name: "名"  #g
        last_name: "姓"  #g
        birthday: "誕生日"  #g
        blood_type: "血液型"  #g
        zip_code: "郵便番号"  #g
        address1: "住所1"  #g
        address2: "住所2"  #g

  railties: 
    scaffold: 
      editing: "編集"  #g
      show: "参照"  #g
      back: "戻る"  #g
      listing: "一覧"  #g
      new: "新規"  #g
      edit: "編集"  #g
      create: "登録" #g
      update: "更新" #g
      destroy: "削除" #g

これで、新規(new.html.erb)と編集(edit.html.erb)を除いて完全に日本語化された。new.html.erbとedit.html.erbが不完全なのはなんでだ?
#f.labelの引数にUser.human_attribute_name()が付いてない。

  • new.html.erb
<h1><%= translate(:new, :default => "New {{model}}", :model => User.human_name, :scope => [:railties, :scaffold]) %></h1>

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </p>
  <p>
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </p>
  <p>
    <%= f.label :birthday %><br />
    <%= f.date_select :birthday %>
  </p>
  <p>
    <%= f.label :blood_type %><br />
    <%= f.text_field :blood_type %>
  </p>
  <p>
    <%= f.label :zip_code %><br />
    <%= f.text_field :zip_code %>
  </p>
  <p>
    <%= f.label :address1 %><br />
    <%= f.text_field :address1 %>
  </p>
  <p>
    <%= f.label :address2 %><br />
    <%= f.text_field :address2 %>
  </p>
  <p>
    <%= f.submit translate(:create, :default => "Create", :scope => [:railties, :scaffold]) %>
  </p>
<% end %>

<%= link_to translate(:back, :default => 'Back', :scope => [:railties, :scaffold]), users_path %>
  • edit.html.erb
<h1><% translate(:editing, :default => "Editing {{model}}", :model => User.human_name, :scope => [:railties, :scaffold]) %></h1>

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </p>
  <p>
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </p>
  <p>
    <%= f.label :birthday %><br />
    <%= f.date_select :birthday %>
  </p>
  <p>
    <%= f.label :blood_type %><br />
    <%= f.text_field :blood_type %>
  </p>
  <p>
    <%= f.label :zip_code %><br />
    <%= f.text_field :zip_code %>
  </p>
  <p>
    <%= f.label :address1 %><br />
    <%= f.text_field :address1 %>
  </p>
  <p>
    <%= f.label :address2 %><br />
    <%= f.text_field :address2 %>
  </p>
  <p>
    <%= f.submit translate(:update, :default => "Update", :scope => [:railties, :scaffold]) %>
  </p>
<% end %>

<%= link_to translate(:show, :default => 'Show', :scope => [:railties, :scaffold]), @user %> |
<%= link_to translate(:back, :default => 'Back', :scope => [:railties, :scaffold]), users_path %>


これ以下は間違ってます。こちらを参照願います。m(_ _)m


両方とも直してみる。

  • new.html.erb
<h1><%= translate(:new, :default => "New {{model}}", :model => User.human_name, :scope => [:railties, :scaffold]) %></h1>

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label User.human_attribute_name('first_name') %><br />
    <%= f.text_field :first_name %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('last_name') %><br />
    <%= f.text_field :last_name %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('birthday') %><br />
    <%= f.date_select :birthday %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('blood_type') %><br />
    <%= f.text_field :blood_type %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('zip_code') %><br />
    <%= f.text_field :zip_code %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('address1') %><br />
    <%= f.text_field :address1 %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('address2') %><br />
    <%= f.text_field :address2 %>
  </p>
  <p>
    <%= f.submit translate(:create, :default => "Create", :scope => [:railties, :scaffold]) %>
  </p>
<% end %>

<%= link_to translate(:back, :default => 'Back', :scope => [:railties, :scaffold]), users_path %>
  • edit.html.erb
<h1><% translate(:editing, :default => "Editing {{model}}", :model => User.human_name, :scope => [:railties, :scaffold]) %></h1>

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label User.human_attribute_name('first_name') %><br />
    <%= f.text_field :first_name %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('last_name') %><br />
    <%= f.text_field :last_name %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('birthday') %><br />
    <%= f.date_select :birthday %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('blood_type') %><br />
    <%= f.text_field :blood_type %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('zip_code') %><br />
    <%= f.text_field :zip_code %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('address1') %><br />
    <%= f.text_field :address1 %>
  </p>
  <p>
    <%= f.label User.human_attribute_name('address2') %><br />
    <%= f.text_field :address2 %>
  </p>
  <p>
    <%= f.submit translate(:update, :default => "Update", :scope => [:railties, :scaffold]) %>
  </p>
<% end %>

<%= link_to translate(:show, :default => 'Show', :scope => [:railties, :scaffold]), @user %> |
<%= link_to translate(:back, :default => 'Back', :scope => [:railties, :scaffold]), users_path %>

これでnewとeditのラベルも日本語化された。

最初からこんなソースを吐いてほしいんだけど、どう変更すればいいんだろう?>i18n_generators

*1:うちのネットワーク環境のせいかも知れませんが、オンラインで辞書を引いているみたいで時間が掛かります。