3
votes

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.

1
I still don't really see the problem with this...sevenseacat
I am wondering if there is a helper that should be used here (otherwise I'm going to write my own).. Since the usual suspects (form helpers etc) all use I18n by default it seemed like awfully ugly in this case..Tigraine

1 Answers

7
votes

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 )