i18nによる国際化
i18nによる国際化
Gemfileに以下を追記
gem 'rails-i18n'
※[rails-i18n]Gemは、基本の翻訳情報ファイルが最初から入っているもので、[config/locales/ja.yml]などに[rails-i18n]Gemの翻訳ファイルを記載する必要はない。
↓
bundle install
↓
[config/application.rb]に以下を記載。
donfig.i18n.default_locale = :ja #デフォルトでの言語を指定(指定しないとEnglishの翻訳ファイルが読み込まれる) config.i18n.load_path += Dir[Rails.root.join('config/locales/**/*.{rb,yml}').to_s] #複数のロケールファイルを読み込むように設定(参照するファイルのパスを設定している。)
↓
[config/locales]以下にデフォルト言語で設定した名前のyamlファイル(ja.yml)を作成。(翻訳ファイルの作成)
ex)
モデルに関する翻訳ファイル ja: activerecord: models: user: ユーザー #モデル名 attributes: user: #モデル名 last_name: 姓 #モデルの属性名 first_name: 名 email: メールアドレス password: パスワード password_confirmation: パスワード確認
上記のようにモデル名をactiverecord以下に記述することで、[human_attribute_name]や[Model.model_name.human]が使用できる。
ビューに関する翻訳ファイル ja: defaults: login: 'ログイン' logout: 'ログアウト' registration: '登録' user_sessions: #コントローラ名 new: #アクション名 forgot_password: 'パスワードをお忘れの方はこちら' title: 'ログイン' to_registration_page: '登録ページへ' users: new: title: ユーザー登録 to_login_page: 'ログインページへ'
[補足] ※form_withでモデルを渡してる場合は、railsが自動で対応の翻訳ファイルがあれば読み込んできて国際化してくれる。 ex)
<%= form_with model: @user, local: true do |f| %> <div class="form-group"> <%= f.label :last_name %> <%= f.text_field :last_name, class: 'form-control' %>
下記ファイルを自動で読んでくれる
ja: activerecord: models: user: ユーザー attributes: user: last_name: 姓 first_name: 名 email: メールアドレス password: パスワード password_confirmation: パスワード確認
form_withでモデルを渡していない場合は、自分で国際化する必要がある。(国際化の際にはhuman_attribute_nameやModel.model_name.humanを使用) ex)
<%= form_with scope: :session, url: login_path, local: true do |f| %> <div class="form-group"> <%= f.label :email, User.human_attribute_name(:email) %> <%= f.text_field :email, class: 'form-control' %>
国際化を記載する時は、lazy lookup記法を使う。(ビューだけでなくコントローラでも使えるため。)
・[ja.yml]ファイルのdefaultsを使う場合(t 'defaults.○○')の形にする ex)
<%= link_to (t 'defaults.logout'), logout_path, method: :delete, class: 'dropdown-item' %> <h1><%= t 'defaults.login' %></h1>
・[ja.yml]ファイルのdefaultsを使わない場合(t '.○○')の形にする ex)
<%= link_to (t '.to_registration_page'), new_user_path %> <a href="#"><%= t '.forgot_password' %></a>
config.i18n.load_pathの記述の意味
[config.i18n.load_path]の設定の記述は、以下のように実行されます。
irb(main):001:0> Rails.root => #<Pathname:/Users/higmonta/workspace/fishing_cooking> irb(main):002:0> Rails.root.join('config') => #<Pathname:/Users/higmonta/workspace/fishing_cooking/config> irb(main):003:0> Rails.root.join('config', 'locales') => #<Pathname:/Users/higmonta/workspace/fishing_cooking/config/locales> irb(main):004:0> Rails.root.join('config', 'locales', '**') => #<Pathname:/Users/higmonta/workspace/fishing_cooking/config/locales/**> irb(main):005:0> Rails.root.join('config', 'locales', '**', '*.{rb,yml}') => #<Pathname:/Users/higmonta/workspace/fishing_cooking/config/locales/**/*.{rb,yml}> irb(main):006:0> Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] => ["/Users/higmonta/workspace/fishing_cooking/config/locales/en.yml", "/Users/higmonta/workspace/fishing_cooking/config/locales/ja.yml"]
[/]という記述は、ワイルドカード */ の0回以上の繰り返しを意味し、ディレクトリを再帰的にたどってマッチを行います。
例えば, foo//bar は foo/bar, foo//bar, foo//*/bar ... (以下無限に続く)に対してそれぞれマッチ判定を行います。
※注意
appファイル以下のコードやconfig/routes.rbなどのファイルは再起動しなくても反映されるが、他のファイルは基本的にはrails sでサーバーを起動し直す必要がある。
[ja.ymlファイル]にコントローラやアクション毎に階層的に記載しているのは、見やすくする為に整理して書いているだけなので、基本的にどこに翻訳データを記載しても、翻訳される。
参考記事
あなたはいくつ知っている?Rails I18nの便利機能大全! - Qiita
[初学者]Railsのi18nによる日本語化対応 - Qiita