LocalizationSimplified April 26, 2007

Posted by Slobodan Kovacevic in : Ruby & Rails , trackback

Rails is excellent in handling almost everything if you are building a site in English. Unfortunately, when it comes to localization (sites in other languages, UTF-8 support, etc.) Rails is not very well equipped to handle it.

There are localization plugins that make life easier, but most of them are rather complex, hard to use and have significant overhead (i.e. takes time to setup, learn, etc.). Fortunately, plugin called Localization Simplified aims to simplify localization as much as possible:

Fast and easy localization of one-language applications. Adds UTF-8 support for Ruby + database. Modifies ActiveRecord errors + html error helpers, Date/Time helpers, locale time formats, to_currency, to_sentence

It’s easy to use mainly because it only overrides existing Rails methods, so you don’t have to do anything special to use it.

Localization Simplified is a great plugin which you can download from Ruby Forge.

Comments»

1. bmihelac - April 26, 2007

What means ‘Adds UTF-8 support for Ruby + database.’? Isn’t just

encoding: UTF8

enough to be added in

database.yml

,

Is there way to translate custom strings or

LocalizationSimplified

only translates Rails messages?

2. Slobodan Kovacevic - April 26, 2007

Before I tried out the Localization Simplified I added only ‘encoding’ in database.yml and it worked for me. But I’ve read on the net that people had more problems with UTF-8 and that function such the one below (which has been taken from Localization Simplified) has helped them:

class ActionController::Base
   before_filter :configure_charsets

  def configure_charsets(charset='utf-8')
    # Set connection charset. MySQL 4.0 doesn't support this so it
    # will throw an error, MySQL 4.1+ needs this.
    suppress(ActiveRecord::StatementInvalid) do
      ActiveRecord::Base.connection.execute "SET NAMES 'UTF8'"
    end
  end
end

As far as I can see it only sets encoding for database communication, which obviously is what ‘encoding’ setting in database.yml should do. Perhaps it’s a left-over from previous Rails versions, i.e. when ‘encoding’ wasn’t available.

I am not sure what exactly you mean when you say “custom strings”, but it replaces ActiveRecord::Errors::@@default_error_messages, distance_of_time_in_words(), date, time and currency formats and few others.

3. bmihelac - April 26, 2007

By ‘custom strings’ I meant that plugin would handle messages your application PRODUCE and handle them in one YAML file or database (it uses YAML right?). For example in controller you may need to create message that “E-mail entered is already in use”. Next, you would need to handle translations of models and model fields.

Both Globalize-Rails and GetText offer these but are overkill for projects in one language only.

4. Slobodan Kovacevic - May 3, 2007

No, it doesn’t do that. I think that the author focused on Rails localization itself. You still need to handle “custom” localization yourself, i.e. while writing the code.