模型验证中的 Rails 国际化 (I18n):可能吗?

发布于 2024-10-07 22:18:28 字数 335 浏览 5 评论 0原文

我在模型中进行了以下验证:

validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever')

似乎翻译在生产模式下不起作用:在所有语言中,显示的始终是英语翻译(可能是因为我将英语设置为我的应用程序中的默认区域设置...?) 。

因此,我假设我们无法翻译模型中的验证,因为模型仅加载一次 - 当服务器启动时(然后将应用默认区域设置)。

我说得对吗?如果是,你会如何解决这个问题?

感谢您的帮助!

I have the following validation in a model:

validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever')

It seems that the translation does not work in production mode: in all languages it's always the english translation that gets diplayed (probably because I set english as the default locale in my app...?).

So I am assuming that we can't translate validations in models, because models get loaded only once - when the server is booted (and then, the default locale would be applied).

Am I right? If yes, how would you solve this problem?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

静待花开 2024-10-14 22:18:28

解决方案是不要在模型中包含任何自定义消息键,例如......

: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

The solution is to NOT include any custom message keys in the models, like...

:message => I18n.t('activerecord.errors.models.my_model.attributes.whatever.please_select_whatever')

The model will then apply the default message keys, for example ":inclusion" in the case of "validates_inclusion_of"

...and in config/locales/en.yml you need to have:

en:
  activerecord:
    errors:
      models:
        my_model:
          attributes:
            whatever:
              inclusion: "Please select whatever." # see default key: "inclusion"

for reference, check out the respective Rails guide:

http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

ゝ杯具 2024-10-14 22:18:28

您可以使用符号来指定翻译:

validates_inclusion_of :whatever, :in => [true, false], :message => :select_whatever

并且它将在特定范围内进行翻译。有关更多详细信息,请参阅I18n 指南

You can use symbols, to specify translations:

validates_inclusion_of :whatever, :in => [true, false], :message => :select_whatever

And it will be translated with a particular scope. See the I18n guide for more details.

最终幸福 2024-10-14 22:18:28

好的,iain 答案有效,但我花了很长时间才弄清楚应该将 :select_whatever 放在哪里。

validates_inclusion_of:无论如何,:in => [true, false], :message => :select_whatever

好的,您的 en.yml 应该如下所示:

en:
  errors:
    messages:
      select_whatever: "error!!"

OK, iain answer works, but I took very long time to figure out where should I put the :select_whatever.

validates_inclusion_of :whatever, :in => [true, false], :message => :select_whatever

OK your en.yml should look like this:

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