Skip to content

Commit

Permalink
Support ±0 as a key
Browse files Browse the repository at this point in the history
  • Loading branch information
movermeyer committed Dec 16, 2021
1 parent dbee3d8 commit d014c01
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 d014c01

Please sign in to comment.