simplyglobal : A simple globalization plugin for Rails

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.

8 thoughts on “simplyglobal : A simple globalization plugin for Rails

  1. Nice one Dan, I like the way it’s simple and therefore flexible. One question: If, say, one wanted to draw on a database of phrases instead of a manual hash, is there a method to reload the translations when they are edited?

  2. I guess you could just reload a new hash of translations and it would replace the old translation hash. I’d say that if you change it once, it would change for the whole application in production (since the classes are cashed and it uses @@ class variables). In development, you’d have to reload the new translations at each request.
    I’m not 100% sure of what I say but if you test it, I’d like to know the result.
    Thanks!

  3. this is a subject of some interest to me. simplicity is good, and this is probably fine for completely static translations. what about dynamically-generated text? i don’t know if any frameworks really handle well a situation where, e.g., the text “i found #{x} files in #{y} directories” needs to be translated into a language where the word order would come out “in #{y} directories, #{x} files were found” and furthermore you need to inflect files/directories depending on the cardinality of x and y (which in some languages is far more complicated than singular vs plural). keep hoping someone will code that one up…

  4. I just wanted to say that we used SimplyGlobal in 4 projets already and it did the job very well everytime. What I like the most about Dan’s plugin is the fact that the translations are NOT stored in YML files or in the database. Everything is plain Ruby. Moreover it’s a very light plugin that just does what you expect. Now maybe i18n support in rails 2.2 will make simplyglobal unnecessary… this I don’t know. But for now, this plugin is the best of its kind in my opinion.

Leave a Reply to Zubin Cancel reply

Your email address will not be published. Required fields are marked *