Sha256: 1316c262f4db43221bbbbbc4255b040f9af72c82b040da79a9ec10dc577a80b3

Contents?: true

Size: 1.56 KB

Versions: 24

Compression:

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

24 entries across 22 versions & 8 rubygems

Version Path
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/i18n-1.8.11/lib/i18n/interpolate/ruby.rb
i18n-1.8.11 lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/interpolate/ruby.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/interpolate/ruby.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.10/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.5/lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/interpolate/ruby.rb
i18n-1.8.10 lib/i18n/interpolate/ruby.rb
i18n-1.8.9 lib/i18n/interpolate/ruby.rb
i18n-1.8.8 lib/i18n/interpolate/ruby.rb
i18n-1.8.7 lib/i18n/interpolate/ruby.rb
i18n-1.8.6 lib/i18n/interpolate/ruby.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.5/lib/i18n/interpolate/ruby.rb