Browse files

Matching whole words only

  • Loading branch information...
1 parent 26d3d99 commit cfba59b710e8308597c742b5962a1d1eca017b5b Jan Gloser committed Apr 3, 2012
Showing with 21 additions and 2 deletions.
  1. +1 −1 Gemfile
  2. +3 −1 lib/delocalize/localized_date_time_parser.rb
  3. +17 −0 test/delocalize_test.rb
View
2 Gemfile
@@ -1,3 +1,3 @@
source 'http://rubygems.org'
-gemspec
+gemspec
View
4 lib/delocalize/localized_date_time_parser.rb
@@ -55,7 +55,9 @@ def default_parse(datetime, type)
def translate_month_and_day_names(datetime)
translated = I18n.t([:month_names, :abbr_month_names, :day_names, :abbr_day_names], :scope => :date).flatten.compact
original = (Date::MONTHNAMES + Date::ABBR_MONTHNAMES + Date::DAYNAMES + Date::ABBR_DAYNAMES).compact
- translated.each_with_index { |name, i| datetime.gsub!(name, original[i]) }
+ translated.each_with_index { |name, i|
+ datetime.gsub!(name, original[i]) if datetime.split(' ').any? {|chunk| chunk == name }
+ }
end
def input_formats(type)
View
17 test/delocalize_test.rb
@@ -179,6 +179,23 @@ def setup
assert_equal @product.price, 0
assert_equal @product.price_before_type_cast, "asd"
end
+
+ test 'it should gsub only whole translated words and not mess up the original string' do
+ orig_march = I18n.t('date.month_names')[3]
+ orig_monday = I18n.t('date.abbr_day_names')[1]
+
+ #Simulate Dutch
+ I18n.t('date.month_names')[3] = 'Maart'
+ I18n.t('date.abbr_day_names')[1] = "Ma"
+
+ subject = '30 Maart 2011'
+ Delocalize::LocalizedDateTimeParser.send(:translate_month_and_day_names, subject)
+
+ assert_equal subject, '30 March 2011'
+
+ I18n.t('date.month_names')[3] = orig_march
+ I18n.t('date.abbr_day_names')[1] = orig_monday
+ end
end
class DelocalizeActionViewTest < ActionView::TestCase

0 comments on commit cfba59b

Please sign in to comment.