0

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?

  • Can you share the exact error message? Also, the `todo.rb` file snippet seems to have extra `end` at the bottom but i suppose you just copied it wrong – mbajur Dec 29 '17 at 15:00
  • Possible duplicate of [Ruby on Rails i18n - Want To Translate Custom Messages in Models](https://stackoverflow.com/questions/17935597/ruby-on-rails-i18n-want-to-translate-custom-messages-in-models) – XML Slayer Dec 29 '17 at 15:21
  • @mbajur ,yes there is extra 'end' in todo.rb sorry for that.I am getting duplication error .My question is how do i translate error logs in both applcation_draft.rb and todo.rb without any duplication errors?I have used the above mentioned en.yml code but throws this error: `Failure/Error: expect(todo.errors.messages).to eq({ user: ['too many reviewers'] }) expected: {:user=>["too many reviewers"]} got: {:user=>["translation missing: en.activerecord.errors.models.todo.attributes.user.too_many_reviewers"]}` – Nilesh Gulia Dec 29 '17 at 16:27
  • @mbajur How do i translate messages in multiple models at the same time? – Nilesh Gulia Dec 29 '17 at 16:37

1 Answers1

0

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
Dyaniyal Wilson
  • 1,012
  • 10
  • 14