Skip to content

Add support for i18n #112

Closed
wants to merge 2 commits into from
View
38 lib/liquid/standardfilters.rb
@@ -165,14 +165,27 @@ def date(input, format)
return input.to_s
end
- if ((input.is_a?(String) && !/^\d+$/.match(input.to_s).nil?) || input.is_a?(Integer)) && input.to_i > 0
- input = Time.at(input.to_i)
+ input, date = parse_date_input(input)
+
+ if date.respond_to?(:strftime)
+ date.strftime(format.to_s)
+ else
+ input
+ end
+ rescue => e
+ input
+ end
+
+ def I18n_date(input, format)
+
+ if format.to_s.empty?
+ return input.to_s
end
- date = input.is_a?(String) ? Time.parse(input) : input
+ input, date = parse_date_input(input)
if date.respond_to?(:strftime)
- date.strftime(format.to_s)
+ I18n.localize(date, :format => format_for_I18n(format))
else
input
end
@@ -235,6 +248,23 @@ def to_number(obj)
end
end
+ def format_for_I18n(format)
+ if format.to_s.include?('%')
+ format.to_s
+ else
+ format.to_sym
+ end
+ end
+
+ def parse_date_input(input)
+ if ((input.is_a?(String) && !/^\d+$/.match(input.to_s).nil?) || input.is_a?(Integer)) && input.to_i > 0
+ input = Time.at(input.to_i)
+ end
+ date = input.is_a?(String) ? Time.parse(input) : input
+
+ [input, date]
+ end
+
end
Template.register_filter(StandardFilters)
View
10 test/liquid/standard_filter_test.rb
@@ -1,5 +1,11 @@
require 'test_helper'
+class I18n
+ def self.localize(date, options)
+ Time.parse(date)
+ end
+end
+
class Filters
include Liquid::StandardFilters
end
@@ -110,6 +116,10 @@ def test_date
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
end
+ def test_I18n_date
+ assert_equal '2006-07-05 10:00:00', @filters.I18n_date("2006-07-05 10:00:00", "")
+ assert_equal '2006-07-05 10:00:00', @filters.I18n_date("2006-07-05 10:00:00", nil)
+ end
def test_first_last
assert_equal 1, @filters.first([1,2,3])
Something went wrong with that request. Please try again.