Skip to the content
sistrall.it
December 18, 2006

Globalyses and symbols for date formats

Hey, this post is vintage content. It dates back more than 18 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)
      end

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

end

Previous

Next

Journey in ActiveSupport

December 19, 2006

This page was built on Fri, 17 Jan 2025 13:51:56 GMT from code version 38bda9c8.