I18n support for validates_date_time

This simple fix uses the fact, that if the second argument to the ‘add’ method is a symbol, then a generate_message is called, which does all the voodoo of Rails 2.2 I18n.
I tried to contact the original author to no avail, therefore I’ll post it here, hoping that if anyone needs it, it’ll surface on a Google search.
The fix itself is quite simple:

Index: lib/validates_date_time.rb
===================================================================
--- lib/validates_date_time.rb  (revision 403)
+++ lib/validates_date_time.rb  (working copy)
@@ -114,7 +114,7 @@
       if options[:before]
         options[:before].each do |r|
           if r.value(record) and value >= r.last_value
-            record.errors.add(attr_name, options[:before_message] % r)
+            record.errors.add(attr_name, :before, :default => options[:before_message] % r)
             break
           end
         end
@@ -123,7 +123,7 @@
       if options[:after]
         options[:after].each do |r|
           if r.value(record) and value <= r.last_value
-            record.errors.add(attr_name, options[:after_message] % r)
+            record.errors.add(attr_name, :after, :default => options[:after_message] % r)
             break
           end
         end

I’ve a fork of the git-svn mirror here:
http://github.com/romanbsd/validates_date_time/tree/master

Then, for using it, you’ll just need to have the following path in your locale yml files (for example):

  activerecord:
    errors:
      models:
        user:
          attributes:
            birth_date:
              before: 'is wrong'
              after: 'is wrong'

2 Responses to “I18n support for validates_date_time”

  1. Patrick Espake Says:

    I want to translate the message “is an invalid date”.
    How do i do?

    Thanks.

    Reply

    Roman Reply:

    It’s currently unsupported. If you fix it, I bet that the author will be glad to apply this.

    Reply

Leave a Reply

Pages

Adsense