如何从 Rails I18n 中的 yml 文件中检索所有翻译 [英] How to retrieve all translations from yml files in Rails I18n
问题描述
我猜 Rails 将所有已解析的翻译 yml 文件存储在一种数组/哈希中.有没有办法访问它?
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
我可以做类似 I18n.translations_store[:en][:test_string] 的事情吗?我可以使用 YAML::load 解析 yml 文件,但在我的情况下,我已将 yml 文件拆分为子文件夹以进行组织,而且我很确定 rails 已经将它们全部解析了.
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.
推荐答案
你必须在后端调用一个私有方法.这是您获得访问权限的方式:
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"
这篇关于如何从 Rails I18n 中的 yml 文件中检索所有翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!