[Note] Error message when using Rails Form object pattern Japanese

Overview

--Since the error content played in the validation of the input form when using the Rails Form object pattern is displayed in Japanese, I will summarize it as a memorandum.

Premise

--The Rails application uses the Form object pattern.

Briefly about Form object patterns

--One of the implementation patterns in development using Rails. --Mainly used when you want to operate multiple models when submitting one form, or when you want to validate information that does not store information in the table.

Validation when using Form object

--First, when saving data from one form to multiple tables, if you do not use a Form object, in the model related to multiple tables when processing (create or update) the value sent from the form If the input value is flipped by the validation of the description, the process cannot be continued. --The Form object pattern is used to make up for that point. --By describing the validation in the instance of Form object, if the input value from the form is caught in the validation, an error message can be returned to the form together with the input information.

About Japanese localization of error messages

  1. In application.rb in the config folder of the application being created

config.i18n.default_locale = :ja ← Add this sentence.


cnofig>application.rb


module (Created application name)
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    #Japanese language setting
    config.i18n.default_locale = :ja  #← Apply this sentence in config.Added to rb.

    # 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
  1. Introduce a gem called "rails-i18n" that supports Japanese. -With the introduction of this Gem, English sentences translated by rails-i18n can be used as Japanese.

Gemfil


~Omission~
gem 'rails-i18n'

Terminal


bundle install
  1. Create a locale file for Japanese translation for words that cannot be supported by the rails-i18n Gem. -Locale file: A language file that can handle various languages. -First, create the locales folder in the config folder. -And create a file called "ja.yml" in the config/locales directory. スクリーンショット 2021-01-13 16.36.23.png

  2. After creating the ja.yml file, write the English / English words and Japanese translation you want to translate into Japanese in the file. -For the description in the ja.yml file, please refer to the code below.

ja.yml


ja:
  activerecord:  #← Translation part for the model that inherits ActiveRecord
    attributes:
      user:               #← Model file name
        nickname:nickname#←rails-Word names not translated by i18n
        gender:sex#←rails-Word names not translated by i18n
        age:age#←rails-Word names not translated by i18n

  activemodel:  #← Translation of Form object pattern created by inheriting ActiveModel to model
    attributes:
      book_data:                   #← Model file name using Form object pattern
        title:title#←rails-Word names not translated by i18n
        publisher:the publisher#←rails-Word names not translated by i18n
        author:Author#←rails-Word names not translated by i18n
        isbn: ISBN                 #←rails-Word names not translated by i18n
  1. Check with your browser after restarting the server. -After restarting, check if the form validation error message is translated without any problem.

Recommended Posts

[Note] Error message when using Rails Form object pattern Japanese
[Rails] Japanese localization of error message when using Form object
Error when using rails capybara
[Rails] Let's translate the error message into Japanese
[Rails] Japanese localization of validation error message ~ ja.yml ~
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Note] Rails error list
Error when using SnapKit
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
[Rails] Japanese localization using rails-i18n
Finally implemented Rails Form object
From Ruby on Rails error message display to Japanese localization
Detailed tips when using Rails
[Rails] What was the error message?
[Rails] Japanese localization of error messages
[Rails] Display form error messages asynchronously
Japanese localization of error messages (rails)
[Rails] About the error when displaying the screen due to the autofocus of the form
When submitting form, no error message is displayed even though validation failed
Get the error message using the any? method