我按照正常方式组织我的翻译:
en:
foo:
bar: 'hello friend'
es:
foo:
bar: 'hola amigo'
我需要返回给定键的所有翻译的哈希值,其中语言作为哈希的键,格式如下:
# translations for foo.bar
{
en: 'hello friend',
es: 'hola amigo'
}
我该如何从i18n引擎中提取它?
我按照正常方式组织我的翻译:
en:
foo:
bar: 'hello friend'
es:
foo:
bar: 'hola amigo'
# translations for foo.bar
{
en: 'hello friend',
es: 'hola amigo'
}
如果有人感兴趣,我来回答自己的问题。这可能不是解决问题最有效的方法,但以下方法可以实现:
Hash[
I18n.available_locales.map{|locale|
[locale, I18n.translate('foo.bar', default: '', locale: locale)]
}
]
您确实可以使用I18n
来获取给定关键字的所有翻译:
key = 'hello'
I18n.available_locales.reduce({}) do |acc, locale|
acc[locale] = I18n.with_locale(locale) { I18n.t(key) }; acc
end