Peter Marklund

Peter Marklund's Home

Tue November 27, 2007
Programming

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.

Comments

Andy Stewart said over 6 years ago:

Useful!

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

--------------------------------------------------------------------------------

Nigel said over 6 years ago:

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' \\)`.split.map(&:chomp)

Thanks again ... I'm sure this will be useful ....

--------------------------------------------------------------------------------

face said over 6 years ago:

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 = text.tr('[ ]', '_').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' ')'`.split.map(&:chomp)

Thanks for the great utility, it makes Gibberish much easier...

--------------------------------------------------------------------------------

face said over 6 years ago:

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 over 6 years ago:

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: http://myutil.com/2008/1/23/prototype-translations-for-gibberish-with-google-language-tools-a-mouse-click-and-13-lines-of-code

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.

--------------------------------------------------------------------------------

bob said over 6 years ago:

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?

--------------------------------------------------------------------------------

bob said over 6 years ago:

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

--------------------------------------------------------------------------------

Jeff Jones said over 6 years ago:

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?

--------------------------------------------------------------------------------