Skip to content

Commit

Permalink
Matching whole words only
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Gloser committed Apr 3, 2012
1 parent 26d3d99 commit cfba59b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,3 +1,3 @@
source 'http://rubygems.org'

gemspec
gemspec
4 changes: 3 additions & 1 deletion lib/delocalize/localized_date_time_parser.rb
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/delocalize_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit cfba59b

Please sign in to comment.