11
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでf.labelを日本語化する (i18n)

Last updated at Posted at 2019-05-04
new.slim
= f.label :first_name
= f.text_field :first_name

= f.label :family_name
= f.text_field :family_name

デフォルトではフォームにFirstName FamilyNameと表示されてしまいます。
これらを日本語化したいときは以下の2ステップ。

1.config/application.rbに設定

以下のように変更&追記

config/application.rb
config.i18n.default_locale = :ja #jaに変更
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]

2.config/locales以下にmodels/ja.ymlを追加

config/locales以下にmodelsファイルを作成し、その中に以下のようなja.ymlファイルを配置

config/locales/models/ja.yml
ja:
  activerecord:
    models:
      user: ユーザー
      admin: 管理者
    attributes:
      user:
        id: ID
        first_name: 
        family_name: 
      admin:
        id: ID

これで無事、日本語表示されます。

11
13
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
11
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?