Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Move error messages to translation file #878

Open
emcoding opened this issue Nov 21, 2017 · 12 comments
Open

Move error messages to translation file #878

emcoding opened this issue Nov 21, 2017 · 12 comments

Comments

@emcoding
Copy link
Member

emcoding commented Nov 21, 2017

We have some custom error messages, for example here.

def different_projects_required
if project1 && project1 == project2
errors.add(:projects, 'must not be selected twice')
end
end

We want to move these custom error messages to the translation file en.yml. In this commit you can see how it is done, for an error message on the Todo model and its user attribute.

Collect all the error messages and put them into the translation file, at the right place.

Further reading: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

@emcoding
Copy link
Member Author

@asquare14 The best way to start is by selecting one of the issues with the 'beginner-friendly' label. Like this issue (or click on the yellow label, to see all issues with that label). Even if you are not a beginner at coding, they are suitable for first time contributors.
Does this work for you?

After that, if you are looking for something more challenging, you can take a look at the issues labeled 'rgsoc18'. I wouldn't recommend all of them, but we can chat about that later.

To learn more about projects: see https://railsgirlssummerofcode.org/guide/projects/
I don't know much more that what is in the guides ;-) , there is an email address in the guide for further inquiries.

@asquare14
Copy link

asquare14 commented Dec 22, 2017 via email

@nileshgulia1
Copy link

Hey @F3PiX ,I would like to work on the issue,will send a PR soon :)

@emcoding
Copy link
Member Author

Hi @nileshgulia1 Welcome at the Teams App!!! Great that you want to contribute. 👍 🎉

@asquare14 Are you currently working on this issue? Or can we pass it to @nileshgulia1 ?

@nileshgulia1 I'd hate to see you do double work for nothing, so please wait a few days until we know if @asquare14 is working on this.

@asquare14
Copy link

asquare14 commented Dec 27, 2017 via email

@emcoding
Copy link
Member Author

@nileshgulia1 All yours!!! 💃

@asquare14 Thanks for your swift reaction! Much appreciated.

@emcoding
Copy link
Member Author

@nileshgulia1 One more thing: be sure to first pull in the latest changes - we just merged a huge PR - . Then create your branch, so that your commits are on top of the changes.
This will save you a lot of merge conflicts later on.

(Ask away if you need more explanation/help.)

@nileshgulia1
Copy link

hey @F3PiX ,need help translating error messages via i18n.
For translating error messages in app/models/applicaton_draft

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

  def accepted_projects_required
    if projects.any? { |p| p && !p.accepted? } # if they don't exist, the presence validation will handle it
      errors.add(:projects, :must_have_been_accepted)
    end
  end

  def only_one_application_draft_allowed
    unless team.application_drafts.where(season: season).none?
      errors.add(:base, :Only_one_application_may_be_lodged)
    end
  end

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

 errors:
        models:
          application_draft:
                  attributes:
                      projects:
                          must_not_be_selected_twice: must not be selected twice

is this the right approach?

@emcoding
Copy link
Member Author

This is a great start, yes!

  1. The general idea is correct: the original string value becomes the value for the error key. ('must not be selected twice'). I assume the last code block you showed ^ is in the en.yml file. Right? It should be.

  2. It is not required per se that the whole sentence becomes the key; main goal is that it is very clear (for your fellow developers) what error message is generated. That is partly a matter of opinion, but generally they can be more concise than the message for the user. I myself like it when the describe the error. For instance, 'must not be selected twice' can be called 'multiple_selection_error'

  3. I did not yet check the exact place in the en.yml file. One way to check that yourself is update the test files (with these new error keys') and then run the tests. If you happen to add a error to the wrong place, the tests will fail. (For example, for the Application Draft: some are going to :base, others to :projects.) The tests will tell you!

  4. Minor detail: be sure to use lowercase only for the keys. (not :Only_one_application... , in the third method in your question ^)

Okay?

@nileshgulia1
Copy link

@F3PiX Great,Thank you :)

@emcoding
Copy link
Member Author

emcoding commented Jan 7, 2018

Hi @nileshgulia1 How are things going? Let me know if you need help?

@nileshgulia1
Copy link

Hello,@F3PiX, Got busy doing other work,Yeah things are going great ,Sure i will ask if i need help.Thank you :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants