GetTextで実際に日本語化作業をやってみる。

GetTextを利用する処理が、RadRails内で快適に処理できるようになったので、実際に翻訳作業をやってみた。コントローラー、モデル、ビューすべての表示に関する文言を_()で囲ってみる。

1)コントローラー:app/controllers/softwares_controller.rbでの、_()による囲み作業例

  def create
    @software = Software.new(params[:software])
    if @software.save
      flash[:notice] = 'Software was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

セオリー通りに、メッセージの文言を_()で囲んだ。

      flash[:notice] = _('Software was successfully created.')

3)で.poファイルが更新された時、こんな感じで翻訳した。

#: app/controllers/softwares_controller.rb:52
msgid "Software was successfully created."
msgstr "ソフトウェアが正常に作成されました。"

2)ビュー:app/views/softwares/での、_()による囲み作業例

ビューの中では単純に出来ないことが多い。

式展開#{}を含む文字列の場合(list.rhtml)
<%= link_to _('Destroy'),
            { :action => 'destroy', :id => software },
            :confirm => "#{software.title} is deleted. Are you sure?",
            :post => true %>

以下のように、フォーマット文字列として処理する必要がある。

<%= link_to _('Destroy'),
            { :action => 'destroy', :id => software },
            :confirm => _("%{software_title} is deleted. Are you sure?") % {:software_title => software.title},
            :post => true %>

3)で.poファイルが抽出された時、こんな感じで翻訳した。

#: app/views/softwares/list.rhtml:39
msgid "%{software_title} is deleted. Are you sure?"
msgstr "タイトル「%{software_title}」を削除します。 よろしいですか?"

webブラウザで確認すると、このように表示される。

単なる文字列の場合(_form.rhtml)
<p><label for="software_title">Title</label><br/>
<%= text_field :software, :title, :size => 40  %></p>

.rhtmlの中では、_()はRubyメソッドなので、<%= _('文字列') %>のように囲む必要がある。

<p><label for="software_title"><%= _('Title') %></label><br/>
<%= text_field :software, :title, :size => 40  %></p>
ifを使っている...。(list.rhtml)
<%= link_to "#{@direction_mark if @order_field=='keyword_id'}Keyword", 
            :action => 'list_in_order', 
            :keyword_id => @keyword_id, 
            :order_field => 'keyword_id', 
            :order_direction => @order_next_direction %>

1行コードにこだわり過ぎると、このように困ることになる。2行に分けて書き直した。

<%= @direction_mark if @order_field=='keyword_id' %>
<%= link_to _('Keyword'), 
            :action => 'list_in_order', 
            :keyword_id => @keyword_id, 
            :order_field => 'keyword_id', 
            :order_direction => @order_next_direction %>

3).poファイルの更新

  • RadRailsの「Rakeタスク」タブでupdatepoを実行した。
  • po/ja/softwarebook.poに、先ほど_()で囲った文言が追加されている。今まで翻訳した文言はそのまま残っている。以下のように翻訳した。
#: app/controllers/softwares_controller.rb:52
msgid "Software was successfully created."
msgstr "ソフトウェアが正常に作成されました。"

#: app/controllers/softwares_controller.rb:68
msgid "Software was successfully updated."
msgstr "ソフトウェアが正常に更新されました。"

#: app/models/keyword.rb:-
msgid "keyword"
msgstr "キーワード"

#: app/models/keyword.rb:-
msgid "Keyword|Name"
msgstr "キーワード"

#: app/models/software.rb:-
msgid "software"
msgstr "ソフトウェア"

#: app/models/software.rb:-
msgid "Software|Title"
msgstr "タイトル"

#: app/models/software.rb:-
msgid "Software|Description"
msgstr "概要"

#: app/models/software.rb:-
msgid "Software|Url"
msgstr "URL"

#: app/models/software.rb:-
msgid "Software|Created on"
msgstr "作成日時"

#: app/models/software.rb:-
msgid "Software|Updated on"
msgstr "修正日時"

#: app/models/software.rb:-
msgid "Software|Keyword"
msgstr "キーワード"

#: app/models/software.rb:-
msgid "Software|User"
msgstr ""

#: app/views/softwares/_form.rhtml:4
msgid "Title"
msgstr "タイトル"

#: app/views/softwares/_form.rhtml:8
msgid "Description"
msgstr "概要"

#: app/views/softwares/_form.rhtml:11
msgid "Url"
msgstr "URL"

#: app/views/softwares/_form.rhtml:14 app/views/softwares/list.rhtml:14
msgid "Keyword"
msgstr "キーワード"

#: app/views/softwares/edit.rhtml:1
msgid "Editing software"
msgstr "ソフトウェアの編集"

#: app/views/softwares/edit.rhtml:5 app/views/softwares/list.rhtml:36
#: app/views/softwares/show.rhtml:17
msgid "Edit"
msgstr "編集"

#: app/views/softwares/edit.rhtml:8 app/views/softwares/list.rhtml:35
msgid "Show"
msgstr "詳細"

#: app/views/softwares/edit.rhtml:9 app/views/softwares/list.rhtml:52
#: app/views/softwares/show.rhtml:18
msgid "Back"
msgstr "戻る"

#: app/views/softwares/list.rhtml:1
msgid "Listing softwares"
msgstr "ソフトウェアの一覧"

#: app/views/softwares/list.rhtml:37
msgid "Destroy"
msgstr "削除"

#: app/views/softwares/list.rhtml:39
msgid "%{software_title} is deleted. Are you sure?"
msgstr "タイトル「%{software_title}」を削除します。 よろしいですか?"

#: app/views/softwares/list.rhtml:45
msgid "Previous page"
msgstr "前ページ"

#: app/views/softwares/list.rhtml:46
msgid "Next page"
msgstr "次ページ"

#: app/views/softwares/list.rhtml:50 app/views/softwares/new.rhtml:1
msgid "New software"
msgstr "新規作成"

4).moファイルの更新

  • RadRailsの「Rakeタスク」タブでmakemoを実行した。

5)webブラウザで確認

  • 以下のように日本語化された。