Tuesday, October 18, 2011

Addenda for AWDwR 4th beta chapter15

Agile Web Development with Rails 4th edition beta has a quality almost like final version. But, chapter15 (Internationalization) just has beta one. There are some stuck points. So I try to explain these points.

Configure default locale and available locales

In this book, author creates the file config/initializers/i18n.rb and configure default locale and locales for drop down list in this file. But, there are two better ways:
  1. This sample does not explain the code for available locales.
  2. There are some definitions for locale in config/application.rb file.
So that we should write below in config/application.rb file, not config/initializers/i18n.rb file:
# -*- coding: UTF-8 -*-
........
LANGUAGES = [
  ['English', 'en'],
  ['日本語'.html_safe, 'ja']
]


module Depot
  class Application < Rails::Application
........
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :en
    config.i18n.available_locales = LANGUAGES.map(&:last)

........
  end
end

default_url_options in controller

In this book, method default_url_options is inplemented in ApplicationController class. But, this brings two troublesome points.
  1. default_url_options would take an optional argument for hash.
  2. This works when server running or functional test running, but does not when integration test running.
So that we re-write these:
application_controller.rb
  def default_url_options(options={})
    options.merge({locale: I18n.locale})
  end
In each integration tests
  def setup
    app.default_url_options = { locale: I18n.locale }
  end

No comments:

Post a Comment