Skip to content

Commit

Permalink
fix interpolation on 1.9.1 (p378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Jun 5, 2010
1 parent f521a7c commit 8d45bed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
19 changes: 14 additions & 5 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Base
RESERVED_KEYS = [:scope, :default, :separator, :resolve]
RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/
DEPRECATED_INTERPOLATION_SYNTAX_PATTERN = /(\\)?\{\{([^\}]+)\}\}/
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
Expand Down Expand Up @@ -150,7 +151,8 @@ def pluralize(locale, entry, count)
# interpolation).
def interpolate(locale, string, values = {})
return string unless string.is_a?(::String) && !values.empty?

original_values = values.dup

preserve_encoding(string) do
string = string.gsub(DEPRECATED_INTERPOLATION_SYNTAX_PATTERN) do
escaped, key = $1, $2.to_sym
Expand All @@ -162,10 +164,17 @@ def interpolate(locale, string, values = {})
end
end

keys = string.scan(INTERPOLATION_SYNTAX_PATTERN).flatten
return string if keys.empty?

values.each do |key, value|
value = value.call(values) if interpolate_lambda?(value, string, key)
value = value.to_s unless value.is_a?(::String)
values[key] = value
if keys.include?(key.to_s)
value = value.call(values) if interpolate_lambda?(value, string, key)
value = value.to_s unless value.is_a?(::String)
values[key] = value
else
values.delete(key)
end
end

string % values
Expand All @@ -174,7 +183,7 @@ def interpolate(locale, string, values = {})
if string =~ RESERVED_KEYS_PATTERN
raise ReservedInterpolationKey.new($1.to_sym, string)
else
raise MissingInterpolationArgument.new(values, string)
raise MissingInterpolationArgument.new(original_values, string)
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/api/tests/localization/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setup
setup_date_translations
@date = ::Date.new(2008, 3, 1)
end

define_method "test localize Date: given the short format it uses it" do
# TODO should be Mrz, shouldn't it?
assert_equal '01. Mar', I18n.l(@date, :format => :short, :locale => :de)
Expand Down Expand Up @@ -55,14 +55,14 @@ def setup
define_method "test localize Date: given a format is missing it raises I18n::MissingTranslationData" do
assert_raise(I18n::MissingTranslationData) { I18n.l(@date, :format => :missing) }
end

define_method "test localize Date: it does not alter the format string" do
assert_equal '01. Februar 2009', I18n.l(::Date.parse('2009-02-01'), :format => :long, :locale => :de)
assert_equal '01. Oktober 2009', I18n.l(::Date.parse('2009-10-01'), :format => :long, :locale => :de)
end

protected

def setup_date_translations
store_translations :de, {
:date => {
Expand Down
7 changes: 4 additions & 3 deletions test/api/tests/localization/procs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ module Procs
if can_store_procs?
setup_time_proc_translations
time = ::Time.parse('2008-03-01 6:00 UTC')
assert_equal '[Sat Mar 01 06:00:00 UTC 2008, {}]', I18n.l(time, :format => :proc, :locale => :ru)
assert_equal [time, {}].inspect, I18n.l(time, :format => :proc, :locale => :ru)
end
end

define_method "test localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
if can_store_procs?
setup_time_proc_translations
time = ::Time.parse('2008-03-01 6:00 UTC')
assert_equal '[Sat Mar 01 06:00:00 UTC 2008, {:foo=>"foo"}]', I18n.l(time, :format => :proc, :foo => 'foo', :locale => :ru)
time = ::Time.parse('2008-03-01 6:00 UTC')
options = { :foo => 'foo' }
assert_equal [time, options].inspect, I18n.l(time, options.merge(:format => :proc, :locale => :ru))
end
end

Expand Down

0 comments on commit 8d45bed

Please sign in to comment.