【问题标题】:How do I specify shared attributes translations between models in I18n locale file?如何在 I18n 语言环境文件中指定模型之间的共享属性转换?
【发布时间】:2011-12-07 11:34:21
【问题描述】:

我尝试了以下方法:

es:
  activerecord:
    attributes:
      name: Nombre

但它没有用。

但以下方法确实有效:

es:
  activerecord:
    attributes:
      person:
        name: Nombre

在模型间定义默认属性的正确方法是什么?

我也在使用 Formtastic 和 Active Admin。

【问题讨论】:

    标签: ruby-on-rails activerecord internationalization locale models


    【解决方案1】:

    将通用attributes 上移一级:

    es:
      attributes:
        name: Nombre
      activerecord:
        attributes:
          user:
            birthday: Etc
    

    Example

    【讨论】:

    • 这应该是选择的答案。
    【解决方案2】:

    这就是我的做法。不完美,但可以胜任。

    activerecord:
      attributes:
        attribute_commons: &commons
          name: Nome
          description: Descrição
        user:
          <<: *commons
        role:
          <<: *commons
          level: Nível
        product:
          <<: *commons
          size: Tamanho
    

    【讨论】:

    • 这种方法很有用,但如果它适用于某些但不是所有模型。
    • 请查看 Halil Özgür 的答案,这是正确的答案。
    【解决方案3】:

    如果您使用 yaml 来存储翻译,您应该在 yaml 引用中查找别名和锚点: http://yaml.org/spec/1.2/spec.html

    但它一点也不完美。例如:

    bill-to:  &id001
        street: |
                123 Tornado Alley
                Suite 16
        city:   East Centerville
        state:  KS
    
    ship-to:  *id001
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2012-03-12
      • 1970-01-01
      • 2011-07-24
      相关资源
      最近更新 更多