Last active
August 29, 2015 14:10
-
-
Save mimosa/c3505bd9422baed54635 to your computer and use it in GitHub Desktop.
Rails View 中 多语言的实践。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # app/views/orders/_filter_form.html.erb %> | |
<%= t '.provider' %> | |
<%= t '.start_at' %> | |
<%= t '.end_at' %> | |
<%= t '.no' %> | |
<%= t '.mobile' %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
module ApplicationHelper | |
# 针对 VIEWS 中,多语言的加强 | |
def t(key, opts = {}) | |
# VIEWS 相关 | |
if key =~ /^\./ | |
lookups = [] | |
paths = @virtual_path.split("/") | |
default = "defaults#{key}" | |
# 避免再次调用,scope_key_by_partial | |
# /actionview/lib/action_view/helpers/translation_helper.rb | |
key = (paths.join('.') + key) | |
# 根据路径找默认值 | |
total = paths.count | |
if total >= 2 # 路径大于2级 | |
-2.downto(-total).each do |i| | |
lookups << (paths[0..i] << default).join('.') | |
end | |
end | |
lookups << default # 全局默认 | |
# 最后(找/返回)指定(路径/字符串) | |
unless opts[:default].blank? | |
if opts[:default].is_a?(Array) | |
lookups += opts.delete(:default) | |
else | |
lookups << opts.delete(:default) | |
end | |
end | |
# | |
opts[:default] = lookups | |
end | |
super(key, opts).presence | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
# config/locales/views/zh.yml | |
zh: | |
defaults: | |
provider: '平台' | |
start_at: '开始时间' | |
end_at: '结束时间' | |
orders: | |
defaults: | |
mobile: '客人手机号' | |
filter_form: | |
'no': '订单号' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment