Oct 30, 2008 @ 02:38 pm

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 :

  1. #français
  2. fr = {    "hi" => "bonjour",    "welcome" => "bienvenue"  }
  3. # espanol
  4. es = {    "hi" => "hola",    "welcome" => "bienvenida"}
  5. SimplyGlobal.add_language_hash(:fr, fr)
  6. 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) :

  1. fr = {"hi" => "bonjour"} # Create the language hash
  2. SimplyGlobal.add_language_hash(:fr, fr) # Add the language hash to simplyglobal
  3. SimplyGlobal.locale = :fr # Assigns the locale to use
  4. "hi".t # returns "bonjour"

As simple as that!

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

  1. fr = {"hi %s%d" => "bonjour %s%d"} # Create the language hash
  2. SimplyGlobal.add_language_hash(:fr, fr) # Add the language hash to simplyglobal
  3. SimplyGlobal.locale = :fr # Assigns the locale to use
  4. "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.

  1. SimplyGlobal.add_language_hash(:fr, {"hi" => "bonjour"}) # Add the french language hash
  2. SimplyGlobal.add_language_hash(:es, {"hi" => "hola"}) # Add the spanish language hash
  3. "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.

  1. def index
  2.   SimplyGlobal.locale = :fr
  3.   # Will try to render index_fr.html.erb
  4.   # rather than index.html.erb
  5. end

It also works for the partial.

  1. <%= render :partial => "info" %>

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

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Rate this post :
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
Posted under : Easy reading
8 Comments
MyAvatars 0.2

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?

Comment by : Zubin
— October 30, 2008 @ 4:14 pm

MyAvatars 0.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!

Comment by : Dan
— October 30, 2008 @ 4:24 pm

MyAvatars 0.2

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…

Comment by : Luke
— October 31, 2008 @ 11:33 am

MyAvatars 0.2

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 : http://code.google.com/p/simplyglobal/issues/detail?id=4

Comment by : Dan
— October 31, 2008 @ 11:48 am

MyAvatars 0.2

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

Comment by : jeremy
— November 1, 2008 @ 12:59 am

MyAvatars 0.2

[…] simplyglobal : A simple globalization plugin for Rails (tags: ruby rails programming) […]

— November 1, 2008 @ 3:08 am

MyAvatars 0.2

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.

Comment by : Frank
— November 1, 2008 @ 10:20 am

MyAvatars 0.2

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

Comment by : Dan
— November 3, 2008 @ 6:14 pm




Leave a comment
Name (required)
Email (will not be publish) (required)
Website