
I18n.localize: select format date vs. time
Reported by Steve | July 29th, 2009 @ 03:05 PM
At the moment i18n detects the format to use ('date.formats.' vs. 'time.formats'), by checking whether or not an object responds to :sec. Which is O.K.
But passing the usual Rails ActiveSupport::TimeWithZone to I18n.l doesn't give me an opportunity to say whether I want it to be formatted as a date or time, since it'll choose time (TimeWithZone.responds_to(:sec) == true) and and option 'type' does not exist.
This snippet solves the issue:
def localize(locale, object, format = :default, options={})
(...)
type = options[:type] || (object.respond_to?(:sec) ? 'time' : 'date')
if format === Symbol
format = lookup(locale, :"#{type}.formats.#{format}")
end
format = resolve(locale, object, format, options.merge(:raise => true))
(...)
end
Greetings, my friends! (Can your heart stand the shocking fact about I18n without gettext? -- A tribute to Criswell, the famous psychic telling the story about "Plan 9: From Outer Space." - a classic by Ed Wood.)
Comments and changes to this ticket
-
lindamartn15 (at gmail) September 28th, 2023 @ 09:44 AM
I’ve been recently thinking the very same matter personally lately. Happy to see a person on the same wavelength! Nice article. credit repair severna park
-
-
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
Internationalization API for Ruby.
This library is used in Ruby on Rails but also suited for use in other contexts.