Rails国际化的关键步骤。Rails I18n Key Steps.
Rails I18n的关键点
1、设定locales文档。
config/locales/en.yml
------------------------------
en:
hello: "Hello world"
store: "Store"
------------------------------
config/locales/zh-CN.yml
------------------------------
zh-CN:
hello: "你好"
store: "商店"
------------------------------
2、解析locale。
app/controller/application.rb
------------------------------
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || 'zh-CN' #如果没有,则使用默认值。
end
...
end
------------------------------
3、在控制器中读取变量
------------------------------
class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
flash[:hello_flash] = t(:hello_flash)
end
...
end
------------------------------
4、在视图层中使用变量
------------------------------
<% if flash[:hello_flash] -%>
<div id="hello_flash"><%= flash[:hello_flash] %></div>
<% end -%>
------------------------------
--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------
1、设定locales文档。
config/locales/en.yml
------------------------------
en:
hello: "Hello world"
store: "Store"
------------------------------
config/locales/zh-CN.yml
------------------------------
zh-CN:
hello: "你好"
store: "商店"
------------------------------
2、解析locale。
app/controller/application.rb
------------------------------
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || 'zh-CN' #如果没有,则使用默认值。
end
...
end
------------------------------
3、在控制器中读取变量
------------------------------
class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
flash[:hello_flash] = t(:hello_flash)
end
...
end
------------------------------
4、在视图层中使用变量
------------------------------
<% if flash[:hello_flash] -%>
<div id="hello_flash"><%= flash[:hello_flash] %></div>
<% end -%>
------------------------------
--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------