LoginSignup
0
0

More than 3 years have passed since last update.

【超初心者的】Rails 多言語化対応

Last updated at Posted at 2020-08-10

ここはまだ経験が浅いので備忘録的な記事になっています。理解が深まるに伴って加筆していきます。

##設定

27行目あたり。タイムゾーン設定の下に追加

config/application.rb
config.i18n.default_locale = :ja

##gem

末尾に追記

config/Gemfile
gem 'rails-i18n', '~> 6'

##インストール

ターミナル
% bundle install

##翻訳ファイルの作成

config/locales/ja.yml
ja:
  activerecord:
    models:
      menu: メニュー
    attributes:
      menu:
        name: 商品名
        price: 値段
        description: 説明

Railsサーバ再起動で読み込まれる

##使用例

自動的にja.ymlに記載した日本語で表示される

formの場合

<%= form.label :name %>
<%= form.label :price %>
<%= form.label :description %>

tableの場合

<th><%= Menu.human_attribute_name(:name) %></th>
<th><%= Menu.human_attribute_name(:price) %></th>
<th><%= Menu.human_attribute_name(:description) %></th>
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