Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5528

Rails internalization does not translate validation errors

      Rails internalization does not translate validation errors 🗣️

      The Issue

      I want to translate API responses (API = GraphQL) errors to other languages (not only english). Specifically, I want to translate the error strings to pt-BR language.

      Well, on ActiveRecord, there is already a way to translate API responses through the [Rails Internalizations Approach|https://guides.rubyonrails.org/i18n.html.]

      In order to do that on Mongoid, I created a pt-BR yaml file (/config/locales/pt-BR.yml) and placed there the portuguese translations for the mongoid errors file that I found here:

      https://github.com/mongodb/mongoid/blob/master/lib/config/locales/en.yml

      However, the validations is still not being translated to portuguese. It seems like there are some hidden validation error messages that still nee to be translated. I'll talk more about this scenario on the Steps To Reproduce section.

      Steps to Reproduce

      1. Create a class that has some default validations

      ➡ PATH/app/models/sheet.rb

       

      class Sheet < ApplicationRecord  belongs_to :project
        field :id, type: String  field :title, type: String  field :key, type: String  field :position, type: Float  field :active, type: Boolean, default: true
        validates :id, presence: true  validates :title, presence: true, uniqueness: { scope: :project_id }  validates :key, presence: true, uniqueness: { scope: :project_id }  validates :position, presence: true, numericality: { greater_than: 0 }  validates :active, presence: trueend
       

       

      2. Add translations to controller response

      ➡ app/controllers/application_controller.rb

       

        around_action :switch_locale
      
        private
      
        def switch_locale(&action)    locale = params[:locale] || I18n.default_locale    I18n.with_locale(locale, &action)  end 

       

       

      3. Try to create a Sheet with not present values and providing "locale=pt-BR" in the URL

      Result: The errors are NOT translated. 🚨
      (translation missing)

       

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            victorcorcos@gmail.com Victor Costa
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: