Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Include the missing key in "missing interpolation argument" errors. #166

Merged
merged 1 commit into from

3 participants

Henrik Nyh Sven Fuchs Christopher Dell
Henrik Nyh

It's a bit annoying to manually compare the passed-in hash to the arguments, so we fixed it. Hope you'll merge.

Some things I noticed:

test/i18n/exceptions_test.rb had a commented-out test about including the missing key in an exception accessor. Couldn't figure out from git blame why it was commented out in the first place ("cleanup"), so I restored it.

backend/interpolation_compiler.rb did this:

        def missing_key(key)
          "raise(MissingInterpolationArgument.new(#{key}, self))"
        end

That looks like forgotten, possibly dead code. Before my pull request, the actual method profile was values, string; the key wasn't passed. I just modified it to pass {} for values, so it's about as broken as it was, but that class should probably be reviewed.

Sven Fuchs
Owner

thanks @henrik!

looks like a good idea to me, but i'll leave this up to @tigrish :)

Christopher Dell tigrish merged commit 09c0aa7 into from
Christopher Dell
Collaborator

Good stuff, it seems so obvious!
Of course we want to see which key is missing :)

Henrik Nyh

:) Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  lib/i18n/backend/interpolation_compiler.rb
View
@@ -77,7 +77,7 @@ def nil_key(key)
end
def missing_key(key)
- "raise(MissingInterpolationArgument.new(#{key}, self))"
+ "raise(MissingInterpolationArgument.new(#{key}, {}, self))"
end
def reserved_key(key)
8 lib/i18n/exceptions.rb
View
@@ -81,10 +81,10 @@ def initialize(entry, count)
end
class MissingInterpolationArgument < ArgumentError
- attr_reader :values, :string
- def initialize(values, string)
- @values, @string = values, string
- super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
+ attr_reader :key, :values, :string
+ def initialize(key, values, string)
+ @key, @values, @string = key, values, string
+ super "missing interpolation argument #{key.inspect} in #{string.inspect} (#{values.inspect} given)"
end
end
2  lib/i18n/interpolate/ruby.rb
View
@@ -21,7 +21,7 @@ def interpolate_hash(string, values)
'%'
else
key = ($1 || $2).to_sym
- value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(values, string))
+ value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(key, values, string))
value = value.call(values) if value.respond_to?(:call)
$3 ? sprintf("%#{$3}", value) : value
end
4 test/i18n/exceptions_test.rb
View
@@ -54,14 +54,14 @@ def test_invalid_locale_stores_locale
test "MissingInterpolationArgument stores key and string" do
assert_raise(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
force_missing_interpolation_argument do |exception|
- # assert_equal :bar, exception.key
+ assert_equal :bar, exception.key
assert_equal "%{bar}", exception.string
end
end
test "MissingInterpolationArgument message contains the missing and given arguments" do
force_missing_interpolation_argument do |exception|
- assert_equal 'missing interpolation argument in "%{bar}" ({:baz=>"baz"} given)', exception.message
+ assert_equal 'missing interpolation argument :bar in "%{bar}" ({:baz=>"baz"} given)', exception.message
end
end
Something went wrong with that request. Please try again.