Sha256: db2d621bf192a104bc5744604f0b247b3dc5f8f0b4bc4d22cfdadda04e8daedc

Contents?: true

Size: 1.13 KB

Versions: 16

Compression:

Stored size: 1.13 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
    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 = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(key, values, string))
          value = value.call(values) if value.respond_to?(:call)
          $3 ? sprintf("%#{$3}", value) : value
        end
      end
    end
  end
end

Version data entries

16 entries across 14 versions & 7 rubygems

Version Path
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/i18n-0.6.2/lib/i18n/interpolate/ruby.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
challah-1.0.0 vendor/bundle/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
font-awesome-rails-3.1.1.2 vendor/ruby/2.0.0/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
vagrant-shell-0.2.6 vendor/bundle/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
vagrant-shell-0.2.5 vendor/bundle/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/i18n-0.6.4/lib/i18n/interpolate/ruby.rb
i18n-0.6.4 lib/i18n/interpolate/ruby.rb
i18n-0.6.3 lib/i18n/interpolate/ruby.rb
i18n-0.6.2 lib/i18n/interpolate/ruby.rb