malcoauri
malcoauri

Reputation: 12189

Translate model name by string in Rails

There is the following code:

model_name = self.class.name.demodulize.sub("Controller", "").singularize
message = t('activerecord.exceptions.not_found', model_name: model_name) 
render json: message, status: :not_found

And there is some YAML:

ru:
  activerecord:
    exceptions:
      not_found: "%{model_name} не найден"

As you can see I have Russian text in translation, but model name is still in English. How can I translate model name in Russian too? Thanks in advance!

Upvotes: 1

Views: 1160

Answers (1)

max
max

Reputation: 102026

ru:
  activerecord:
    models:
      user: пользователь

model_name = self.class.name.demodulize.sub("Controller", "").singularize
model_klass = model_name.constantize
message = t(
 'activerecord.exceptions.not_found', 
 model_name: model_klass.model_name.human
) 
render json: message, status: :not_found

http://guides.rubyonrails.org/i18n.html

Upvotes: 1

Related Questions