RoR: i18n_template_selector
[ English | Japanese ]

News

November 10, 2007

  • Open (this) web page for RoR: i18n_template_selector.

Introduction

I know there have been many RoR plugins developed to add i18n feature to it, but I feel a little odd about them because they typically take some gettext-like approaches. I believe that gettext (or similar mechanism) should not be used to make MVC framework I18N-capable by reason that:

  • I18N is typically needed in some output functions.
  • You know C(ontroller) must not be involved in output, so gettext should not be used in C(ontroller).
  • V(iew) is employed to output by its nature, and I18n affects almost all the template, therefore it would be more effective to switch whole template file.
  • M(odel) may not need I18n in most cases.

i18n_template_selector switches template files themselves to make RoR I18N-capable.

Features

Key features of i18n_template_selector are:

  • RoR will become I18N-capable just after you install this plugin.
  • Existing RoR application would work without any modification, so you can add I18N template files gradually.
  • You can allow users to specify language with small code.

Environment

i18n_template_selector works well at least on RoR 1.2.5.

API & Basic Usage

i18n_template_selector adds only one method; AbstractRequest#accept_languages. This method returns a value of instance variable in AbstractRequest which is array of languages code like this:

request.accept_languages
=> [ "ja", "ko", "en" ]

This values are expected to be sorted in the order of descending priorities. This array is initialized according to Accept-Language in HTTP header when this method is called for the first time in a request.

You have to create template files, action.lang.rhtml, for each language you support in addition to standard template file, action.rhtml. When render is called, these template files are examined in the order of values in request.accept_languages. Suppose you've created WelcomeController#index, for instance, and view files corresponding to this action as following:

index.en.rhtml
index.ja.rhtml
index.rhtml

Your application will render index.ja.rhtml when request.accepted_languages returns [ "ja", "ko", "en" ]. If index.ja.rhtml does not exist, index.en.rhtml is rendered instead. Moreover when no files corresponding to languages in the array exist, standard template file, index.rhtml, is rendered.

Tips

How can I allow users to specify preferred language?

This can be archived by modifying request.accept_languages. For instance, the following code allows users to change template language to English by adding lang=en to the query string:

class ApplicationController < ActionController::Base
  before_filter :set_lang

  private

  def set_lang
    session[:lang] = params[:lang] if params[:lang]
    request.accept_languages.unshift(session[:lang]) if session[:lang]
  end
end

How can I set the default language?

Simply push the default language to the array like this:

class ApplicationController < ActionController::Base
  before_filter :set_default_lang

  private

  def set_default_lang
    request.accept_languages.push("en")
  end
end

In this case, you don't have to create action.rhtml as long as action.en.rhtml exists.

Download & Install

HTTP access to the SVN repository has not provided yet. Download tarball and expand it in RAILS_ROOT/vendor/plugins. That's all.