Reputation: 3995
I have my translations organized in the normal fashion:
en:
foo:
bar: 'hello friend'
es:
foo:
bar: 'hola amigo'
I need to return a hash of all translations for a given key, with the language as the key of the hash, like this:
# translations for foo.bar
{
en: 'hello friend',
es: 'hola amigo'
}
How can I pull that from the i18n engine?
Upvotes: 4
Views: 3349
Reputation: 345
You can indeed use I18n
to pull all translations for a given key:
key = 'hello'
I18n.available_locales.reduce({}) do |acc, locale|
acc[locale] = I18n.with_locale(locale) { I18n.t(key) }; acc
end
Upvotes: 2
Reputation: 3995
Answering my own question, in case anyone is interested. Might not be the most efficient way to solve the problem, but this does it:
Hash[
I18n.available_locales.map{|locale|
[locale, I18n.translate('foo.bar', default: '', locale: locale)]
}
]
Upvotes: 5