[Ruby on Rails 3, Ruby on Rails 4] gems: i18n
プロジェクトの国際化(多言語化)対応をするためのプラグイン。
[markdown]
> * [Ruby – Railsで日本語化対応にする方法 – Qiita [キータ]](http://qiita.com/kusu_tweet/items/b534c808ac1ee0382f05)
> * [Railsの多言語化対応 i18nのやり方を整理してみた!【国際化/英語化】 – 酒と泪とRubyとRailsと](http://morizyun.github.io/blog/i18n-english-rails-ruby-many-languages/)
## インストール
Rails をインストールすると一緒に入っています。
“`
% cd ~/project_foo
% bundle list
Gems included by the bundle:
* i18n (0.6.5)
“`
## default_locale を設定する
config/application.rb
“`config/application.rb
# config.i18n.default_locale = :de
config.i18n.default_locale = :ja
“`
## 辞書ファイルをダウンロードする
config/locale/ に、雛形として使う辞書ファイルをダウンロードする。
“`
% wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/en.yml -P config/locales/
% wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml -P config/locales/
“`
## 必要な翻訳を追記する
例えば、default でこんな記述がされている。
config/locales/en.yml
“`config/locales/en.yml
en:
hello: “Hello world”
“`
View 内でこのように書ける。
app/views/store/index.html.erb
“`app/views/store/index.html.erb
<%= t :hello %>
“`
“`prettyprinted
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t ‘hello’
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# ‘true’: ‘foo’
#
# To learn more, please read the Rails Internationalization guide
# available at http://guides.rubyonrails.org/i18n.html.
“`
## サーバを再起動する
“`
% rails s
“`
## 補遺
辞書ファイルを分割して管理したり、ローケルを振り分けたりといろいろある。
> * [Railsの多言語化対応 i18nのやり方を整理してみた!【国際化/英語化】 – 酒と泪とRubyとRailsと](http://morizyun.github.io/blog/i18n-english-rails-ruby-many-languages/)
> * [Rails 3 の I18n について – おもしろWEBサービス開発日記](http://d.hatena.ne.jp/willnet/20100430/1272618929)
> * [RailsのI18n APIの使い方の基本と辞書ファイルの整理方針: Modelごと、Viewごとに分けて管理する – memo.yomukaku.net](http://memo.yomukaku.net/entries/LXvSUpT)
> * [Rails で JavaScript を国際化する – present](http://tnakamura.hatenablog.com/entry/2013/03/16/201338)
> * [Rails3 事始め: [Rails3] 国際化 I18n のまとめ(その1:準備編)](http://rails3try.blogspot.jp/2011/12/rails3-i18n_27.html)
> * [Rails3 事始め: [Rails3] 国際化 I18n のまとめ(その2:ロケールの切り替え)](http://rails3try.blogspot.jp/2011/12/rails3-i18n.html)
> * [Rails3 事始め: [Rails3] 国際化 I18n のまとめ(その3:辞書ファイルの使い方)](http://rails3try.blogspot.jp/2012/01/rails3-i18n.html)
> * [Rails3のi18n機能および日本語化 – へびにっき](http://tkyk.name/blog/2011/06/22/Rails-Ruby-rails3_i18n/)
[/markdown]