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 />

Split Translations<br />

Large applications may have hundreds of translations keys. If they are stored in a single file it<br />

becomes really hard to manage them, therefore it’s a good idea to split your messages into<br />

various files stored in separate folders. For example, you may have a folder named ​models<br />

storing translations for the models’ attributes,​ forms​ to translate form-related stuff etc:<br />

● locales<br />

○ models<br />

■ en.yml<br />

■ ru.yml<br />

○ forms<br />

■ en.yml<br />

■ ru.yml<br />

However, by default this is not going to work as <strong>Rails</strong> does not load translations from the nested<br />

directories. To fix this, you’ll need to add the following line of code inside your<br />

config/application.rb​ file:<br />

1 config.i18n.load_path += Dir[<strong>Rails</strong>.root.join('config', 'locales', '**', '*.{rb,yml}')]<br />

load_path​ is a setting to announce your custom translation files and it should do the trick.<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!