Skip to content

Commit

Permalink
Merge pull request #213 from nashbridges/simple-interpolation-missing
Browse files Browse the repository at this point in the history
Simplify handling of missing interpolation argument
  • Loading branch information
José Valim committed Aug 13, 2013
2 parents 6f4342d + b40df79 commit b22a410
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 79 deletions.
1 change: 0 additions & 1 deletion lib/i18n.rb
@@ -1,7 +1,6 @@
require 'i18n/version'
require 'i18n/exceptions'
require 'i18n/interpolate/ruby'
require 'i18n/interpolate/missing_interpolation_argument_handler'

module I18n
autoload :Backend, 'i18n/backend'
Expand Down
21 changes: 12 additions & 9 deletions lib/i18n/config.rb
Expand Up @@ -65,22 +65,25 @@ def exception_handler=(exception_handler)
@@exception_handler = exception_handler
end

# Return the current handler for situations when interpolation argument
# is missing. Defaults to MissingInterpolationArgumentHandler, which
# raises an exception.
# Returns the current handler for situations when interpolation argument
# is missing. MissingInterpolationArgument will be raised by default.
def missing_interpolation_argument_handler
@@missing_interpolation_argument_handler ||= MissingInterpolationArgumentHandler.new
@@missing_interpolation_argument_handler ||= lambda do |missing_key, provided_hash, string|
raise MissingInterpolationArgument.new(missing_key, provided_hash, string)
end
end

# Sets the missing interpolation argument handler. It can be any
# object that responds to #call.
# object that responds to #call. The arguments that will be passed to #call
# are the same as for MissingInterpolationArgument initializer. Use +Proc.new+
# if you don't care about arity.
#
# == Example:
# You can supress raising an exception by reassigning default handler
# with options:
# You can supress raising an exception and return string instead:
#
# I18n.config.missing_interpolation_argument_handler =
# MissingInterpolationArgumentHandler.new(raise_exception: false)
# I18n.config.missing_interpolation_argument_handler = Proc.new do |key|
# "#{key} is missing"
# end
def missing_interpolation_argument_handler=(exception_handler)
@@missing_interpolation_argument_handler = exception_handler
end
Expand Down
4 changes: 0 additions & 4 deletions lib/i18n/exceptions.rb
Expand Up @@ -86,10 +86,6 @@ def initialize(key, values, string)
@key, @values, @string = key, values, string
super "missing interpolation argument #{key.inspect} in #{string.inspect} (#{values.inspect} given)"
end

def html_message
%(<span class='interpolation_missing' title='#{message}'>%{#{key}}</span>)
end
end

class ReservedInterpolationKey < ArgumentError
Expand Down
30 changes: 0 additions & 30 deletions lib/i18n/interpolate/missing_interpolation_argument_handler.rb

This file was deleted.

5 changes: 0 additions & 5 deletions test/backend/exceptions_test.rb
Expand Up @@ -32,9 +32,4 @@ def setup
exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
assert_equal 'missing interpolation argument "key" in "string" ({:this=>"was given"} given)', exception.message
end

test "exceptions: MissingInterpolationArgument html message includes missing key, provided keys and full string" do
exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
assert_equal %|<span class='interpolation_missing' title='missing interpolation argument "key" in "string" ({:this=>"was given"} given)'>%{key}</span>|, exception.html_message
end
end
30 changes: 0 additions & 30 deletions test/i18n/missing_interpolate_handler_test.rb

This file was deleted.

0 comments on commit b22a410

Please sign in to comment.