Peter Marklund

Peter Marklund's Home


Rails Internationalization with new gibberish_translate Plugin

We needed to internationalize the user interface of a Rails application that we are building and looked at a plethora of alternatives, such as Globalize, Globalite and the localization plugin by DHH. Finally we settled for the Gibberish plugin. Gibberish uses an unusual hybrid approach with both inline english texts in the code and keys for message lookups. A typical Gibberish lookup looks like this:

    update_show_page("The course has been booked"[:bookings_book_confirm])

To complement the Gibberish plugin I've drafted the gibberish_translate plugin that adds a script for extracting all message lookups from a Rails app and a controller with a web UI for doing translations. The plugin also keeps track of which english texts a translation was made from so that you can flag when english texts are changed. The plugin avoids using the database and works directly against the YAML message files in the lang directory.

8 comment(s)


Jeff Jones said 2008-06-25 10:48:

Wonderful piece of code. Makes my life a lot easier. One thing, I have the ya2yml gem installed but Japanese translations are still being output as hex codes instead of the actual Kana. Is there a way to check that the plugin is seeing the gem?

bob said 2008-05-01 17:06:

Ah, the plugins are loaded before application.rb so u have to put the protect_from_forgery in the translate_controller too

bob said 2008-05-01 17:02:

I'm using Rails 2.0.2 I get a ActionController::InvalidAuthenticityToken in Gibberish/translations#index when trying to go to /translations The only way i can get it to work is to comment out protect_from_forgery :secret in application controller but i dont want to do this. Any ideas how to fix this one?

face said 2008-01-27 15:15:

Ok, about 30 seconds after posting the regular expression above I yet again realized it was wrong (didn't handle string interpolated). 16 days later and I haven't broken it yet so I think it is safe to correct my corrected regular expression: /#{start_token}((?:[^#{end_token}](?:\\#{end_token})?)+)#{end_token}\[:*([a-z_]*)[,\]]/m I also wrote an article on using gibberish_translate with Google Language Tools for automatic translations which can be found here: In using gibberish_translate I found a bug (at lest under Rails 2.0.2). translations_controller.set_translation_locale sets the translation_locale to either params[:translate_locale] or Gibberish.languages.first. As params[:translate_locale] is not a field or hidden field in the Form a save will always overwrite Gibberish.languages.first no matter what locale you are editing. I fixed this by modifying set_translation_locale() to check the session before the default. You can see the exact code in my article about Google Language tools linked above. Thanks again Mark for a great tool.

face said 2008-01-10 16:10:

Hello Again, I was a little over zealous, and my regular expression to match "foo bar"[] was not right. Here is the corrected version: /#{start_token}((?:[^#{end_token}](?:\\#{end_token})?)+)#{end_token}\[([:a-z_]*)\]/m Thanks again

face said 2008-01-09 13:21:

Good Stuff. Thanks Peter! A couple tweaks I made though: 1) In Gibberish, it is valid to say "foo bar"[]. That is the same as "foo bar"[:foo_bar]. To fix this in extractor.rb I changed the message_pattern to be: /#{start_token}((?:[^#{end_token}](?:\\#{end_token})?)+)#{end_token}\[:*([a-z_]*)/m and also added one line of code to add_message to handle empty keys: def add_messages(contents, start_token, end_token) contents.scan(message_pattern(start_token, end_token)).each do |text, key| key ='[ ]', '_').downcase if ( key == '' ) add_message(key, remove_quotes(text, end_token)) end end 2) The only other thing I changed was the unix find call, OpenBSD doesn't support -regex: `find #{dirs_to_search.join(" ")} -type f '(' -name '*rb' -or -name '*.ml' ')'` Thanks for the great utility, it makes Gibberish much easier...

Nigel said 2007-12-05 17:52:

Great plugin Peter. Thanks! I changed files_with_messages to pick up my haml and rhtml files ... `find #{dirs_to_search.join(" ")} -type f \\( -regex '.*ml' -or -regex '.*rb' \\)` Thanks again ... I'm sure this will be useful ....

Andy Stewart said 2007-11-29 03:41:

Useful! Thanks for this. It'll ease writing multi-lingual Gibberish.