Skip to content

Commit

Permalink
Modify behaviour for Hash and (maybe nested) Array translations
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-lord committed Mar 24, 2024
1 parent afb20cb commit fe82be8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
19 changes: 13 additions & 6 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,8 @@ def interpolation_keys(key, **options)
return [] unless exists?(key, **options.slice(:locale, :scope))

translation = translate(key, **options.slice(:locale, :scope))

raise I18n::NonStringTranslationError unless translation.is_a?(String)
translation
.scan(Regexp.union(I18n.config.interpolation_patterns))
.flatten
.compact
interpolation_keys_from_translation(translation)
.flatten.compact
end

# Returns true if a translation exists for a given key, otherwise returns false.
Expand Down Expand Up @@ -457,6 +453,17 @@ def normalize_key(key, separator)
keys
end
end

def interpolation_keys_from_translation(translation)
case translation
when ::String
translation.scan(Regexp.union(I18n.config.interpolation_patterns))
when ::Array
translation.map { |element| interpolation_keys_from_translation(element) }
else
[]
end
end
end

extend Base
Expand Down
2 changes: 0 additions & 2 deletions lib/i18n/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,4 @@ def initialize(file_errors)
MSG
end
end

class NonStringTranslationError < ArgumentError; end
end
15 changes: 10 additions & 5 deletions test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,18 @@ def setup
assert_equal [], I18n.interpolation_keys("does-not-exist")
end

test "interpolation_keys raises I18n::ArgumentError when non-string argument" do
assert_raises(I18n::ArgumentError) { I18n.interpolation_keys(["bad-argument"]) }
test "interpolation_keys returns an empty array when nested translation" do
store_translations(:en, "example_nested" => { "one" => "One %{foo}", "two" => "Two %{bar}" })
assert_equal [], I18n.interpolation_keys("example_nested")
end

test "interpolation_keys returns an array of keys when translation is an Array" do
store_translations(:en, "example_array" => ["One %{foo}", ["Two %{bar}", ["Three %{baz}"]]])
assert_equal ["foo", "bar", "baz"], I18n.interpolation_keys("example_array")
end

test "interpolation_keys raises I18n::NonStringTranslationError when translation is not a String" do
store_translations(:en, "example_nested" => { "one" => "One", "two" => "Two" })
assert_raises(I18n::NonStringTranslationError) { I18n.interpolation_keys("example_nested") }
test "interpolation_keys raises I18n::ArgumentError when non-string argument" do
assert_raises(I18n::ArgumentError) { I18n.interpolation_keys(["bad-argument"]) }
end

test "exists? given an existing key will return true" do
Expand Down

0 comments on commit fe82be8

Please sign in to comment.