Img source: railscasts.com

We are in an interconnected world, where people across the planet can use the projects that we develop, and having the opportunity to offer them with additional international languages make them a lot better. We can obviously use locale for doing the translations of static strings like labels, or placeholder descriptions that appear across our applications, so we should also have something that can give us a way to translate the dynamic content.  There is a really great Ruby gem that makes the translation of model attributes a lot easier. This gem is called Globalize and is very easy to use.

You can install it quick and easy, as any other Ruby gem.

After you have installed it, you can start to use it.

Whether you are trying to create a new model and then translate that model’s attributes, or trying to translate the data from an existing model, you will need to create a new migration for creating a new table. Then, this table will be used as a utility that is tied to the table of the model that we are intending to do the translations for. For example, we may have a model called Post, and then want to translate the title and the name of this posts from this model. To do that, we must generate a migration similar to the following:

TranslatePosts < ActiveRecord::Migration
<span style="font-weight: 400;">  </span><span style="font-weight: 400;">def</span> <span style="font-weight: 400;">self.up</span>
<span style="font-weight: 400;">    </span><span style="font-weight: 400;">Post</span><span style="font-weight: 400;">.create_translation_table!({</span>
<span style="font-weight: 400;">      </span><span style="font-weight: 400;">:title</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">:string</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">      </span><span style="font-weight: 400;">:text</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">:text</span>
<span style="font-weight: 400;">    }, {</span>
<span style="font-weight: 400;">      </span><span style="font-weight: 400;">:migrate_data</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">true</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">      </span><span style="font-weight: 400;">:remove_source_columns</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">true</span>
<span style="font-weight: 400;">    })</span>
<span style="font-weight: 400;">  </span><span style="font-weight: 400;">end</span>

<span style="font-weight: 400;">  </span><span style="font-weight: 400;">def</span> <span style="font-weight: 400;">self.down</span>
<span style="font-weight: 400;">    </span><span style="font-weight: 400;">Post</span><span style="font-weight: 400;">.drop_translation_table! </span><span style="font-weight: 400;">:migrate_data</span><span style="font-weight: 400;"> => </span><span style="font-weight: 400;">true</span>
<span style="font-weight: 400;">  </span><span style="font-weight: 400;">end</span>
<span style="font-weight: 400;">end</span>

Then we also need to add the following at the Post model:

translates :title, :text

After we are done with this, we can now see that we have already new associations added to posts instances.

class <span style="font-weight: 400;">Post < ActiveRecord::Base</span>
<span style="font-weight: 400;">  translates </span><span style="font-weight: 400;">:title</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">:name</span>
<span style="font-weight: 400;">end</span>

<span style="font-weight: 400;">puts</span><span style="font-weight: 400;"> post.translations.inspect</span>
<span style="font-weight: 400;"># => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize rocks!", name: "Globalize">,</span>
<span style="font-weight: 400;">      </span><span style="font-weight: 400;">#<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]</span>

<span style="font-weight: 400;">I18n</span><span style="font-weight: 400;">.locale </span><span style="font-weight: 400;">=</span> <span style="font-weight: 400;">:en</span>
<span style="font-weight: 400;">post.title </span><span style="font-weight: 400;"># => 'Globalize rocks!'</span>
<span style="font-weight: 400;">post.name  </span><span style="font-weight: 400;"># => 'Globalize'</span>

<span style="font-weight: 400;">I18n</span><span style="font-weight: 400;">.locale </span><span style="font-weight: 400;">=</span> <span style="font-weight: 400;">:nl</span>
<span style="font-weight: 400;">post.title </span><span style="font-weight: 400;"># => ''</span>
<span style="font-weight: 400;">post.name  </span><span style="font-weight: 400;"># => 'Globalize'</span>

You can also watch an example of this gem being used in a Rails application by watching the brief episode from RailsCast:

You can go to this gem’s GitHub page to view its source code, and learn more about.