instance method
interpolate
Ruby on Rails 2.2.3
Since v2.2.3 Last seen in v2.2.3Signature
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.0.1/i18n/backend/simple.rb, line 145
def interpolate(locale, string, values = {})
return string unless string.is_a?(String)
if string.respond_to?(:force_encoding)
original_encoding = string.encoding
string.force_encoding(Encoding::BINARY)
end
result = string.gsub(MATCH) do
escaped, pattern, key = $1, $2, $2.to_sym
if escaped
pattern
elsif INTERPOLATION_RESERVED_KEYS.include?(pattern)
raise ReservedInterpolationKey.new(pattern, string)
elsif !values.include?(key)
raise MissingInterpolationArgument.new(pattern, string)
else
values[key].to_s
end
end
result.force_encoding(original_encoding) if original_encoding
result
end
Defined in activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb line 145
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in I18n::Backend::Simple