Sha256: 7b67eec0aa5572f185a1a7f4c00fb263b2de80767b5e1d77d9f2d8f6b4568e49

Contents?: true

Size: 1.59 KB

Versions: 23

Compression:

Stored size: 1.59 KB

Contents

# heavily based on Masao Mutoh's gettext String interpolation extension
# http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb

module I18n
  DEFAULT_INTERPOLATION_PATTERNS = [
    /%%/,
    /%\{(\w+)\}/,                               # matches placeholders like "%{foo}"
    /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
  ].freeze
  INTERPOLATION_PATTERN = Regexp.union(DEFAULT_INTERPOLATION_PATTERNS)
  deprecate_constant :INTERPOLATION_PATTERN if method_defined? :INTERPOLATION_PATTERN

  class << self
    # Return String or raises MissingInterpolationArgument exception.
    # Missing argument's logic is handled by I18n.config.missing_interpolation_argument_handler.
    def interpolate(string, values)
      raise ReservedInterpolationKey.new($1.to_sym, string) if string =~ RESERVED_KEYS_PATTERN
      raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash)
      interpolate_hash(string, values)
    end

    def interpolate_hash(string, values)
      string.gsub(Regexp.union(config.interpolation_patterns)) do |match|
        if match == '%%'
          '%'
        else
          key = ($1 || $2 || match.tr("%{}", "")).to_sym
          value = if values.key?(key)
                    values[key]
                  else
                    config.missing_interpolation_argument_handler.call(key, values, string)
                  end
          value = value.call(values) if value.respond_to?(:call)
          $3 ? sprintf("%#{$3}", value) : value
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 6 rubygems

Version Path
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
spiral_form-0.1.1 vendor/bundle/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
spiral_form-0.1.0 vendor/bundle/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
ric-0.12.2 vendor/bundle/ruby/2.5.0/gems/i18n-1.6.0/lib/i18n/interpolate/ruby.rb
i18n-1.6.0 lib/i18n/interpolate/ruby.rb
i18n-1.5.3 lib/i18n/interpolate/ruby.rb
i18n-1.5.2 lib/i18n/interpolate/ruby.rb
i18n-1.5.1 lib/i18n/interpolate/ruby.rb
i18n-1.4.0 lib/i18n/interpolate/ruby.rb