How can I translate ActiveRecord model class name?

Viewed 31822

What is the simplest way to get translated name of ActiveRecord model class when I have an instance of it?

For example - I have model class like this:

class Category < ActiveRecord::Base
  ...
end

I have an instance of the class:

category = Category.first

And I have YAML file config/locales/cs.yml:

cs:
  activerecord:
    models:
      category: Kategorie

And I need to do this dynamicaly, even when I don't previously know with what model class' instance I will be dealing. So I don't want to explicitly specify "activerecord.models.category".

Is there an easy way to do this? I know, that I can do something like this

"activerecord.models.#{category.class.name.underscore}"

But there has to be a better way to do this.

2 Answers
Related