例如,如果我有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应用程序上使用i18n的YAML文件,值得注意的是
i18n
gem提供了一个帮助程序模块
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)
输出:
$ruby test.rb { "en.questions.new": "New Question","en.questions.other.recent": "Recent","en.questions.other.old": "Old" }