如何从Rails I18n中的yml文件中检索所有翻译 [英] How to retrieve all translations from yml files in Rails I18n

查看:125
本文介绍了如何从Rails I18n中的yml文件中检索所有翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜想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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆