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 config/locales/l10nized.yml:

things:
  some_heading: Some heading
  this_thing_is_called_a: This thing is called {{a}}

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 t().

There’s more information on GitHub, and it’s available as a gem called threedaymonk-l10nizer.

Comments

Skip to the comment form

  1. Tom Ward

    Wrote at 2009-07-12 16:57 UTC using Safari 530.19 on Mac OS X:

    Looks pretty cool. I didn’t really understand what you were trying to get across when you showed the snippet the other day, but now I see. Could well be really useful.

Leave a comment

Please read the comment guidelines before posting. Comments are Gravatar-enabled. Your email address will not be published.

To prove that you’re human, type human in the Bot check field.

Trying to post some program output or a long code sample? Please use a paste service and link to it instead.