存储模型属性值转换的位置

时间:2011-04-27 15:54:22

标签: ruby-on-rails internationalization translation

我有一个带有属性payment_status的模型发票。 payment_status有固定值unfayed | partial_payed | payed我想将翻译存储在locale-file中。 我认为将它放在模型local-file

中会很好
de:
  activerecord:
    attributes:
      payment_status: Zahlstatus
      payment_status_values:
        unpayed: offen
        partial_payed: teilgezahlt
        payed: ausgeglichen

现在我可以获得最后一张发票的翻译payment_status值,如下所示

I18n.t Invoice.last.payment_status , :scope => "activerecord.attributes.invoice.payment_status_values"
=> "offen"
对我来说,它看起来像打字很多,是否有可能采用范围方法来获得翻译或更好的方法来完成这项工作?

1 个答案:

答案 0 :(得分:0)

我们使用了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

这会达到你的目标吗?

相关问题