1

有没有办法通过 Rails 控制台手动注入翻译?

假设我在开发环境中工作,并且我想在生产控制台中测试一些代码(例如,测试一些真实数据的统计数据)。问题是,我要测试的代码依赖于不存在的新翻译(或已更改)在生产环境中。

所以我的代码返回了很多translation_missing

我可以注入缺少的翻译吗?通过哈希或 YML 文件?

I18n.load_translations(hash_or_file) 
4

1 回答 1

1

Usually Application instances that serve http requests (for example running under Unicorn/Puma) are not available via Rails console. When someone login to production server and type $RAILS_ENV=production rails c it starts another application process. Translations dictionary is a kind of in-memory cache and usually it is not possible to change that cache for/from another process (in general). You can reload translations only for application instance that started by Rails console, but not for running server.

Only one way to hot reload translations is adding kind of a hook into source code of application to re-read YAML file, but it seems better just restart application server.

UPDATE: For testing purposes I18n cache could be modified like:

I18n.backend.send(:translations)[:en][:date][:formats][:default] = "%Y-%Z"
于 2016-10-27T08:45:51.443 回答