Sha256: 55296fe7b6defe437f5a44a43dfa23382fdf383296ffdfe91fe0517e08463b78

Contents?: true

Size: 1.39 KB

Versions: 119

Compression:

Stored size: 1.39 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
  INTERPOLATION_PATTERN = Regexp.union(
    /%%/,
    /%\{(\w+)\}/,                               # matches placeholders like "%{foo}"
    /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
  )

  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(INTERPOLATION_PATTERN) do |match|
        if match == '%%'
          '%'
        else
          key = ($1 || $2).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

119 entries across 105 versions & 35 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/i18n-0.6.9/lib/i18n/interpolate/ruby.rb