Emil Ahlbäck
Emil Ahlbäck

Reputation: 6206

Global attribute localization

I'm working with a legacy database schema which uses camelCase for its attributes, which Rails doesn't translate too well into human readable form. For one model I can define eg.

en:
  active_record:
    attributes:
      user:
        createDate: "Create date"

But that works only for the User model. Is there a way to define "global attribute translations"? Something like

en:
  activerecord:
    attributes:
      global: 
        createDate: "Create date"

Thanks you.

Upvotes: 2

Views: 806

Answers (1)

Chris Bailey
Chris Bailey

Reputation: 4136

Rails will lookup translations for attributes in the following places (if your attribute name is not namespaced):

  • activerecord.attributes.{model_name}.{attribute_name}
  • attributes.{attribute_name}

(take a look at the ruby docs for human_attribute_name)

so to achieve what you're looking for above you should use the following:

en:
  attributes:
    createDate: "Create date"

you can still override these 'default' attribute names by adding translations like your first example e.g.

en:
  active_record:
    attributes:
      user:
        createDate: "User creation date"

will result in:

User.human_attribute_name(:createDate) => "User creation date"
Topic.human_attribute_name(:createDate) => "Create date"
SomeOtherModelClass.human_attribute_name(:createDate) => "Create date"

Upvotes: 7

Related Questions