2010-07-23 20:09:54 +00:00
|
|
|
module Locomotive
|
|
|
|
module Liquid
|
|
|
|
module Filters
|
2010-05-30 23:57:33 +00:00
|
|
|
module Date
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def localized_date(input, *args)
|
2011-07-01 00:15:45 +00:00
|
|
|
return '' if input.blank?
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-07-01 00:15:45 +00:00
|
|
|
format, locale = args
|
|
|
|
|
|
|
|
locale ||= I18n.locale
|
|
|
|
format ||= I18n.t('date.formats.default', :locale => locale)
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-01-10 23:50:42 +00:00
|
|
|
if input.is_a?(String)
|
2011-01-11 11:42:12 +00:00
|
|
|
begin
|
2011-07-01 00:15:45 +00:00
|
|
|
fragments = ::Date._strptime(input, format)
|
|
|
|
input = ::Date.new(fragments[:year], fragments[:mon], fragments[:mday])
|
2011-01-11 11:42:12 +00:00
|
|
|
rescue
|
2011-07-01 00:15:45 +00:00
|
|
|
input = Time.parse(input)
|
2011-01-11 11:42:12 +00:00
|
|
|
end
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-07-01 00:15:45 +00:00
|
|
|
return input.to_s unless input.respond_to?(:strftime)
|
|
|
|
|
|
|
|
I18n.l input, :format => format, :locale => locale
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-07-01 00:15:45 +00:00
|
|
|
alias :format_date :localized_date
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
::Liquid::Template.register_filter(Date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|