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
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 :

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) :

As simple as that!

You can use it like the % method of the String class.

You can also return all translations for a word. That is a special feature developed only for Frank.

Using it with views

Just create a view ending with _fr and simplyglobal will use it.

It also works for the partial.

will try to render _info_fr.html.erb rather than _info.html.erb.

  • Zubin

    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?

  • Dan

    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.


  • Luke

    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…

  • Dan

    I had this problem too. When I coded the “hello %s”.t(“Johnny”), it suited my needs… until the day I had the problem you are describing. Maybe one day, I will add that feature. I keep it has a possible enhancement :

  • jeremy

    Will this not become redundant with the imminent release of rails 2.2 with i18n support?

  • Pingback: links for 2008-11-01 « Bloggitation()

  • Frank

    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.

  • Dan

    The i18n support in Rails 2.2 looks great! Can’t wait to try it. But for the moment, I will still use SimplyGlobal.