Tigraine
Tigraine

Reputation: 23648

Rails i18n with translated model names

In my application I want to translate the validation error line: "3 errors have prohibited this order from being saved".

Obviously this can be made generic to fit all models that use this translation so in my de.yml locale template I got activerecord.errors.template.header like this:

  activerecord:
    errors:
      template:
        header:
          one:    "Konnte %{model} nicht speichern: ein Fehler."
          other:  "Konnte %{model} nicht speichern: %{count} Fehler."

Now the issue is how to call this validation without repeating myself a lot. Obviously you can simply call this through:

t('activerecord.errors.template.header', :count => @order.count, :model => Order)

But this won't translate Order (Order is called Bestellung in German)

I could go ahead and fix this by calling translate again inside the translate call:

t('activerecord.errors.template.header', :count => @order.count, :model => t('activerecord.models.#{Order}'))

But this really feels like a pretty bad solution and I am pretty sure there has to be a built in way to do this (as usually there is a cleaner way to do dirty stuff in Rails).

Any pointers on what the recommended way to deal with translations like this would be appreciated.

Upvotes: 3

Views: 4977

Answers (1)

Florent2
Florent2

Reputation: 3513

Have you tried the following, using :model => Order.model_name.human instead of :model => Order?

t('activerecord.errors.template.header', :count => @order.count, :model => Order.model_name.human )

Upvotes: 7

Related Questions