Sha256: 018825ffad46af3e308a2cfd0c620a9473018a463d1a8b85cdc8250cc41d5ae3

Contents?: true

Size: 1.93 KB

Versions: 21

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

# 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*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
  ].freeze
  INTERPOLATION_PATTERN = Regexp.union(DEFAULT_INTERPOLATION_PATTERNS)
  deprecate_constant :INTERPOLATION_PATTERN

  INTERPOLATION_PATTERNS_CACHE = Hash.new do |hash, patterns|
    hash[patterns] = Regexp.union(patterns)
  end
  private_constant :INTERPOLATION_PATTERNS_CACHE

  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 =~ I18n.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)
      pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns]
      interpolated = false

      interpolated_string = string.gsub(pattern) do |match|
        interpolated = true

        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

      interpolated ? interpolated_string : string
    end
  end
end

Version data entries

21 entries across 21 versions & 9 rubygems

Version Path
i18n-1.14.6 lib/i18n/interpolate/ruby.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.5/lib/i18n/interpolate/ruby.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/lib/i18n/interpolate/ruby.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/lib/i18n/interpolate/ruby.rb
i18n-1.14.5 lib/i18n/interpolate/ruby.rb
i18n-1.14.4 lib/i18n/interpolate/ruby.rb
i18n-1.14.3 lib/i18n/interpolate/ruby.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/interpolate/ruby.rb
i18n-1.14.1 lib/i18n/interpolate/ruby.rb
i18n-1.14.0 lib/i18n/interpolate/ruby.rb