The Why’s of I18n

Internationalization (I18n) is one of the simplest steps you can take to make your Rails code shine. I8n offers a way to extract string and localization data from a Rails app. It mainly consists of using two functions in your views I18n.translate (for strings) and I18n.localize (for date/times). They are often seen in their abbreviated forms t and l. At Metova, I18n is a minimum requirement for writing code: if your views haven’t been I18n’d then your merge request is going to be rejected. Keep scrolling for reasons why you should care about I18n.


Avoiding “magic strings”

Text typed directly in views is no different than calling a function directly with…726, -2,000,000 or 3.14 as the argument. Why is that there, what does it represent? This is known as a magic number, and strings can suffer from the same phenomenon. Magic strings are easy to ignore since we feel that the intent is implicit in the actual content of the string. This is not necessarily the case, and the code becomes noisier and harder to understand for the next developer who has to work on the project. Concise and explicit expression of intent is a must at all times when writing code. Magic strings should feel gross … embrace and cultivate this feeling.


Staying DRY

Don’t Repeat Yourself is a key philosophy in modern development. Why should that be ignored when it comes to strings? You shouldn’t be fixing broken tests every time you change some text. You shouldn’t have to jump all over your project when you want to change a ‘Save’ button to ‘Submit’. With some luck these changes could be as easy as a project search/replace, but frequently it’s a lot uglier and more time consuming, and it can lead to mistakes such as words that are spelled the same but mean different things.

 

It’s actually…pretty easy

Set up a file for your language and add the keys and values you need (English is included by default in Rails). Then put those keys on the front end. I18n even offers a convenient hierarchical way to organize this stuff. Like most things in Rails, I18n works with convention over configuration, so if that hierarchy matches the file/folder structure of your views, you can shortcut to the keys for your page. Beyond that, I18n is such a standard tool that the most popular gems come with support for it out of the box.


Preparing for translation

Is your app going to be used for more than 5 minutes and 5 users? If so then there is a good chance that you’ll eventually have users that would benefit from your project being available in a different language. If you have been using I18n already, then you’re well on your way to adding other languages when the time comes. Adding another language to a site that is already using I18n is a quick process. Once you have the translations for your locale file, it can be a matter of hours to get everything you explicitly added set up.
Starting to sound like a good idea?


Stay tuned to the blog for more tips on implementing I18n on your sites.

SEE OUR JOB OPENINGS

 

Topics: Ruby on Rails, Coding & Development

Lee Dykes

Written by Lee Dykes

Lee has been in software development for over 7 years. He has worked with a variety of languages and frameworks including Ruby on Rails, Sinatra, .NET MVC/Web forms, and PHP. Often, he is the sole developer on projects and takes them from concept to launch. Lee is no stranger to developing working relationships with customers and often assists with non-development tasks including business strategy, marketing, SEO, and UX/UI design.

Enjoy the article? Share it!