Rails on the Run

Rails experiments by Matt Aimonetti

Browsing Posts tagged i18n

Today I updated Globalite, refactoring the code and fixing some issues.

I added a bunch of aliases since I was tired of typing:

Globalite.current_language = :fr

I shortened it to

Globalite.language = :fr

But the old method is still available so it won’t break your code ;)

The major change with this update is a better support for locales. Let’s say that in our application we setup the language to :fr (French) but we didn’t specify a country. Remember that Globalite lets you have specific translations/localizations per country so you can display dates, prices properly.

Previously, if my locale was set to :fr-* then Globalite would try to load the fr.yml files and not fr-FR.yml.

If you decided to use a fr.yml file for your UI localization, you might have noticed that all the Rails core messages weren’t translated(now you know why). This update deals with this issue and if a translation isn’t found in your locale, it will look for translations in your language but from a different country and pick a random one.

I’m expecting Rails core translations in Japanese, Korean and Chinese. Feel free to email me your own file and let me know on what project you used Globalite.

I’ve been recently slightly annoyed by the lack of simple yet powerful internationalization/localization plugin for Rails.
It seems like the only real solution out there is Globalize

However, it’s a heavy, bloated, hard to setup plugin. I might seem to complain, but I’m actually glad that someone came up with a decent solution. Globalize is quite powerful but I would like to see something simpler, lighter and easier to use.

While I was looking for alternate solutions, Err released Gibberish a simple and nice localization plugin.

Looking at Chris’ code, I found a clean and nice solution but unfortunately too simple. The major problem I had with Giberrish was the lack of support for locales. That means that one cannot create an application supporting British English and American English. I lived in both America and the UK and believe me, I can never spell some words properly. How should I spell the following words: colours or colors, behaviour or behavior, aluminium or aluminum? On top of that, your UI might be slightly different based on the region your visitor is coming from. Localization is more than translation.

Anyway, I decided to come up with my own solution, a breed of the best features from my favorite i18n/l10n projects. This hybrid solution should, hopefully be useful for people not needing all the resources of Globalize. I called my new project Globa_lite_

What’s cool with Globalite?

  • Simple UI localization

    • support different locales
    • yml file based
    • simple syntax
    • no setup needed
    • enough for most projects needing localization
    • pass variables to the localization and let the translator deal with it
  • Simple Rails Localization

    • support different locales
    • nothing to do, rails comes in your own language (including currency conversion and other helpers)
    • yml file based localization for easy update
    • support for Rails 1.2x
    • simple to maintain
    • simple to add/edit a new locale
  • Simple content i18n/l10n (coming soon)

UI localization:

  • Create a lang/ui folder at the root of the folder.
  • create 2 localization files: fr.yml and en-US.yml
  • add localization keys in the files:

fr.yml:

hello: bonjour

welcome: bienvenue

en-US.yml

hello: howdy

welcome: welcome
  • Declare the current locale or language

    Globalite.current_language :fr
    
  • In your view (or anywhere else) localize a key

    :hello.l
    

or

:hello.localize

you can also do

:unknown_key.l("this is the default text to display if the localization isn't found")

Also you can pass one or many variables to the localization(check the documentation)

Rails Localization

Rails is automatically localized for you (meaning most helpers, including the currency converter are localized for the locale you declared).
You really easily add a new supported rails language/settings by adding a yml file or editing an existing one. (for now, I only create an English and a French localization file, but I’ll add plenty more very soon)

Globalite also saves the locale in the session allowing many users to see a site at the same time in different languages :)

I believe Globalite is/will be a good solution for most of my international projects. It still lacks the content i18n and l10n as well as support for pluralization but it should be added soon.

Give it a try and let me know what you think:

RubyForge page

svn repository: svn checkout svn://rubyforge.org/var/svn/globalite/trunk globalite

view repository content

ps: The project is still in beta and will change during the next few weeks, feel free to submit patches, ideas, suggestions…