10.04.2017 Views

Rails I18n – Seven Best Practices That You Should Know About

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Rails</strong> Internationalization (<strong>I18n</strong>) - <strong>Seven</strong> <strong>Best</strong> <strong>Practices</strong> <strong>That</strong> <strong>You</strong> <strong>Should</strong> <strong>Know</strong> <strong>About</strong><br />

Take Advantage Of Nesting<br />

It is not advised to store all your translation messages on the same level of nesting (using flat<br />

naming). For example, this is not a recommended practice:<br />

● en:<br />

○ submit: ‘Submit’<br />

○ log_out: ‘Log Out’<br />

○ blog: ‘Browse Blog’<br />

○ errors: ‘Errors were found:’<br />

As you see, all the messages here are messed up <strong>–</strong> they relate to different pieces of the<br />

application but still they are mixed together. It is much better to introduce the parent keys to<br />

nest translations of the same type. For example, you might have something like that:<br />

● en:<br />

○ forms:<br />

■ submit: ‘Submit’<br />

■ errors: ‘Errors were found’<br />

○ main_menu:<br />

■ log_out: ‘Log Out’<br />

■ blog: ‘Browse Blog’<br />

This way the messages are grouped and it is easier to manage them. By the way, when<br />

translating <strong>Rails</strong> models you must follow this principle, as attributes’ names should be nested<br />

properly:<br />

1<br />

2<br />

3<br />

4<br />

5<br />

en:<br />

activerecord:<br />

attributes:<br />

category:<br />

name: 'Name'<br />

phraseapp.com | sales@phraseapp.com | +49-40-357-187-76 | twitter.com/phraseapp | facebook.com/phraseapp | linkedin.com/company/phraseapp

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!