Sha256: 7693db314b12f8bab9a55a44eb106107da9e224e292586bda0a6ac4270f93b4d

Contents?: true

Size: 1.41 KB

Versions: 73

Compression:

Stored size: 1.41 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 || 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

73 entries across 61 versions & 12 rubygems

Version Path
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/i18n-0.9.5/lib/i18n/interpolate/ruby.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/i18n-0.9.5/lib/i18n/interpolate/ruby.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/i18n-0.9.5/lib/i18n/interpolate/ruby.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/i18n-0.9.5/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/i18n-0.8.0/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/i18n-0.8.0/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/i18n-0.9.5/lib/i18n/interpolate/ruby.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/i18n-0.9.5/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/i18n-0.8.0/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/i18n-1.1.1/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/i18n-0.8.0/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/i18n-0.8.0/lib/i18n/interpolate/ruby.rb