L10nizer: Ex post facto localisation for Rails applications
Localisation (or localization or localisación or just ‘l10n’ to avoid such conflicts) lets you display a website in multiple languages and language variants. Even if you’re not doing this it can be useful in letting you decouple copy from templates and models, and putting the copy in one place where a copywriter can review and edit it.
If you didn’t start out by localising your application,
though—or even if you did but strayed from the righteous path along
the way—then you’re faced with a problem. You have to go back and
find all the text in the templates, change it into calls to
t(), and move all the text into a separate
localisation file. Tedious, dull, and error-prone.
If only a computer could automate this work …
With a bit of help from Treetop, I’ve written a utility called l10nizer that does just this.
For example, given a file
app/views/things/show.html.erb with this content:
<div class="thing"> <h1>Some heading</h1> <p>This thing is called <%= h(@thing.name)</p> </div>
l10nizer will change it to:
<div class="thing"> <h1><%= t("things.some_heading") %></h1> <p><%= t("things.this_thing_is_called_a", :a => h(@thing.name)) %></p> </div>
and generate the following entries in
things: some_heading: Some heading this_thing_is_called_a: This thing is called
In other words, it extracts text, including any Ruby code
evaluated inline as part of that text, generates a reasonable
localisation key with any placeholders needed, and replaces the
occurrence of that text in the template with a call to
information on GitHub, and it’s available as a gem called