JacobEvelyn
JacobEvelyn

Reputation: 3971

Rails engine ActiveRecord models I18n

I have a Rails app that depends on a separate Engine (stored in vendor/engine_name). That engine has an ActiveRecord object Bar:

module Foo
  class Bar < ActiveRecord::Base
    # has an attribute bar_attr
  end
end

In that engine's config/locales/en.yml file, I've tried:

en:
  activerecord:
    attributes:
      bar_attr: "TEST"

I've also tried:

en:
  activerecord:
    attributes:
      bar:
        bar_attr: "TEST"

and:

en:
  activerecord:
    attributes:
      foo:
        bar:
          bar_attr: "TEST"

But no matter what, when I call Foo::Bar.human_attribute_name("bar_attr") from the parent app, I get "Bar attr" (e.g. the default human attribute name). Note that the same problem occurs with Foo::Bar.model_name.human when I try translations using:

en:
  activerecord:
    models:
      ...

I'm not sure if the app/engine structure is relevant, as I've tried the above three en.yml formats within the parent app's translations file too, with no luck.

What am I missing to get these model names/attributes to translate correctly?

Upvotes: 3

Views: 1792

Answers (1)

richard
richard

Reputation: 1585

It took a bit of searching but I found an answer to this in another question Inheriting Rails i18n validation error messages in the subclass as I was also trying to get i18n working for ActiveRecord in a Rails engine.

Based on the example above, the answer is probably something like,

en:
  activerecord:
    attributes:
      foo/bar:
        bar_attr: "TEST"

Upvotes: 9

Related Questions