Subscribe to my Feed, follow me on Twitter, recommend me on Working With Rails or see my code on GitHub
Introducing Babilu: Rails i18n for your JavaScript
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 Mon 02 Feb 13:51
Good work! This could be really really helpful once I start making more use of the I18n stuff in Rails.
By Jon Wood at Tue 03 Feb 05:32
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 Tue 03 Feb 08:07
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 Tue 03 Feb 12:16
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 Tue 03 Feb 12:20
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 Wed 15 Apr 10:31
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 Thu 16 Apr 15:27
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 Mon 18 May 09:09
Looks like something we could use at MadeinLocal.
There are a couple of considerations, though:
It would be interesting to hear other people’s opinion about those points.
Best, Jonas
Leave a comment