# Dates in Gettext

**URL:** <https://www.ruby-forum.com/t/dates-in-gettext/118825>\
**Category:** Rails I18n\
**Created:** [October 31, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/dates-in-gettext/118825 "2007-10-31T12:25:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samlown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@samlown](https://www.ruby-forum.com/u/samlown)\
**Post date:** [October 31, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/dates-in-gettext/118825/1 "2007-10-31T12:25:10Z")

</div>

Hi All,

It comes up quite a lot, and after much searching, I’ve not been able to  
find a solution to handling dates in Gettext that I actually liked.  
Overriding array constants inside various libraries and expecting things  
to work didn’t do it for me.

So I made my own solution.

The attached gettext\_date.rb file is a _preview_ to try and get a bit of  
feedback about the viability of this approach. Dates are messy, so I’m  
expecting that my solution does not cover all the possible exceptions to  
the basic rules I’ve defined.

It’s primarily designed for Ruby on Rails, but should work with any ruby  
app, although the TimeExtensions is unnecessary.

To install, copy the gettext\_date.rb file to your /lib directory, and  
add the following line to your environment.rb:

require ‘lib/gettext\_date’

Assuming you have every other part of Rails setup for Gettext, and  
you’re .po files contain the translated month and day names, it should  
“just work”. There is no need to add the .localize call to all your time  
methods, and you can stick with the standard strftime calls. Its also  
designed so that Time.now.to\_s(:rfc822) or similar RFC calls will use  
the standard ruby strftime method, and remain in english as the standard  
requires.

The key advantage with my approach, is that if your application does not  
use English as your native language, the GettextDate::Conversions class  
can be overwritten with the names in the other language, therefore  
removing any confusing language inconsistencies for the translators.

I’ll set up a web site as soon as possible with more details, but I’d  
love to get some feedback! At least now Gettext fans don’t have to be  
jealous of the Globalize crowd 🙂

Cheers, sam

---

<div class="post-metadata">

**Author:** ![samlown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@samlown](https://www.ruby-forum.com/u/samlown)\
**Post date:** [October 31, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/dates-in-gettext/118825/2 "2007-10-31T14:01:56Z")

</div>

Forgot to mention, the \_( ) method needs to be accessible to the  
GettextDate library, a quick fix for this is to add the following in  
/config/environment.rb just after the ‘gettext/rails’ require.

include GetText::Rails  
bindtextdomain(‘DOMAIN’)  
require ‘lib/gettext\_date’

DOMAIN should be replaced with your current application domain. This  
approach provides the \_( ) method for everything, and duplicates the  
init\_gettext call, which is a bit lame, so I’ll work on doing it another  
way. Good for testing though!

Cheers,  
sam

---

<div class="post-metadata">

**Author:** ![samlown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@samlown](https://www.ruby-forum.com/u/samlown)\
**Post date:** [October 31, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/dates-in-gettext/118825/3 "2007-10-31T14:41:19Z")

</div>

Samuel L. wrote:

> include GetText::Rails  
> bindtextdomain(‘DOMAIN’)  
> require ‘lib/gettext\_date’

Scrap that, use this version instead, which automatically extends the  
ActionController::Base with a version of init\_gettext that adds the text  
domain to the Conversions:

[http://www.samlown.com/uploads/ruby/gettext\_date.rb](http://www.samlown.com/uploads/ruby/gettext_date.rb)

sam.
