53

I have a model Products::Car. How can I translate its attributes?

I have already tried this:

activerecord: 
  models:
    products:
      car: "Автомобиль"
  attributes:
    products:
      car:
        owner: "Владелец"

And this:

activerecord: 
  models:
    products_car: "Автомобиль"
  attributes:
    products_car:
      owner: "Владелец"

But if I try to use Products::Car.model_name.human it still says "Car". My other translations work well, and the language is set to :ru.

Gabe Kopley
  • 16,281
  • 5
  • 47
  • 60
Alex
  • 2,309
  • 2
  • 16
  • 20

2 Answers2

134

I have checked 'model_name.human' source code and found 'i18n_key' method. I have tried this:

irb(main):006:0> Products::Car.model_name.i18n_key
=> :"products/car"

Then I changed my yml file to this:

activerecord:    
  models:
    products/car: "Автомобиль"   
  attributes:
    products/car:
      owner: "Владелец"

and it works!

EDIT:

For further reference: the i18n_key is set in the initializer of ActiveModel::Name https://github.com/rails/rails/blob/375a4143cf5caeb6159b338be824903edfd62836/activemodel/lib/active_model/naming.rb#L147

and it is simply based on

MyClass.name.underscore
kluka
  • 271
  • 3
  • 16
Alex
  • 2,309
  • 2
  • 16
  • 20
  • Thanks for sharing your method for finding this. It's the singular model name after a namespace that tripped me. – Thilo Feb 22 '13 at 09:13
  • Ten years and three Rails versions later and this yml syntax still works. Activemodel 6.1.3.1. here. – Jake Apr 02 '21 at 08:53
0

As of rails 3.2.12 it seems to be converted back to this:

activerecord: 
  models:
    products:
      car: "Автомобиль"
  attributes:
    products:
      car:
        owner: "Владелец"

And products/car: "Автомобиль" is not working anymore

bonyiii
  • 2,833
  • 29
  • 25