# I18n date formatting rails 2.2.2

**URL:** <https://www.ruby-forum.com/t/i18n-date-formatting-rails-2-2-2/161686>\
**Category:** Rails\
**Created:** [February 28, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/i18n-date-formatting-rails-2-2-2/161686 "2009-02-28T23:52:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@ronald](https://www.ruby-forum.com/u/ronald)\
**Post date:** [February 28, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/i18n-date-formatting-rails-2-2-2/161686/1 "2009-02-28T23:52:32Z")

</div>

Hello everyone,

I’m trying to get Rails (v2.2.2) to print dates in Dutch but it won’t  
pick up the formats I specify. ☹

I tried the following:

1. created a ‘nl.yml’ file under ‘config/locales’
2. copied over the English defaults for dates from activesupport/…/  
en.yml
3. added Dutch activerecord error messages and number and currency  
formats (euro sign)
4. changed the default locale in ‘environment.rb’

The AR error messages are shown in Dutch just fine so Rails is loading  
the translation file and setting the locale correctly but when I do  
Date.today.to\_s I always get the English versions, no matter what  
format I pass to the to\_s() method. Also numbers and currencies are  
working fine. I just can’t get dates to behave!

Been hunting down documentation and blog posts on the subject for  
hours now and I’m going crazy. Any ideas?

Ronald

---

<div class="post-metadata">

**Author:** ![ronald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@ronald](https://www.ruby-forum.com/u/ronald)\
**Post date:** [February 28, 2009, 11:58pm UTC](https://www.ruby-forum.com/t/i18n-date-formatting-rails-2-2-2/161686/2 "2009-02-28T23:58:58Z")

</div>

They didn’t worke for me either, so I rewrote the to\_s method for  
dates and times:

Date.class\_eval do

def to\_s( format\_name = :default )  
format = case format\_name  
when String  
self.strftime( format )  
when Symbol  
I18n.localize( self, :format =\> format\_name)  
end  
end

end

Time.class\_eval do

def to\_s( format\_name = :default )  
format = case format\_name  
when String  
self.strftime( format )  
when Symbol  
I18n.localize( self, :format =\> format\_name)  
end  
end

end

I did also search for reasons why it doesn’t work (from the I18N docs  
it seemed that it should work), but this method at the Date object  
shows that it has no idea about I18N:

```
    def to_formatted_s(format = :default)
      if formatter = DATE_FORMATS[format]
        if formatter.respond_to?(:call)
          formatter.call(self).to_s
        else
          strftime(formatter)
        end
      else
        to_default_s
      end
    end

```

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![ronald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@ronald](https://www.ruby-forum.com/u/ronald)\
**Post date:** [March 1, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/i18n-date-formatting-rails-2-2-2/161686/3 "2009-03-01T12:10:31Z")

</div>

On Feb 28, 11:58 pm, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> I18n.localize( self, :format => format_name)
> self.strftime( format )
> when Symbol
> I18n.localize( self, :format => format_name)
> end
> 
> ```
> 
> end
> 
> end

Great! Thanks, this works for me. (Small bug: parameter to strftime  
should be format\_name, not format).

> ```
> end
> else
> to_default_s
> end
> end
> 
> ```

Yeah I dug up this piece of source code as well. Guess I was too tired  
to compare this with for instance number\_to\_currency’s source and come  
to this conclusion myself 😉 Anyway, thanks again!

Ronald
