Internationalization Tricks in Rails

Here are some quick tips and tricks I learnt when internationalizing my Rails projects.

Automatically Escaping HTML content
This is probably the most often overlooked feature in Rails’ Internationalization API. You may have come across code snippets like below:

Rails has built in HTML escaping behaviour built into their API when your translation key has the term html

Both the forms below are perfectly valid:

Localizing Pluralized Nouns
Another common use case when translating locale is handling pluralization of certain terms. Instead of writing helpers, we can simply define the translations like so:

And to tell the translator when to use the pluralized forms, we’ll just pass in the count interpolation variable:

Looping Arrays of YML definitions
I’ve built static pages using only translated content and these sometimes comes in repeated patterns. Instead of having unique keys for each of the translated labels, you can actually define these terms like an array pattern.

When rendering your views, you can do this:

Hope these little snippets and tricks will come useful for your projects.

Static Pages in Rails

I recently came across a legacy Rails application that had an interesting approach to implementing static pages. This app is basically leveraging on the rails_admin gem to provide a basic form of CMS for building static pages and a controller that parses the friendly slug to render the content. It’s certainly a serviceable solution, however it also comes with it’s own set of complexity such as introducing the friendly_id gem and a category model to separate the content for something that’s unlikely to exceed more than 10 records.

After some thoughts I basically determined that there will ever be just 5 static pages; About Us, Terms of Use, Privacy Policy, Contact Us and FAQ for this project. Enter Thoughtbot’s high_voltage gem. high_voltage was designed to address this very common use-case. If you just need static pages that does not require frequent updates or data driven content, high_voltage fits the bill very well.

To get started, all you need is to bundle the gem in your project.

And do the following:

Then link the pages like so in your app:

And that’s pretty much all you need to start adding new content.

As a general rule, if a page that you’re building does not require frequent updates or simply does not add any business value or logic to the site, it probably better to build it as a static page with as simple backend like high_voltage. Having static pages as views in your app also gives us the benefit of having highly customized CSS/HTML layout design as opposed to a database backed model which makes high fidelity design and content difficult to insert, eg. managing media assets. However, if your solution does require extensible pages being created, you should probably be looking at CMS solutions such as Radiant CMS than building your own solution anyway.

Be sure to check out their Gem for some idea of how you could still manage custom content within a layout for your static pages.

Thoughtbot – High Voltage
https://github.com/thoughtbot/high_voltage
http://thoughtbot.github.io/high_voltage/

sferik – Rails Admin
https://github.com/sferik/rails_admin

norman – Friendly Id
https://github.com/norman/friendly_id

Radiant CMS
http://radiantcms.org/