LoginSignup
0
0

More than 1 year has passed since last update.

日本語でエラー文章

Posted at

gemを入れる

GEM
gem 'rails-i18n'
bundle install

config/application.rbに追記

config/application.rb
module Stoma
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2
    config.i18n.default_locale = :ja    ここ追加する
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

3点未入力がありますのエラー部分テンプレート作成

layouts/_errors.html.erb
<% if obj.errors.any? %>
  <div id="error_explanation">
    <h3><%= pluralize(obj.errors.count, "") %> 未入力があります</h3>
    <ul>
      <% obj.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
<% end %>

obj:@エラー文の対象とし、部分テンプレートを呼び出す

html
<%= render 'layouts/errors', obj: @ostomy%>

config/locales/models/ja.ymlで日本語化する

インデントずれると翻訳できないので注意

config/locales/models/ja.yml
ja:
  activerecord:
    errors:
      messages:
        record_invalid: 'バリデーションに失敗しました: %{errors}'
        restrict_dependent_destroy:
          has_one: "%{record}が存在しているので削除できません"
          has_many: "%{record}が存在しているので削除できません"
    attributes:
      ostomy:
        color: ストーマの色
        edema: むくみ
        skin: 皮膚の状態

##########この順序でかく##################################

attributes:         # attributes:の直下
  post:            # モデル名
    title: 名前           #カラム
     content:ブログ本文    #カラム名
############enumsの訳も以下############################
enums:  #enum
    ostomy: #モデル名
      color:  #カラム
        pink: ピンク  #訳
        red: あか    #訳
        brown: ちゃいろ #訳
        black: くろ      #訳
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