跳至主要内容

[Rails] i18n

使用

# controller
I18n.t 'store.title'
I18n.l Time.now

Configuration

# ./config/initializers/i18n.rb
Rails.application.config.i18n.available_locales = [:"zh-TW", :en]
Rails.application.config.i18n.default_locale = :"zh-TW"
# ./app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
before_action :set_locale

protected

def set_locale
if params[:locale] && I18n.available_locales.include?( params[:locale].to_sym )
session[:locale] = params[:locale]
end

I18n.locale = session[:locale] || I18n.default_locale
end
end

參考

Rails Internationalization (I18n) API @ RailsGuides