diff --git a/lib/i18n.rb b/lib/i18n.rb index 16f28b29..13f1bfd1 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -397,7 +397,7 @@ def normalize_key(key, separator) keys.delete('') keys.map! do |k| case k - when /\A[-+]?[1-9]\d*\z/ # integer + when /\A[-+]?([1-9]\d*|0)\z/ # integer k.to_i when 'true' true diff --git a/test/backend/simple_test.rb b/test/backend/simple_test.rb index 960e8cc9..1a34bb97 100644 --- a/test/backend/simple_test.rb +++ b/test/backend/simple_test.rb @@ -19,9 +19,29 @@ def setup end test "simple backend translate: given integer as a key" do - store_translations :en, available: { 1 => "Yes", 2 => "No" } + store_translations :en, available: { -1 => "Possibly", 0 => "Maybe", 1 => "Yes", 2 => "No", 3 => "Never" } + assert_equal "Possibly", I18n.t(:available)[-1] + assert_equal "Possibly", I18n.t('available.-1') + assert_equal "Maybe", I18n.t(:available)[0] + assert_equal "Maybe", I18n.t('available.0') assert_equal "Yes", I18n.t(:available)[1] assert_equal "Yes", I18n.t('available.1') + assert_equal "No", I18n.t(:available)[2] + assert_equal "No", I18n.t('available.2') + assert_equal "Never", I18n.t(:available)[3] + assert_equal "Never", I18n.t('available.+3') + end + + test 'simple backend translate: given integer with a leading positive/negative sign' do + store_translations :en, available: { -1 => "No", 0 => "Maybe", 1 => "Yes" } + assert_equal 'No', I18n.t(:available)[-1] + assert_equal 'No', I18n.t('available.-1') + assert_equal 'Maybe', I18n.t(:available)[+0] + assert_equal 'Maybe', I18n.t(:available)[-0] + assert_equal 'Maybe', I18n.t('available.-0') + assert_equal 'Maybe', I18n.t('available.+0') + assert_equal 'Yes', I18n.t(:available)[+1] + assert_equal 'Yes', I18n.t('available.+1') end test 'simple backend translate: given integer with a lead zero as a key' do