Rails:Model.human_attribute_name :field 在未找到翻译时应引发错误?(可能是由 state_machine 引起的?) [英] Rails: Model.human_attribute_name :field should raise an error when translation not found? (Maybe caused by state_machine?)
问题描述
我们经常在应用程序中偶然发现未翻译的模型属性.它们最常出现是因为属性被重命名或类似的东西.
We often stumble over untranslated model attributes in our application. They most often come because an attribute was renamed or something like this.
当 Model.human_attribute_name :field
找不到翻译时,让 I18n 引发错误真的很有帮助.有没有办法做到这一点?
It would be really helpful to have I18n raise an error when Model.human_attribute_name :field
doesn't find a translation. Is there a way to achieve this?
更新:
好像还有其他问题.这是我的 I18n 设置:
It seems there's some other problem. here are my I18n settings:
I18n.enforce_available_locales = false
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.default_locale = 'de-CH'
config.i18n.available_locales = ['de', 'de-CH', 'en']
config.i18n.locale = 'de-CH'
config.i18n.fallbacks = {'de-CH' => 'de', 'en-GB' => 'en'}
我无法设置 fallbacks = false
因为我希望 de-CH
的缺失翻译能够优雅地委托给 de
,这通常是似乎工作正常.但是对于我的 状态机 属性 human_to_state
方法,它似乎不起作用.这是导致问题的视图代码:
I can't set fallbacks = false
because I want missing translations of de-CH
to gracefully delegate to de
, which in general seems to work fine. But for my state machine attribute human_to_state
method it doesn't seem to work. Here's the view code causing the problem:
= f.input :state_event, collection: f.object.state_transitions,
label_method: :human_to_name # This causes the problem!
这在视图中打印为状态事件",当我添加以下 I18n 键时,它成功转换为状态":
This is printed out as "State event" in the view, and when I add the following I18n key, it's translated successfully to "Status":
德:蒙古人:属性:活动:state_event:状态
de: mongoid: attributes: activity: state_event: Status
所以确实是缺少翻译,但 I18n 并没有以任何方式抱怨.我还尝试使用自定义异常处理程序捕获异常,但这似乎没有引发:
So there really is a missing translation, but I18n doesn't complain in any way. I also tried to catch the exception using a custom exception handler, but this doesn't seem to be raised:
I18n.exception_handler = lambda do |exception, locale, key, options|
binding.pry # This is never reached!
end
知道发生了什么吗?是状态机的问题吗?
Any idea what's going on? Is it a problem with state machine?
推荐答案
问题在于human_attribute_name回退到
The problem lies in the fact that human_attribute_name falls back to
defaults << attribute.to_s.humanize
当没有其他发现时.换句话说,human_attribute_name 永远不会引发错误.
when nothing else found. In other words, human_attribute_name will never raise an error.
我通过覆盖 human_attribute_name 来修复"这个问题,修补上面提到的行:
I "fixed" this by overriding human_attribute_name, patching the above mentioned line:
(把它放在一个初始化器中)
(put this in an initializer)
require 'active_support/core_ext/hash/reverse_merge'
module ActiveModel
module Translation
include ActiveModel::Naming
def human_attribute_name(attribute, options = {})
defaults = lookup_ancestors.map do |klass|
[:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{attribute}",
:"#{self.i18n_scope}.attributes.#{klass.model_name.i18n_key.to_s.tr('.', '/')}.#{attribute}"]
end.flatten
defaults << :"attributes.#{attribute}"
defaults << options.delete(:default) if options[:default]
defaults << attribute.to_s.humanize if Rails.env.production? # Monkey patch
options.reverse_merge! :count => 1, :default => defaults
I18n.translate(defaults.shift, options)
end
end
end
这篇关于Rails:Model.human_attribute_name :field 在未找到翻译时应引发错误?(可能是由 state_machine 引起的?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!