10.04.2017 Views

Rails I18n – Seven Best Practices That You Should Know About

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Helper Methods<br />

<strong>Rails</strong> offers us a bunch of powerful ​helper methods that can be used to display datetime and<br />

month select boxes, convert numbers to currency and display distance of time in words. Some<br />

of them work as magic and can really save you from writing many lines of code every time.<br />

However, be warned that some of these methods are somewhat expensive in tems of processing<br />

time. ​distance_of_time_in_words is one of such methods <strong>–</strong> it says how much time has passed since<br />

the provided datetime. If you are planning to use this method extensively in some view (the<br />

typical example is the comments block to say how old the comment is), then maybe it is better<br />

to reconsider and put it away. <strong>You</strong> might think about employing ​fragment caching here but then<br />

you’ll need to constantly expire it. Time runs fast and the phrase “the comment was published X<br />

days ago” should be constantly updated.<br />

Another solution to this problem is performing the calculation on the client-side. For example,<br />

there is a great library called ​Moment.js​ ​that supports this feature (and many others).<br />

Stick With PhraseApp!<br />

Working with translations is hard: you might easily miss some translations for a specific<br />

language which will lead to user’s confusion. And so PhraseApp can make your life easier!<br />

Grab your 14-days trial​. PhraseApp supports many different languages and frameworks,<br />

including JavaScript of course. It allows to easily import and export translations data. What’s<br />

cool, you can quickly understand which translation keys are missing because it’s easy to lose<br />

track when working with many languages in big applications. On top of that, you can<br />

collaborate with translators as it’s much better to have ​professionally done localization for your<br />

website.<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!