我在模型中进行了以下验证:
validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever')
翻译似乎在生产模式下不起作用:在所有语言中,显示的始终是英语翻译(可能是因为我将英语设置为我的应用程序中的默认区域设置......?)。
所以我假设我们无法翻译模型中的验证,因为模型仅加载一次 - 当服务器启动时(然后将应用默认区域设置)。
我说得对吗?如果是,您将如何解决这个问题?
感谢您的帮助!
最佳答案
解决方案是不要在模型中包含任何自定义消息键,例如......
:message => I18n.t('activerecord.errors.models.my_model.attributes.whatever.please_select_whatever')
然后模型将应用默认消息键,例如“validates_inclusion_of”中的“:inclusion”
...并且在 config/locales/en.yml 中您需要:
en:
activerecord:
errors:
models:
my_model:
attributes:
whatever:
inclusion: "Please select whatever." # see default key: "inclusion"
作为引用,请查看相应的 Rails 指南:
http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
关于ruby-on-rails - 模型验证中的 Rails 国际化 (I18n) : Possible or not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4451076/