Published on December 18, 2006

Globalyses and symbols for date formats

Hey, this post is vintage content. It dates back more than 17 years ago: it may contain outdated and inaccurate information.

Globalize is a plugin that facilitates the internationalisation of applications written in Rails.

The Ruby Mine recently published an introduction to its use.

Globalize provides the localize method that takes care of correctly formatting the date and time, in tune with the location settings chosen for the application. localize, unfortunately, is designed to take a date and time formatting string as a parameter; much more convenient would be to be able to use symbols_, as can be done with @tos@ in Rails. Better date.localise(:long) than date.localise('%d %B %Y %H:%M'), no?

The following code fragment, added to environment.rb, allows the desired result to be obtained (involves the use of alias_method_chain).

module Globalize
  module CoreExtensions
    module Time
      def localize_with_symbol_support(format)
        localize_without_symbol_support(::Time::DATE_FORMATS[format] ? ::Time::DATE_FORMATS[format] : format)

aliasmethodchain :localize, :symbolsupport end module Date def localizewithsymbolsupport(format) localizewithoutsymbolsupport(::Date::DATEFORMATS[format] ? ::Date::DATEFORMATS[format] : format) end aliasmethodchain :localise, :symbolsupport end end




Journey in ActiveSupport

December 19, 2006

I am Silvano Stralla. I am a developer, I like taking photos and riding bikes.
If you want, you can write to me at silvano.stralla at
This site is handmade, with love, in Turin (Italy).

© 2002—2024 Silvano Stralla

This page was built on Mon, 15 Jul 2024 14:47:40 GMT from code version a494c001.