如何调试 Rails I18N 查找?
在 Rails 文档中,I18N 字符串似乎有不同的默认位置,具体取决于 I18N 查找是否是从视图、模型/验证、控制器、帮助器等发起的,如果它是标签等......
如何我看到 Rails 默认尝试在哪里查找内容,例如当我只使用 t('.something') 时?
In the Rails docs there seem to be different default locations for I18N strings, depending if the I18N-lookup was initiated from a view, model / validation, controller, helper, ..., if it's a label, etc...
How can I see where Rails is trying to lookup things by default, e.g. when I just use t('.something') ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在开发模式下对 I18N 后端进行猴子修补,以打印出在后端查找到的 I18n 键。
检查这里:
http://www.unixgods.org/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
You can monkey patch the I18N backend in development mode to print out the I18n keys that are looked up in the backend.
Check here:
http://www.unixgods.org/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
独立的
I18n.t
不会以任何方式为您的翻译密钥添加前缀,以下是负责 Rails 魔力的辅助方法/模块:(单击方法下方的“源”链接描述以查看内部发生的情况)
ActionView:
http://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-it
scope_key_by_partial
ActiveModel:
http://api.rubyonrails.org/classes/ActiveModel/Translation .html#method-i-i18n_scope
AbstractController
http://api.rubyonrails.org/classes/AbstractController/Translation.html
the standalone
I18n.t
does not prefix your translation key in any way, here are the helper methods/modules that are responsible for the rails' magic:(click on the "source" link below the methods' description to see what's happening inside)
ActionView:
http://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-i-t
scope_key_by_partial
ActiveModel:
http://api.rubyonrails.org/classes/ActiveModel/Translation.html#method-i-i18n_scope
AbstractController
http://api.rubyonrails.org/classes/AbstractController/Translation.html
上面的解决方案无助于找到正在查找密钥的文件。我没有找到任何优雅的解决方案,下面是我想出的最佳方法。这些说明必须针对生产盒进行调整。
bundle execrails c
I18n.load_path.join("\n")
并将其复制到剪贴板。如果您将 pry 与一些剪贴板助手一起使用,只需运行copy< /code> 在控制台中
pbpaste | ack 'en.yml$' | xargs ack 'key:'
这将打印出包含 I18n 尝试访问的密钥的文件列表The solution above does not help find what file a key is being looked up in. I did not find any elegant solution to this, below is the best method I came up with. The instructions would have to be adapted for a production box.
bundle exec rails c
I18n.load_path.join("\n")
and copy this to your clipboard. If you use pry with some clipboard helpers, just runcopy
in the consolepbpaste | ack 'en.yml$' | xargs ack 'key:'
This will print out a list of files containing the key I18n is trying to access在 Rails 3.2(也可能是较低版本)中,视图中的 t - 帮助器会生成一个跨度,显示您在翻译中搜索了哪个键。这并不是适用于所有情况(来自控制器等)的解决方案,但我认为这可以是许多搜索这个问题的人的答案,其中上面的完整猴子补丁将位于顶部(猴子补丁也适用于 i18n 0.7.0 并提供更多细节)
In rails 3.2 (maybe also lower versions), a span is produced by the t - helper in views that shows you which key was searched for the translation. This isn't a solution for all cases (from controller and so on), but I think it can be the answer for a lot of people who search for this question, where the full monkey patch from above would be over the top (the monkey patch also works for me in i18n 0.7.0 and gives more detail)
如果您尝试调试的翻译是在控制器中设置的,则有一个简单的解决方案:
将控制器中的变量设置为不存在的标记,并读取翻译丢失警告返回的路径。
例如在控制器中
在您看来:
将返回:
翻译缺失:fr.unexpected_namespace.controller_name.action_name.choco_pizza
If the translation you're trying to debug is being set in the controller, there is a simple solution:
Set a variable in the controller to a tag that's doesn't exist and read the path return by the translation missing warning.
For instance in the controller
In your view:
Will return:
translation missing: fr.unexpected_namespace.controller_name.action_name.choco_pizza