Skip to content

Commit

Permalink
Handle accessing keys with a leading period (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
gingermusketeer authored and timfjord committed Nov 20, 2017
1 parent 52bd44e commit 118bc91
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails_3.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
i18n-active_record (0.2.0)
i18n-active_record (0.2.1)
i18n (>= 0.5.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails_4.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
i18n-active_record (0.2.0)
i18n-active_record (0.2.1)
i18n (>= 0.5.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails_5.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
i18n-active_record (0.2.0)
i18n-active_record (0.2.1)
i18n (>= 0.5.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails_master.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GIT
PATH
remote: ..
specs:
i18n-active_record (0.2.0)
i18n-active_record (0.2.1)
i18n (>= 0.5.0)

GEM
Expand Down
11 changes: 9 additions & 2 deletions lib/i18n/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ def store_translations(locale, data, options = {})

def lookup(locale, key, scope = [], options = {})
key = normalize_flat_keys(locale, key, scope, options[:separator])
result = if key == '.'
if key.first == '.'
key = key[1..-1]
end
if key.last == '.'
key = key[0..-2]
end

result = if key == ''
Translation.locale(locale).all
else
Translation.locale(locale).lookup(key)
Expand All @@ -69,7 +76,7 @@ def lookup(locale, key, scope = [], options = {})

def build_translation_hash_by_key(lookup_key, translation)
hash = {}
if lookup_key == '.'
if lookup_key == ''
chop_range = 0..-1
else
chop_range = (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
Expand Down
9 changes: 9 additions & 0 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def teardown
assert_equal expected_hash, I18n.t('.')
end

test "accessing keys with a trailing/leading period" do
expected_hash = { :bar => 'bar', :baz => 'baz' }
assert_equal expected_hash, I18n.t('foo')
assert_equal expected_hash, I18n.t('.foo')
assert_equal expected_hash, I18n.t('foo.')
assert_equal expected_hash, I18n.t('.foo.')
assert_equal expected_hash, I18n.t('.foo.')
end

test "returning all keys via . when there are no keys" do
I18n.t('.') # Fixes test flakiness by loading available locales
I18n::Backend::ActiveRecord::Translation.destroy_all
Expand Down

0 comments on commit 118bc91

Please sign in to comment.