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:- This sample does not explain the code for available locales.
- There are some definitions for locale in config/application.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.- default_url_options would take an optional argument for hash.
- This works when server running or functional test running, but does not when integration test running.
application_controller.rb
def default_url_options(options={})In each integration tests
options.merge({locale: I18n.locale})
end
def setup
app.default_url_options = { locale: I18n.locale }
end
No comments:
Post a Comment