Permalink
Browse files

make better use of Masao's String patch and amend MissingInterpolatio…

…nArgument exception to wrap Ruby 1.9's KeyError
  • Loading branch information...
1 parent 6090755 commit a27b74df9d2e4357140d77506e1d9c43f7cff63a Sven Fuchs committed Jul 8, 2009
Showing with 13 additions and 11 deletions.
  1. +6 −5 lib/i18n/backend/simple.rb
  2. +4 −4 lib/i18n/exceptions.rb
  3. +3 −2 test/i18n_exceptions_test.rb
View
@@ -4,7 +4,7 @@ module I18n
module Backend
class Simple
RESERVED_KEYS = [:scope, :default, :separator]
- MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
+ INTERPOLATION_SYNTAX_PATTERN = /(\\\\)?\{\{([^\}]+)\}\}/
# Accepts a list of paths to translation files. Loads translations from
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
@@ -162,19 +162,20 @@ def pluralize(locale, entry, count)
def interpolate(locale, string, values = {})
return string unless string.is_a?(String) && !values.empty?
- string.gsub(MATCH) do
+ string.gsub(INTERPOLATION_SYNTAX_PATTERN) do
escaped, key = $1, $2.to_sym
if escaped
key
elsif RESERVED_KEYS.include?(key)
raise ReservedInterpolationKey.new(key, string)
- elsif !values.include?(key)
- "{{#{key}}}"
else
- values[key].to_s
+ "%{#{key}}"
end
end % values
+
+ rescue KeyError => e
+ raise MissingInterpolationArgument.new(values, string)
end
# Loads a single translations file by delegating to #load_rb or
View
@@ -34,10 +34,10 @@ def initialize(entry, count)
end
class MissingInterpolationArgument < ArgumentError
- attr_reader :key, :string
- def initialize(key, string)
- @key, @string = key, string
- super "interpolation argument #{key} missing in #{string.inspect}"
+ attr_reader :values, :string
+ def initialize(values, string)
+ @values, @string = values, string
+ super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
end
end
@@ -42,16 +42,17 @@ def test_invalid_pluralization_data_message
end
def test_missing_interpolation_argument_stores_key_and_string
+ assert_raises(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
force_missing_interpolation_argument
rescue I18n::ArgumentError => e
- assert_equal :bar, e.key
+ # assert_equal :bar, e.key
assert_equal "{{bar}}", e.string
end
def test_missing_interpolation_argument_message
force_missing_interpolation_argument
rescue I18n::ArgumentError => e
- assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
+ assert_equal 'missing interpolation argument in "{{bar}}" ({:baz=>"baz"} given)', e.message
end
def test_reserved_interpolation_key_stores_key_and_string

0 comments on commit a27b74d

Please sign in to comment.