Skip to content

Commit

Permalink
Merge pull request #594 from movermeyer/movermeyer/normalize_zero_key
Browse files Browse the repository at this point in the history
Fix lookups of `0` keys
  • Loading branch information
radar committed Jan 3, 2022
2 parents 8a3b856 + d014c01 commit 3a5ad2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/i18n.rb
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion test/backend/simple_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 3a5ad2a

Please sign in to comment.