Click To Globalize: Ready for Rails 2.1!

Posted by luca
on Monday, July 21

Click to Globalize is ready for Rails 2.1!

I spent a lot of time to make this release the best of ever! I know that a lot of time has passed since the latest Rails major release, and I apologize for this.
The official Globalize it seems dead, but the great work of Nate Clark (aka heythisisnate) has made it compatible with Rails 2.1. Thanks Nate!
The Rails Edge now includes a basic support for i18n!

What's Changed?

The first big change is about the configuration, past versions has cluttered a bit your ApplicationController, I know, but now Click uses a YAML file configuration file for locales.

As you can see, it's more readable than put an Hash inside your main controller, and it also take the advantage to separate configuration from logic.

The second change regards the default language, you can use the above file to declare it, or use the Globalize syntax (Globalize::Locale.set_base_language('en-US')), but you have to choose a locale before the app starts.
This because Click needs a secure fallback mechanism, if no active Locale was set, it's always guarantees your application to work.

The third feature is about formatting: you can always choose between textile and markdown without editing ApplicationController, but using the same configuration file.

I removed the deprecated class method ApplicationController#globalize? in favor of the instance one. This means you can use it to turn on/off Click to Globalize, just returning a boolean value:

This version also includes tiny fixes, refactoring and cleanup.




You may wish to learn a bit about Click to Globalize, so I encourage you to visit the official page.
I hope you enjoy your i18n on Rails!

What's New In Rails Edge: i18n 1

Posted by luca
on Friday, July 18

Ruby on Rails has just integrated a basic support for i18n.

ActiveSupport

ActiveSupport now includes the i18n gem which provides the API and the settings for the default locale: en-US.
The gem abstracts the repository where the translations are stored, so all the plugin authors could write their own mechanism. The bundled repository is called Simple and stores all the settings in memory.

Declaring a locale is quite easy:

I18n.backend.store_translations :'it-IT', {
  :support => {
    :array => {
      :sentence_connector => 'e'
    }
  },
  :date => {
    :formats => {
      :default => "%d/%m/%Y",
      :short => "%d %b",
      :long => "%d %B %Y",
    },
    :day_names => %w(Lunedì Martedì Mercoledì Giovedì Venerdì Sabato Domenica),
    :abbr_day_names => %w(Lun Mar Mer Gio Ven Sab Dom),
    :month_names => %w(Gennaio Febbraio Marzo Aprile Maggio Giugno Luglio Agosto Settembre Ottobre Novembre Dicembre).unshift(nil),
    :abbr_month_names => %w(Gen Feb Mar Apr Mag Giu Lug Ago Set Ott Nov Dic).unshift(nil),
    :order => [:day, :month, :year]
  },
  :time => {
    :formats => {
      :default => "%a, %d %b %Y %H:%M:%S %z",
      :short => "%d %b %H:%M",
      :long => "%B %d, %Y %H:%M",
    },
    :am => 'am',
    :pm => 'pm'
  }
}

How can I translate or localize?

I18n.locale = 'it-IT'
I18n.t :hello   # => Ciao
I18n.l Time.now # => "Ven, 18 Lug 2008 10:58:14 +0200"

I18n#t is also useful to fetch locale defaults:
I18n.t :'time.formats.short' # => %d %b %H:%M

ActiveRecord

ActiveRecord now returns localized error messages for validations.

You may wish to declare your messages:

I18n.backend.store_translations :'it-IT', {
  :active_record => {
    :error_messages => {
      :inclusion => "non è incluso nella lista"
      # ...
    }
  }
}

ActionPack

ActionView now supports translations and localization for time and currency helpers (i.e. distance_of_time_in_words, number_to_currency).


UPDATE 2008-07-19: Sven Fuchs wrote a i18n Rails manifesto and a technical post about the i18n API.

Click to Globalize: rewritten and moved to GitHub

Posted by luca
on Monday, May 12

Click to Globalize has been rewritten, in order to use all instance methods of the JavaScript class, instead of class methods.

I also fixed some tiny issues:

  • The form_authentication_token method is called only if the application is protected against the CSRF attacks (protect_against_forgery? returns true)
  • It uses as parameter key what request_forgery_protection_token returns, instead of the hard-coded authenticity_token
  • The JavaScript class uses Prototype's dom:loaded custom event.

I also migrated the plugin GitHub. The new repo is http://github.com/jodosha/click-to-globalize/tree/master, so the oldest one is deprecated.
Have a nice globalization!

If you enjoyed this post, feel free to recommend me on Working With Rails.

Click To Globalize 0

Posted by luca
on Saturday, January 26

Click to Globalize

Click to Globalize is an extension for Globalize plugin, it allows to edit in place globalized labels. With this plugin you don't have to create a globalization back-end, but just edit your interface in place!!

If you have a previous globalization experience, you probably noticed that the main problem of dedicated back-ends is the lack of the context where the string will be placed in. It isn't a trivial issue: you'll never know if the meaning of your string is harmonious with other labels and how your string will be rendered, until the page load.

Now you can forget this issues, you can directly globalize in place!!

Getting Started

  1. Install Globalize 1, if you don't already done.
  2. Install Click to Globalize:
    $ ./scripts/plugin install git://github.com/jodosha/click-to-globalize.git
  3. Run the setup task:
    $ rake click:setup
  4. Edit config/click.yml
  5. Add at the end of your layout the following code:
    <%= click_to_globalize -%>
  6. Put in your view:
    <%= 'hello_world'.t %>
  7. Start the server and click to globalize.

Prerequisites

  • Globalize 1 plugin: www.globalize-rails.org.
  • Prototype >= 1.5.1.1, this version was bundled in the outdated versions.
  • RedCloth gem (optional) for textile formatting.
  • BlueCloth gem (optional) for markdown formatting.

Install

NOTE the following instructions are for Click to Globalize v0.4.0 (Rails 2.1.x), for older version, please check your README.

  1. Install Globalize 1, if you don't already done.
  2. Install Click to Globalize:
    Rails 2.1.x
    $ ./script/plugin install git://github.com/jodosha/click-to-globalize.git
    Rails 2.0.x
    $ ./script/plugin install http://dev.23labs.net/svn/rails/plugins/click_to_globalize/trunk
    Rails 1.2.x
    $ ./script/plugin install http://dev.23labs.net/svn/rails/plugins/click_to_globalize/branches/for-1.2.x
  3. Run the setup task:
    $ rake click:setup
  4. Run the tests (optional):
    $ rake click:test
  5. Edit config/click.yml, adding your locales (including the default one), and, optionally, the formatting style:
  6. locales:
      english: en-US
      italian: it-IT
    
    #optional
    default: :english
    
    # optional
    formatting: textile

    NOTE: Make sure to choose a default locale before the app starts.
  7. Personalize the access to the globalization features, overriding #globalize:
    class ApplicationController < ActionController::Base
      def self.globalize?
        current_user.admin?
      end
    end

    NOTE: #globalize? is method to turn on/off all the Click to Globalize features.
  8. Add at the end of your layout the following code:
    <%= click_to_globalize -%>

Uninstall

$ ./script/plugin remove click_to_globalize

Features

  • In-place editing for each globalized string.
  • Easy and painless plug-in/plug-out process.
  • Unobtrusive Javascript.
  • Auto transformation from input text to textarea for long strings.

Common Issues

  • The install process (for Rails 1.2.x) upgrades your Prototype version to 1.5.1.1 only if needed, but it's advisable to backup your scripts.
  • Due to unobtrusive nature of the plugin, each page is parsed by javascript to find the right element and bind to it an Ajax.InPlaceEditor.
    Be sure your (X)HTML is wellformed.
    NOTICE: If you use the Rails 1.2.x scaffold system, Click to Globalize doesn't works. Cleanup the code before to use.
  • If you use a wiki-formatting style it could be advisable to use #h to avoid security problems (i.e. XSS).
    <%= textilize(h('some_text'.t)) %>
  • The official Globalize repository is still not compatible with Rails 2.1, use the heythisisnate's fork instead.

Contribute

  • Check out the code and test it:
    $ git clone git://github.com/jodosha/click-to-globalize.git
    $ rake click
  • Create tickets at the Sushistar Lighthouse Page.
  • Create a patches and add as attachement to the tickets.

Repository

http://github.com/jodosha/click-to-globalize/tree/master

Credits

Javascript tests are based on Prototype test libs.

Vote

If you find it useful feel free to add to your favs on agilewebdevelopment.com.

Copyright

Copyright © 2007 - 2008 http://lucaguidi.com, released under the MIT license.

Tutorials

  • Getting Started --high resolution
    Click to Globalize: Getting Started [High Resolution]
  • Getting Started
  • Restrict The Access
  • Text Area Transformation
  • Formatting
  • How to Prevent XSS

Click To Globalize: Rails 2.0 Ready

Posted by luca
on Friday, December 14

I have finished to work on Click To Globalize, to made it Rails 2.0 compatible.

What's Changed?

All and nothing: from the user point of view, the plugin has the same behaviors of the previous version. My recent activity was a refactoring, now it:

  • Works with CSRF Killer
  • Works with Prototype 1.6.0.1 and Scriptaculous 1.8.0.1
  • Works with rewritten version of Scriptaculous Ajax.InPlaceEditor
  • Works with new Prototype events handling
  • Uses new Prototype's Element#addMethods and Function#wrap to add methods and AOP
  • Uses Protoype Hash#get, instead of square brackets
  • Uses .html.erb as helper, instead of .rhtml
  • Has a more clean installation/disinstallation process
  • Has DRYed up tests
  • Hasn't prototype.js into the packaging

How To Use It?

Rails 2.0

Rails 1.2.x

For a detailed guide, howtos, snippets, video-tutorials and other infos, please visit the Click To Globalize page.

Click To Globalize: Repository Changes

Posted by luca
on Sunday, December 09

As I previously announced, I'm working on Rails 2.0 compatibility for Click To Globalize.

I decided to move the stuff for Rails 1.2.x to for-1.2.x branch and continue the porting into trunk.

How To Install

Rails 2.0

Rails 1.2.x

Click To Globalize: Working On Rails 2.0 Compatibility

Posted by luca
on Sunday, December 09

As you already know, Rails 2.0 it's shipped with updated version of Prototype and Scriptaculous.

Prototype team has committed some breaking changes (e.g. Hash class), and Scriptaculous now has a brand new, rewritten InPlaceEditon.

Those elements are fundamental for Click To Globalize and I'm working on new version for Rails 2.0.

Be patient, I'll soon release it.