Sha256: a0dcec4cf6f735da20f57308280da7c25e4204aadb2a7445566cd74bef7ab187

Contents?: true

Size: 1.13 KB

Versions: 174

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(values, string))
          value = value.call(values) if value.respond_to?(:call)
          $3 ? sprintf("%#{$3}", value) : value
        end
      end
    end
  end
end

Version data entries

174 entries across 155 versions & 23 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/i18n-0.6.0/lib/i18n/interpolate/ruby.rb
classiccms-0.7.4 vendor/bundle/gems/i18n-0.6.0/lib/i18n/interpolate/ruby.rb
classiccms-0.7.3 vendor/bundle/gems/i18n-0.6.0/lib/i18n/interpolate/ruby.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
i18n-0.5.4 lib/i18n/interpolate/ruby.rb
classiccms-0.7.2 vendor/bundle/gems/i18n-0.6.0/lib/i18n/interpolate/ruby.rb
classiccms-0.7.1 vendor/bundle/gems/i18n-0.6.0/lib/i18n/interpolate/ruby.rb
swipe-rails-0.0.5 vendor/bundle/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
classiccms-0.7.0 vendor/bundle/gems/i18n-0.6.0/lib/i18n/interpolate/ruby.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
challah-1.0.0.beta3 vendor/bundle/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb
challah-1.0.0.beta2 vendor/bundle/gems/i18n-0.6.1/lib/i18n/interpolate/ruby.rb