simplyglobal : A simple globalization plugin for Rails
The following is a guest post from Dan Simard
Sometimes, you have to reinvent the wheel. It’s really sad to say and you will probably hate me for saying that (and I know that you’ll do because I hate myself for it). I’ve written a new globalization plugin for Rails.
Why did I reinvented the wheel?
I searched and tried a lot of other globalisation plugins… and I really tried them. I spent hours with Globalize. It was just too much. I tried the gettext_Localize that works with the good ol’ gettext command. Fuck it. Too complicated. It just didn’t fit my needs at all.
All I wanted was a wheel that you can put a wood-pole in the middle and then it could start spinning. I made one.
You can go on the simplyglobal project homepage to learn on to install it and use it.
In fact, this is not really a globalization plugin because there’s no localization handling or anything that can look like it. The name should have been simplytranslated but I already created the project with the name simplyglobal and it was an hassle to change it.
How to install
1. Execute ./script/plugin install http://simplyglobal.googlecode.com/svn/trunk/simplyglobal
2. Create a file named simplyglobal.rb in the config/initializers directory
3. In simplyglobal.rb, create hashes of language
Add the language hashes to the objectYou will end up with a file named simplyglobal.rb that looks like this :
#français
fr = { "hi" => "bonjour", "welcome" => "bienvenue" }
# espanol
es = { "hi" => "hola", "welcome" => "bienvenida"}
SimplyGlobal.add_language_hash(:fr, fr)
SimplyGlobal.add_language_hash(:es, es)
In development, this file will be loaded every request. In production, it is loaded once.
How to use it with strings
After you installed it, you can use it in these various ways.
SimplyGlobal adds a t() method to all string objects that will return the translated string. Example, if you have defined a language hash that looks like this (note : normally, the languages hash are defined in config/initializers/simplyglobal.rb but I put it inline for the sake of the example) :
fr = {"hi" => "bonjour"} # Create the language hash
SimplyGlobal.add_language_hash(:fr, fr) # Add the language hash to simplyglobal
SimplyGlobal.locale = :fr # Assigns the locale to use
"hi".t # returns "bonjour"
As simple as that!
You can use it like the % method of the String class.
fr = {"hi %s%d" => "bonjour %s%d"} # Create the language hash
SimplyGlobal.add_language_hash(:fr, fr) # Add the language hash to simplyglobal
SimplyGlobal.locale = :fr # Assigns the locale to use
"hi".t("Johnny", 5) # returns "bonjour Johnny5"
You can also return all translations for a word. That is a special feature developed only for Frank.
SimplyGlobal.add_language_hash(:fr, {"hi" => "bonjour"}) # Add the french language hash
SimplyGlobal.add_language_hash(:es, {"hi" => "hola"}) # Add the spanish language hash
"hi".t(:all) # returns a hash : {:fr => "bonjour", :es => "hola"}
Using it with views
Just create a view ending with _fr and simplyglobal will use it.
def index
SimplyGlobal.locale = :fr
# Will try to render index_fr.html.erb
# rather than index.html.erb
end
It also works for the partial.
<%= render :partial => "info" %>
will try to render _info_fr.html.erb rather than _info.html.erb.