よたらぼ:locale_rails-2.0.0を用いて、i18nを使う時のメモ。
まずインストール。
# gem1.8 install locale_rails
よたらぼ:Ruby-Locale for Ruby on Rails HOWTOに従い、i18nをしていないアプリケーションをi18n化する。
config/initializer/locale.rbに以下を転記。
# in config/initializer/locale.rb # Tell the I18n library where to find your translations I18n.load_path += Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ] # Tell the supported locales. This is the one of additional setting by Ruby-Locale for Ruby on Rails. # If supported_locales is not set, the locale information which is given by WWW browser is used. # This setting is required if your application wants to restrict the locales. I18n.supported_locales = Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ].collect{|v| File.basename(v, ".*")}.uniq # Tell the default locale. If this value is not set, "en" is set. # With this library, this value is used as the lowest priority locale # (If other locale candidates are not found, this value is used). I18n.default_locale = "ja-JP" # For single language application, set Locale.default with I18n.default_locale. # Locale.default forces to use this locale in Ruby-Locale. I18n.default_locale = "ja-JP" #Locale.default = "ja-JP"
config/environment.rbに以下を追加し、locale_railsを読み込む。
# config/environment.rb Rails::Initializer.run do |config| : : config.gem 'locale' # add config.gem 'locale_rails' # add end
テンプレートがindex.html.erbなら、index.ja-JP.html.erbを置けば自動的に置き換えられる。layout以下のファイルも同様。