例如,如果我有 YAML 文件
en:
questions:
new: 'New Question'
other:
recent: 'Recent'
old: 'Old'
这最终会成为一个 json 对象,例如
{
'questions.new': 'New Question',
'questions.other.recent': 'Recent',
'questions.other.old': 'Old'
}
由于问题是关于在 Rails 应用程序上使用 YAML 文件进行 i18n,因此值得注意的是i18ngem 提供了一个辅助模块I18n::Backend::Flatten完全像这样展平翻译:
test.rb
:
require 'yaml'
require 'json'
require 'i18n'
yaml = YAML.load <<YML
en:
questions:
new: 'New Question'
other:
recent: 'Recent'
old: 'Old'
YML
include I18n::Backend::Flatten
puts JSON.pretty_generate flatten_translations(nil, yaml, nil, false)
Output:
$ ruby test.rb
{
"en.questions.new": "New Question",
"en.questions.other.recent": "Recent",
"en.questions.other.old": "Old"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)