LoginSignup
0
0

More than 1 year has passed since last update.

rails-i18n による日本語化

Last updated at Posted at 2021-10-07

基本的な流れ(自分用に)

gem 'rails-i18n'をインストールする

Gemfile
gem 'rails-i18n'

Gemfileに「rails-i18n」を導入する。

ターミナル
bundle install

「rails-i18n」をインストール

デフォルトの言語を日本語に設定する、i18nの複数のlocalesファイルが読み込まれるようにする

config/application.rb

module RunteqNormal
  class Application < Rails::Application

    config.load_defaults 5.2
       config.generators.system_tests = nil

    config.generators do |g| 
      g.skip_routes true 
      g.assets false 
      g.helper false 
      g.test_framework false 
    end
     #ここから追記
     #↓でデフォルトの言語を日本語に設定する。
    config.i18n.default_locale = :ja
     #↓でi18nの複数のlocalesファイルが読み込まれるようになる
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
    #ここまで
  end
end

views/model用のlocalesファイルをconfig/locales下に作成する

画面表(HTML)を担当するViewとDB関連の処理を担当するModelでは、ファイルを分けておく必要がある。

今回は、View用の「config/locales/view/ja.yml」とModel用の「config/locales/activerecorder/ja.yml」を作成。

それぞれのja.ymlファイルに日本語の設定をする

config/locales/views/ja.yml
ja:
  defaults:
    login: 'ログイン'
    register: '登録'
    logout: 'ログアウト'
  users:  # controller/usersに対応
    new:  # newアクションに対応
      title: 'ユーザー登録'
      to_login_page: 'ログインページへ'
  user_sessions:
    new:
      title: 'ログイン'
      to_register_page: '登録ページへ'
      password_forget: 'パスワードをお忘れの方はこちら'
  boards:
    index:
      title: '掲示板一覧'
    new:
      title: '掲示板作成'
    bookmarks:
      title: 'ブックマーク一覧'
  profiles:
    show:
      title: 'プロフィール'
config/locales/activerecorder/ja.yml
# モデルは全て activerecord 下に記述することで、「User.model_name.human」、「User.human_attribute_name({attr_name})」で使用可能になる。
ja:
  activerecord:
    models:
      user: 'ユーザー'
      board: '掲示板'
    attributes:
      user:
        email: 'メールアドレス'
        password: 'パスワード'
        password_confirmation: 'パスワード確認'
        last_name: '姓'
        first_name: '名'

基本的な使い方

Viewの場合

app/views/users/new.html.erb
<%= t 'users.new.title' %>

→「config/locales/views/ja.yml」に、「users: new: title: ’ユーザー登録’」と記述してあるので「ユーザー登録」と表示される
・対応するビューの中ではツリーを省略できるので、「users.new」の部分は省略して書くことができる。

app/views/users/inew.html.erb
#ja.ymlファイルの
#     users:
#          new:
#           の部分を自動で読み込んでいる状態
<%= t ‘.title’ %>

→同じく「ユーザー登録と表示される」
・「users/index.html.erb」に記述されているので、「ja.yml」ファイルの「users: new:」の部分を自動的に読み込んでいる。

Modelの場合

app/views/users/inew.html.erb
<%= t('activerecord.models.user') %>

→「onfig/locales/activerecorder/ja.yml」に「activerecord: models: user: ‘ユーザー’」と記述してあるので「ユーザー」と表示される

app/views/users/inew.html.erb
<%= t('activerecord.attributes.user.email') %> 

→「メールアドレス」と表示される

「User.model_name.human」、「User.human_attribute_name({attr_name})」を使う

app/views/users/inew.html.erb
#<%= t('activerecord.models.user') %> を書き換える
<li><%= User.model_name.human %></li>

→「ユーザー」と表示される

app/views/users/inew.html.erb
#<%= t('activerecord.attributes.user.email') %> を書き換える
<li><%= User.human_attribute_name(:email) %></li>

→「メールアドレス」と表示される

実際の記述例

app/views/users/inew.html.erb
<%= form_with model: @user, local: true do |f| %>

  <div class="form-group">
          <%= f.label :email, User.human_attribute_name(:email) %>
          <%= f.text_field :email, class: 'form-control' %>
        </div>
        <div class="form-group">
          <%= f.label :password, User.human_attribute_name(:password) %>
          <%= f.password_field :password, class: 'form-control' %>
        </div>

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0