Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails3 I18n: can't override "1 error prohibited this packet from being saved:"

When i get errors on model Packet, i always see the first (english=not translated) line:

1 error prohibited this packet from being saved:
Naam Gelieve het veld Naam in te vullen!

The translations for the error for the single field is found!

While i have the following in my nl.yml

nl:

  activemodel:
    errors:
      template:
        header:
          one:   "1 fout voorkwam dat dit %{model} kon bewaard worden"
          other: "%{count} fouten voorkwamen dat deze %{model} kon bewaard worden"
        body:    "Er waren problemen met de volgende velden:"

  errors:
    template:
      body: "Controleer alstublieft de volgende velden:"
      header:
        one: "Kon dit {{model}} object niet opslaan: 1 fout."
        other: "Kon dit {{model}} niet opslaan: {{count}} fouten."

  activerecord:
    errors:
      template:
        header:
          one:   "1 fout voorkwam dat dit %{model} kon bewaard worden"
          other: "%{count} fouten voorkwamen dat deze %{model} kon bewaard worden"
        body:    "Er waren problemen met de volgende velden:"
      messages:
        blank: Gelieve het veld %{attribute} in te vullen!
    models:
      survey: test
      packet: woordenlijst
      user: gebruiker
    attributes:
      survey:
        name: Naam

If have been looking at a lot of stuff, at the rails-i18n, downloaded their nl.yml but nothing seems to help. Does anybody have a clue why it does not work?

I thought it was a possible conflict with other files (i have localisation files for two other gems), but removing those temporarily did not change a thing.

Does anybody have a clue how to debug this?

Can i somehow remove the default translation, so that i get the error which translation would not be found?

like image 900
nathanvda Avatar asked Sep 29 '10 13:09

nathanvda


1 Answers

Doh! Found it! Stupid me!

In forgot that in rails 3 you no longer use error_messages_for, so instead in my scaffolded view code there was the following code:

 = form_for @packet do |f|
   -if @packet.errors.any?
     #errorExplanation
       %h2= "#{pluralize(@packet.errors.count, "error")} prohibited this packet from being saved:"

So, no wonder i could not translate that. Aaaaarrrgghh!!

The solution is either to use the dynamic-form plugin (which gives you the same functionality as in rails 2.3), or adapt the view accordingly, as i did:

  -if @packet.errors.any?
    #errorExplanation
      %h2
        - if @packet.errors.count == 1
          = t 'activerecord.errors.template.header.one', :model => @packet.class.human_name
        - else
          = t 'activerecord.errors.template.header.other', :model => @packet.class.human_name, :count => @packet.errors.count
      %b= t 'activerecord.errors.template.body'
      %ul
        - @packet.errors.full_messages.each do |msg|
          %li= msg

But, as we need to do this for each view possibly having errors, this should go into a partial, or use the plugin mentioned above :)

like image 85
nathanvda Avatar answered Oct 31 '22 01:10

nathanvda