Globalyses and symbols for date formats
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
December 15, 2006
Next
December 19, 2006