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.
If you're doing this in a rake task, remember to include the enviroment, or otherwise it will not load your own locales which lives under
config/locales/
As per 8xx8's comment, a simpler version of:
is
This mitigates having to both preload the translations or specify the locale.
For the people wandering into this old question, there is a solution that does not require calling protected methods. Change your
yml
file as follows:Now you can simply extract all translations using
I18n.t("all")
, which has the benefit of automatically initializing and reloading the translations in development mode (something which doesn't happen if you call the protected methods.)If you are using
I18n::Fallbacks
unfortunately you can't useI18n.t('.')
as it just returns the contents current locale (eg. 'en-GB') and nothing from any of the fallback locales (eg 'en'). To get round this you can iterate over the fallbacks and usedeep_merge!
to combine them.The default I18n backend is I18n::Backend::Simple, which does not expose the translations to you. (I18.backend.translations is a protected method.)
This isn't generally a good idea, but if you really need this info and can't parse the file, you can extend the backend class.
You can then call
I18n.backend.translations_store
to get the parsed translations. You probably shouldn't rely on this as a long term strategy, but it gets you the information you need right now.You got to call a private method on the backend. This is how you get access: