PJCHENder 未整理筆記

[RailsGuides] i18n

2018-08-28

[RailsGuides] i18n

@(Ruby on Rails)[Rails]

使用

1
2
3
# controller
I18n.t 'store.title'
I18n.l Time.now

Configuration

1
2
3
# ./config/initializers/i18n.rb
Rails.application.config.i18n.available_locales = [:"zh-TW", :en]
Rails.application.config.i18n.default_locale = :"zh-TW"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ./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

Tags: rails

掃描二維條碼,分享此文章