0
0

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 3 years have passed since last update.

[Rails]rails-i18nを用いた日本語化

Posted at

はじめに

rails-i18nというgemを用いることでエラーメッセージなどを日本語化することができます。

目次

  1. 言語を日本語に設定する
  2. rails-i18nのインストール
  3. 日本語に設定するためのファイルを作成

1. 言語を日本語に設定する

application.rbを編集します。

:jaとすることで日本語に設定することができます。

config/application.rb

module CosmeticReview
  class Application < Rails::Application
    config.load_defaults 6.0
    #以下の記述を追記します
    config.i18n.default_locale = :ja
  end
end

2. rails-i18nのインストール

gemfileに以下を追記します。
その後、コマンドでbundle installを実行します。

gemfile
gem 'rails-i18n'

3. 日本語に設定するためのファイルを作成

localファイルを作成し、その中に日本語へ翻訳するためのYAMLファイルを作成します。
今回はモデルのエラーメッセージを日本語化しました。

locales/ja.yml
ja:
 activerecord:
   attributes:
     user:
       nickname: ニックネーム
       gender: 性別
       introduce: 自己紹介
       birthday: 生年月日 
     post:
       name: 商品名
       description: 商品の説明
       image: 画像
       shop_name: 購入店
       evaluation: 商品の評価
       price: 購入価格
       category_id: カテゴリー
   time:
     formats:
       default: "%Y/%m/%d %H:%M:%S"

階層が少しでもずれているとエラーが出るので気をつけましょう。

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?