Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve all translations from yml files in Rails I18n

I'm guessing that rails stores all the parsed translations yml files in a sort of array/hash. Is there a way to access this?

For example, if I've a file:

en:
  test_string: "testing this"
  warning: "This is just an example

Could I do something like, I18n.translations_store[:en][:test_string] ? I could parse the yml file with YAML::load, but in my case I've splitted the yml files in subfolders for organization, and I'm pretty sure that rails already parsed them all.

like image 654
Tiago Avatar asked Sep 29 '10 16:09

Tiago


2 Answers

You got to call a private method on the backend. This is how you get access:

translations = I18n.backend.send(:translations)
translations[:en][:test_string] # => "testing this"
like image 155
balu Avatar answered Nov 03 '22 05:11

balu


As per 8xx8's comment, a simpler version of:

I18n.t(:foo)
I18n.backend.send(:translations)[:en][:test_string]

is

I18n.t(".")[:test_string]

This mitigates having to both preload the translations or specify the locale.

like image 20
Eric H. Avatar answered Nov 03 '22 05:11

Eric H.