Skip to content

Localization Reference

scambra edited this page Sep 14, 2010 · 7 revisions

How it Works

ActiveScaffold facilitates localization by using the Object#as_() method:

<h4><%= as_('Are you sure?') -%></h4>

In the config block ActiveScaffold delays localization until after the session is known, so that you have the chance to know what the target locale should be. Assign the value to the config element:

active_scaffold :event do |config|
  config.columns[:address].label = 'Mailing Address'
end

and ActiveScaffold will call the Object#as_() method later.

How to Localize ActiveScaffold

Choose a localization plugin or gem. The Ruby on Rails website maintains a nice .rubyonrails.org/rails/pages/InternationalizationComparison of localization options. The .globalize-rails.org/globalize/ and the .gnu.org/software/gettext/ appear to be the most widely adopted options.

Then override Object#as_() and redirect control to the plugin or gem:

def as_(*args)
  # place custom localization handling here like
  _(*args)
end

Another Option

At one time ActiveScaffold did more than just facilitate localization. When it was decided to remove that code from core, it was captured in the .google.com/p/activescaffoldlocalize/. This plugin localizes all of the ActiveScaffold strings. It also demonstrates a very simple mechanism for localizing your application.

Bonus Localization

ActiveScaffold will also use the DATE_FORMATS[:default] string for dates and times, if present. More information can be found .rubyonrails.org/rails/pages/HowToDefineYourOwnDateFormat.

Rails 2.2 Localization

Rails 2.2 now localizes natively the as_() method. Therefore, your project will need to have a localization file in config/locales/en.yml (This would be the english one.) if locales does not exist, simply create it along with the language yml file.
The yml file could start something like this:

# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
  active_scaffold:
    cancel_button_label: "Return to List"
 

The cancel_button_label can be defined at your discrection and used wherever you wish. It it is important to note you will need to localize your column label overrides in the controller. Make sure that the yml file contains an entry matching your column label.

Example:

Conroller:
config.column[:company].label = "company_label"

en.yml:
company_label: "Company Name"
Clone this wiki locally