学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.0 | 16 | Webアプリケーション開発 | バリデーションの日本語化

<<前  [TOP]  次>>


バリデーションの表示を日本語化していきます。
日本語化するには「rails-i18n」をインストールしなければなりません。


「shop」フォルダにある「Gemfile」をテキストエディタで開いて、最後に「gem 'rails-i18n'」と追記します。
【shop\Gemfile】

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.6'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.2', '>= 6.0.2.2'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'rails-i18n'



コマンドプロンプトで「shop」フォルダに移動して「bundle install」を実行します。

「rails-i18n」インストール
「rails-i18n」インストール
「rails-i18n」インストール
「rails-i18n」インストール


次に「config」フォルダにある「application.rb」ファイルに次の一文を追記します。
これによりデフォルトの言語を日本語に設定します。

config.i18n.default_locale = :ja



【C:\Rails6\work\shop\config\application.rb】

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 Shop
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    # 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.

	config.i18n.default_locale = :ja

  end
end



これでエラーメッセージが日本語になりました。

メッセージの日本語化
メッセージの日本語化


次にフィールド名を日本語にします。
「config」フォルダにある「locales」フォルダの中に「ja.yml」ファイルを新規作成して以下のように記述して下さい。
「Tab」キーを使用すると文字化けしてエラーがでますので、半角スペースを使用して下さい。


【C:\Rails6\work\shop\config\locales\ja.yml(新規作成)】

ja:
 activerecord:
  attributes:
   good:
     title: '商品名'
     description: '詳細'
     image_url: '画像URL'
     price: '価格'



フィールド名の日本語化
フィールド名の日本語化


↓↓クリックして頂けると励みになります。


<<前  [TOP]  次>>