Sneaky Abstractions

Subscribe to my Feed, follow me on , recommend me on Working With Rails or see my code on GitHub

Introducing Babilu: Rails i18n for your JavaScript

Posted on February 02, 2009 12:48, 8 comments. Tagged with rails, javascript, i18n.

Babilu is a Rails plugin that mirrors your i18n setup in JSON and JavaScript format. This allows you to take your translations with you when you cross that great divide which separates the server from the browser. It provides a very similar environment that you can use in your JavaScript code to do translations directly in the browser.

I18n.defaultLocale // "en"
I18n.locale // Whatever the locale has been set to on the server

I18n.t('hello') // "Hello World"
I18n.t('messages.invalid', {scope:['activerecord', 'errors']})  // "is invalid"
I18n.t('activerecord.errors.template.header', {count:4, model:'horse'}) // "4 errors prohibited this horse from being 
saved"

//Defaults work the same way as in Rails
I18n.t('doesntexist', {defaultValue:'humbaba'}) // "humbaba"
I18n.t('doesntexist', {defaultValue:':hello'}) // "Hello world"
I18n.t('doesntexist', {defaultValue:[':alsodoesntexist', 'The Sasqutch: Fact or Fiction?']}) // "The Sasquatch: Fact or 
Fiction?"

To install:

script/plugin install git://github.com/toretore/lucy.git
script/plugin install git://github.com/toretore/babilu.git

Then load the generated JavaScript in your layout:

<head>
  <script type="text/javascript" src="/javascripts/locales.js"></script>
</head>

Babilu will automatically reload your locales on each request in development mode, just like you’d expect, but in production they will only be loaded when the server starts up.

Comments

Atom feed

By Christian at

Good work! This could be really really helpful once I start making more use of the I18n stuff in Rails.

By Jon Wood at

Looks very cool – I’m slightly confused as to what Lucy is though. Something tells me it’s code with immense power, but I can’t see what it would be.

By Tore Darell at

Jon: Lucy is the missing link between Rails and JavaScript – it generates JavaScript/JSON on demand (on server start or per request). Babilu uses it to export the translations and the JS “framework” which uses them, and I’m planning on using Lucy in a few more plugins. Additionally, Lucy exports a few bits of information about Rails, like the environment and the authenticity token, to common.js.

By Clemens Kofler at

Not to be overly critical here but this plugin doesn’t have a single test (and neither has Lucy). Personally, I’d have a really hard time trusting plugins that aren’t tested properly.

Other than that it looks pretty cool. You’re definitely filling a gap here.

- Clemens

By soleone.myopenid.com at

Just two quick notes about the layout of your blog: 1.) The OpenID textfield needs some padding-left, because you write over the icon (at least in Safari). 2.) The headline is awesome! Simple but nice effect :)

By Cristobal Viedma at

Great work!

Just a simple suggestion. Maybe when a key is not found in a given language, it could use the translation in the default language.

Now it just show a “null”, but I think that other approach is even better. Only in case is not defined in the chosen locale neither in the default one, it would show the ugly null.

By tore.darell.no at

Cristobal: That would be nice, but it’s a goal that Babilu’s interface is as close as possible to that of Rails I18n, which does not fall back to a default language (I think)..

By openid.org/sjlver at

Looks like something we could use at MadeinLocal.

There are a couple of considerations, though:

  • As Clemens said, more unit tests would be nice (although I’ve seen you’ve added some since that post)
  • It would be important to us only to export parts of the translation file (e.g. the keys “javascript.*”). This keeps the javascript smaller and prevents people from seeing translations they shouldn’t see.
  • The Globalize2 plugin provides things like fallback languages. I agree to Cristobal that it would be nice to have them in Babilu, too.

It would be interesting to hear other people’s opinion about those points.

Best, Jonas

Leave a comment

OpenID

Or..

Old school

Search out the door of Namtara. Bring a baked loaf into his presence.