简体   繁体   中英

Translate error messages using rails (I18n) Api

I am using rails internationalization api for activerecord translations .I am having trouble translating error messages. How to translate multiple error messages from different files in my project?I am have files named application_draft.rb,team.rb,user.rb,todo.rb inside my models folder.I want to translate error messages in them ,here's my en.yml file:

errors:
        models:
         -team:
         -application_draft:
         -conference:
         -todo:
         -user:
            attributes:
              roles:
                too_many_reviewers: too many reviewers
                multiple_sel_error: must not be selected twice
                must_accepted: must have been accepted
                one_app_allowed: Only one application may be lodged
                confirmed_email_address: Please make sure every student confirmed the email address.
                wrong_date_sel: must be a later date than start date
                no_more_than_two: "there cannot be more than 2 students on a team."
                cannot_changed: can't be changed

I have implemented this code and throws error(means it did not work). Here's my one of application_draft.rb and todo.rb error code snippets:

application.rb:

def different_projects_required
  if project1 && project1 == project2
    errors.add(:projects, :multiple_sel_error)
  end
end

todo.rb

def validate_number_of_reviewers
  errors.add(:user, :too_many_reviewers) if application.todos.count > 3
end

How to translate these both avoiding duplication errors?

Inherit it from activerecord:

en:
  activerecord:
    errors:
      models:
       -team:
       -application_draft:
       -conference:
       -todo:
       -user:
          attributes:
            roles:
              too_many_reviewers: too many reviewers
              multiple_sel_error: must not be selected twice
              must_accepted: must have been accepted
              one_app_allowed: Only one application may be lodged
              confirmed_email_address: Please make sure every student confirmed the email address.
              wrong_date_sel: must be a later date than start date
              no_more_than_two: "there cannot be more than 2 students on a team."
              cannot_changed: can't be changed

if it didn't work. I have another way. Try entering the translation in the method itself like below

    application.rb:

    def different_projects_required
      if project1 && project1 == project2
        errors.add(:projects, I18n.t('activerecord.errors.models.user.attributes.roles.multiple_sel_error'))
      end
    end

    todo.rb

    def validate_number_of_reviewers
      errors.add(:user, I18n.t('activerecord.errors.models.todo.attributes.roles.too_many_reviewers')) if application.todos.count > 3
    end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM