我在玩弄Ruby而且基本上都是这样
@trans = { :links => {
:quick_notes => "aaaaaaa"
}
}
Run Code Online (Sandbox Code Playgroud)
我想打电话
def t
#...something
end
t('links.quick_notes')
Run Code Online (Sandbox Code Playgroud)
访问
trans[:links][:quick_notes]
Run Code Online (Sandbox Code Playgroud)
我基本上试图实现与使用国际化时相同的功能
I18n.t('something.other.foo')
Run Code Online (Sandbox Code Playgroud)
我想出了这种方法
def t(key)
a=''
key.to_s.split('.').each{|key| a+="[:#{key}]" }
#now a == "[:links][:quick_notes]"
#but I cant figure out how can I call it on @trans variable
end
t('links.quick_notes')
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?感谢名单
mu *_*ort 13
你可以到达那里inject
:
def t(key)
key.to_s.split('.').inject(@trans) { |h, k| h[k.to_sym] }
end
Run Code Online (Sandbox Code Playgroud)
错误检查和"没有这样的条目"检查留作练习.