↓↓クリックして頂けると励みになります。
【09 | バリデーション(検証)】 << 【ホーム】 >> 【11 | 商品詳細ページ編集】
バリデーションの表示を日本語化していきます。
日本語化するには「rails-i18n」をインストールしなければなりません。
「SampleCart」フォルダにある「Gemfile」に以下のコードを追加します。
gem 'rails-i18n', '~> 7.0', '>= 7.0.8'
【SampleCart\Gemfile】74行目
source "https://rubygems.org" ruby "3.1.2" # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.1.2" # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] gem "sprockets-rails" # Use postgresql as the database for Active Record gem "pg", "~> 1.1" # Use the Puma web server [https://github.com/puma/puma] gem "puma", ">= 5.0" # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] gem "importmap-rails" # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] gem "turbo-rails" # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] gem "stimulus-rails" # Build JSON APIs with ease [https://github.com/rails/jbuilder] gem "jbuilder" # Use Redis adapter to run Action Cable in production gem "redis", ">= 4.0.1" # Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] # gem "kredis" # Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mswin mswin64 mingw x64_mingw jruby ] # Reduces boot times through caching; required in config/boot.rb gem "bootsnap", require: false # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri mswin mswin64 mingw x64_mingw ] end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] gem "web-console" # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] # gem "rack-mini-profiler" # Speed up commands on slow machines / big apps [https://github.com/rails/spring] # gem "spring" gem "error_highlight", ">= 0.4.0", platforms: [:ruby] end group :test do # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing] gem "capybara" gem "selenium-webdriver" end gem 'bootstrap', '~> 5.3', '>= 5.3.2' gem 'sassc-rails', '~> 2.1', '>= 2.1.2' #日本語化 gem 'rails-i18n', '~> 7.0', '>= 7.0.8'
ターミナルで「bundle install
」を実行します。
~/Desktop/Rails7_1/SampleCart $ bundle Fetching gem metadata from https://rubygems.org/......... Resolving dependencies... Using rake 13.1.0 Using base64 0.2.0 Using connection_pool 2.4.1 Using ruby2_keywords 0.0.5 Using minitest 5.20.0 Using rack 3.0.8 Using nio4r 2.7.0 Using websocket-extensions 0.1.5 Using mutex_m 0.2.0 Using erubi 1.12.0 Using racc 1.7.3 Using timeout 0.4.1 Using marcel 1.0.2 Using builder 3.2.4 Using execjs 2.9.1 Using bindex 0.8.1 Using msgpack 1.7.2 Using concurrent-ruby 1.2.2 Using bundler 2.3.7 Using matrix 0.4.2 Using regexp_parser 2.8.3 Using stringio 3.1.0 Using io-console 0.7.1 Using error_highlight 0.6.0 Using zeitwerk 2.6.12 Using mini_mime 1.1.5 Using crass 1.0.6 Using thor 1.3.0 Using public_suffix 5.0.4 Using rexml 3.2.6 Using tilt 2.3.0 Using websocket 1.2.10 Using redis-client 0.19.1 Using drb 2.2.0 Using rack-session 2.0.0 Using rack-test 2.1.0 Using websocket-driver 0.7.6 Using puma 6.4.1 Using ffi 1.16.3 Using net-protocol 0.2.2 Using nokogiri 1.16.0 (x86_64-darwin) Using rubyzip 2.3.2 Using i18n 1.14.1 Using webrick 1.8.1 Using tzinfo 2.0.6 Using date 3.3.4 Using sprockets 4.2.1 Using addressable 2.8.6 Using bootsnap 1.17.0 Using net-pop 0.1.2 Using bigdecimal 3.1.5 Using sassc 2.4.0 Using redis 5.0.8 Using loofah 2.22.0 Using xpath 3.2.0 Using selenium-webdriver 4.16.0 Using net-imap 0.4.9 Using rackup 2.1.0 Using autoprefixer-rails 10.4.16.0 Using activesupport 7.1.2 Using rails-html-sanitizer 1.6.0 Using capybara 3.39.2 Using reline 0.4.1 Using popper_js 2.11.8 Using pg 1.5.4 Using psych 5.1.2 Using net-smtp 0.4.0 Using mail 2.8.1 Using bootstrap 5.3.2 Using rails-dom-testing 2.2.0 Using globalid 1.2.1 Using activemodel 7.1.2 Using rdoc 6.6.2 Using activejob 7.1.2 Using activerecord 7.1.2 Using actionview 7.1.2 Using irb 1.11.0 Using actionpack 7.1.2 Using jbuilder 2.11.5 Using actioncable 7.1.2 Using activestorage 7.1.2 Using actionmailer 7.1.2 Using debug 1.9.1 Using railties 7.1.2 Using sprockets-rails 3.4.2 Using actionmailbox 7.1.2 Using actiontext 7.1.2 Using stimulus-rails 1.3.3 Using rails-i18n 7.0.8 Using importmap-rails 2.0.1 Using rails 7.1.2 Using sassc-rails 2.1.2 Using turbo-rails 1.5.0 Using web-console 4.2.1 Bundle complete! 19 Gemfile dependencies, 94 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed.
次に「SampleCart/config」フォルダにある「application.rb」ファイルに次の一文を追記します。
これによりデフォルトの言語を日本語に設定します。
config.i18n.default_locale = :ja
記述追加 【SampleCart/config/application.rb】27行目
require_relative "boot" require "rails/all" # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module SampleCart class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.1 # Please, add to the `ignore` list any other `lib` subdirectories that do # not contain `.rb` files, or that should not be reloaded or eager loaded. # Common ones are `templates`, `generators`, or `middleware`, for example. config.autoload_lib(ignore: %w(assets tasks)) # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files # in config/environments, which are processed later. # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") config.i18n.default_locale = :ja end end
これでエラーメッセージが日本語になりました。
次にフィールド名を日本語にします。
「SampleCart/config」フォルダの中に「ja.yml」という名前のファイルを新規作成してください。
作成した「ja.yml」ファイルを以下のように編集します。
【SampleCart/config/locales/ja.yml(新規作成)】
ja: activerecord: attributes: good: title: '商品名' description: '詳細' image_url: '画像URL' price: '価格'
【09 | バリデーション(検証)】 << 【ホーム】 >> 【11 | 商品詳細ページ編集】
↓↓クリックして頂けると励みになります。