如何调试 Rails I18N 查找?

发布于 2024-12-10 18:37:15 字数 148 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

謸气贵蔟 2024-12-17 18:37:15

您可以在开发模式下对 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

孤星 2024-12-17 18:37:15

独立的 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

两相知 2024-12-17 18:37:15

上面的解决方案无助于找到正在查找密钥的文件。我没有找到任何优雅的解决方案,下面是我想出的最佳方法。这些说明必须针对生产盒进行调整。

  1. 打开 Rails 控制台 bundle execrails c
  2. 运行 I18n.load_path.join("\n") 并将其复制到剪贴板。如果您将 pry 与一些剪贴板助手一起使用,只需运行copy< /code> 在控制台中
  3. 打开一个新的终端窗口并运行 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.

  1. Open up a rails console bundle exec rails c
  2. Run I18n.load_path.join("\n") and copy this to your clipboard. If you use pry with some clipboard helpers, just run copy in the console
  3. Open up a new terminal window and run pbpaste | ack 'en.yml$' | xargs ack 'key:' This will print out a list of files containing the key I18n is trying to access
爱本泡沫多脆弱 2024-12-17 18:37:15

在 Rails 3.2(也可能是较低版本)中,视图中的 t - 帮助器会生成一个跨度,显示您在翻译中搜索了哪个键。这并不是适用于所有情况(来自控制器等)的解决方案,但我认为这可以是许多搜索这个问题的人的答案,其中上面的完整猴子补丁将位于顶部(猴子补丁也适用于 i18n 0.7.0 并提供更多细节)

title="translation missing: de.<path to key>"

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)

title="translation missing: de.<path to key>"
浪荡不羁 2024-12-17 18:37:15

如果您尝试调试的翻译是在控制器中设置的,则有一个简单的解决方案:

将控制器中的变量设置为不存在的标记,并读取翻译丢失警告返回的路径。

例如在控制器中

@test =  t('.choco_pizza')

在您看来:

<%= @test %>

将返回:

翻译缺失: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

@test =  t('.choco_pizza')

In your view:

<%= @test %>

Will return:

translation missing: fr.unexpected_namespace.controller_name.action_name.choco_pizza

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文