Subscribe to my Feed, follow me on Twitter, recommend me on Working With Rails or see my code on GitHub
Jibberish
http://nimrod.interinter.net/plugins/trunk/jibberish/
This is something I created a few weeks ago but didn’t tell anyone about yet. I recently used (something like) it on a client application, and it’s been working really well, so I thought I’d share it with the world. It’s a plugin for Gibberish (yes, that’s a plugin to a plugin) which takes all the defined translations, exports them to JavaScript and adds a few convenience methods.
Why would anyone need this? Well, let’s say you’re building a Multilingual Web 2.0 App With Lots Of Ajax (MWTAWLOA). You use the delightfully simple Gibberish to translate all the strings in your MWTA, and all is well. After a while, you start to layer all the Ajax functionality on top of your (already functioning) application, the way God intended. But wait, what about all those strings in your JavaScript? How will your Elbonian users understand what you’re trying to say? You could create different JS files for each language, and let the server decide which one gets used, but that results in a lot of duplication and WETness. Or you could create a separate translation system for the client side of things (which is very easy), but again this results in duplication. What you should do, of course, is reuse the translations already present on the server on the client.
So, how do you use Jibberish? First of all, you must be using Gibberish, as Jibberish is meaningless without it. Then, after installing the plugin from Github, include the JavaScript file in your layout
<!-- ... -->
<head>
<title>My multilingual Rails app</title>
<script src="/javascripts/jibberish.js"></script>
</head>
<!-- ... -->
And access the translations through the Jibberish object:
Jibberish.locales //=> {es: {...}, no: {...}}
Jibberish.locales.es.hello //=> "Hola"
If you set a “locale” cookie containing the language code of the current language..
def set_locale
Gibberish.current_language = 'es'
cookies['locale'] = 'es'
end
..things get even easier:
Jibberish.currentLanguage //=> "es"
Jibberish.get('order_beer') //=> "Una cerveza, por favor"
The “locale” cookie will be picked up automatically, but you can set the current language on the client as well:
Jibberish.currentLanguage = 'no'
Jibberish.get('order_beer') //=> "Gi mæ en øll"
It even has a convenient String method, like Gibberish:
"Good evening".t('good_evening')//=> "Buenas tardes"
//It has default key handling like Gibberish
"I have fish in my pants".t() //=> "Æ har fesk i boksa"
//The key will override the contents of the string
"My pants are fish-free".t('i_have_fish_in_my_pants') //=> "Æ har fesk i boksa"
