在哪里存储模型属性值翻译

发布于 2024-11-03 19:24:59 字数 588 浏览 1 评论 0原文

我有一个带有属性 payment_status 的模型发票。 payment_status 具有固定值 unpayed|partial_payed|paid 我想将其翻译存储在语言环境文件中。 我认为将它放在模型本地文件中会很好

de:
  activerecord:
    attributes:
      payment_status: Zahlstatus
      payment_status_values:
        unpayed: offen
        partial_payed: teilgezahlt
        payed: ausgeglichen

,现在我可以获取最后一张发票的翻译后的 payment_status-value

I18n.t Invoice.last.payment_status , :scope => "activerecord.attributes.invoice.payment_status_values"
=> "offen"

对我来说,它看起来像输入了很多 sopes,是否有一个范围内的方法来获取翻译或者有更好的方法来做到这一点?

I have a model Invoice with attribute payment_status. payment_status has fixed values unpayed|partial_payed|payed that I want to store the translations for in a locale-file.
I thougt it would be good to have it in the model local-file

de:
  activerecord:
    attributes:
      payment_status: Zahlstatus
      payment_status_values:
        unpayed: offen
        partial_payed: teilgezahlt
        payed: ausgeglichen

now I can get the translated payment_status-value for the last invoice like this

I18n.t Invoice.last.payment_status , :scope => "activerecord.attributes.invoice.payment_status_values"
=> "offen"

to me it looks like typing sopes a lot, is there maybe a scoped method to get the translation or a better way to do this at all?

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

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

发布评论

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

评论(1

聆听风音 2024-11-10 19:24:59

我们使用了 marcel 的 easy_enums 插件。我发现的最接近的是: https://github.com/mschuerig/easy_enums/

语法是这样的。然后您只存储范围标识符的最后一部分。

  has_enum :shipping_mode, :default => :not_set, :fallback => :not_set do
    value :not_set
    value :address
    value :self_collect
    define_method(:localize) { I18n.t("models.payment.shipping_mode.#{self.id}") }
  end

这达到你的目标了吗?

We used the easy_enums plugin from marcel. The closest I found was: https://github.com/mschuerig/easy_enums/

Syntax is like this. Then you store only last part of the scope identifier.

  has_enum :shipping_mode, :default => :not_set, :fallback => :not_set do
    value :not_set
    value :address
    value :self_collect
    define_method(:localize) { I18n.t("models.payment.shipping_mode.#{self.id}") }
  end

Does that hit your goal?

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