instance method
interpolate
Ruby on Rails 2.3.18
Since v2.3.18 Last seen in v2.3.18Signature
interpolate(locale, string, values = {})
Interpolates values into a given string.
interpolate "file %{file} opened by %%{user}", :file => 'test.txt', :user => 'Mr. X' # => "file test.txt opened by %{user}"
Note that you have to double escape the \ when you want to escape the {{...}} key in a string (once for the string and once for the interpolation).
Parameters
-
localereq -
stringreq -
valuesopt = {}
Source
# File activesupport/lib/active_support/vendor/i18n-0.4.1/i18n/backend/base.rb, line 152
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
if escaped
"{{#{key}}}"
else
warn_syntax_deprecation!
"%{#{key}}"
end
end
keys = string.scan(INTERPOLATION_SYNTAX_PATTERN).flatten
return string if keys.empty?
values.each do |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
end
rescue KeyError => e
if string =~ RESERVED_KEYS_PATTERN
raise ReservedInterpolationKey.new($1.to_sym, string)
else
raise MissingInterpolationArgument.new(original_values, string)
end
end
Defined in activesupport/lib/active_support/vendor/i18n-0.4.1/i18n/backend/base.rb line 152
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in I18n::Backend::Base