gir*_*aff 6 debugging ruby-on-rails internationalization
当Rails函数要求翻译(I18n.translate)时,我不想分析他们的代码以获得确切的范围等.
如何为每个要求的字符串添加调试输出到控制台?
例子:
I18n.t 'errors.messages.invalid', :scope => :active_record
# Translation for 'activerecord.errors.messages.invalid' (not) found
label(:post, :title)
# Translation for 'activerecord.attributes.post.title' not found
# Translation for 'views.labels.post.title' not found
Run Code Online (Sandbox Code Playgroud)
这不是一个非常优雅的解决方案,但它对我有用.我创建了一个初始化器:
require 'i18n'
if (Rails.env.development? || Rails.env.test?) && ENV['DEBUG_TRANSLATION']
module I18n
class << self
def translate_with_debug(*args)
Rails.logger.debug "Translate : #{args.inspect}"
translate_without_debug(*args)
end
alias_method_chain :translate, :debug
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后,您可以运行如下命令:
$ DEBUG_TRANSLATION=true rake cucumber
Run Code Online (Sandbox Code Playgroud)
...并且您将看到所有正在尝试的翻译被转储到STDOUT.我不考虑这个生产代码,所以我把它保存在Gist中,而不是在这个阶段将它检查到我的主项目源代码控制中.
Noddy,但它完成了这项工作.
| 归档时间: |
|
| 查看次数: |
602 次 |
| 最近记录: |