Module: I18n::Inflector::Interpolate

Includes:
Config
Included in:
API
Defined in:
lib/i18n-inflector/interpolate.rb

Overview

This module contains methods for interpolating inflection patterns.

Constant Summary

Constants included from Config

Config::MULTI_REGEXP, Config::MULTI_RESTR, Config::OPTION_PREFIX, Config::OPTION_PREFIX_REGEXP, Config::PATTERN_REGEXP, Config::PATTERN_RESTR, Config::TOKENS_REGEXP, Config::TOKENS_RESTR

Instance Method Summary collapse

Methods included from Config

all_consts, gen_regexp, get_i18n_reserved_keys

Instance Method Details

#interpolate(string, locale, options = {}) ⇒ String

Interpolates inflection values in the given string using kinds given in options and a matching tokens.

ComplexPatternMalformed.new

Parameters:

  • string (String)

    the translation string containing patterns to interpolate

  • locale (String, Symbol)

    the locale identifier

  • options (Hash) (defaults to: {})

    the options

Options Hash (options):

Returns:

  • (String)

    the string with interpolated patterns

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/i18n-inflector/interpolate.rb', line 45

def interpolate(string, locale, options = {})
  @inflector_opt_cache = nil

  case string

  when String

    if locale.nil? || !inflected_locale?(locale)
      string.gsub(PATTERN_REGEXP) { Escapes::PATTERN[::Regexp.last_match(1)] ? ::Regexp.last_match(0) : ::Regexp.last_match(1) }
    elsif !string.include?(Markers::PATTERN)
      string
    else
      interpolate_core(string, locale, options)
    end

  when Hash

    if options[:inflector_traverses]
      string.merge(string) { |_k, v| interpolate(v, locale, options) }
    else
      string
    end

  when Array

    if options[:inflector_traverses]
      string.map { |v| interpolate(v, locale, options) }
    else
      string
    end

  when Symbol

    if options[:inflector_interpolate_symbols]
      r = interpolate(string.to_s, locale, options)
      begin
        r.to_sym
      rescue StandardError
        :' '
      end
    else
      string
    end

  else

    string

  end
end

#key_to_pattern(key) ⇒ String

This method creates an inflection pattern by collecting information contained in a key-based inflection data.

Parameters:

  • key (Hash)

    the given key

Returns:

  • (String)

    the inflection pattern



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/i18n-inflector/interpolate.rb', line 102

def key_to_pattern(key)
  key  = key.dup
  pref = key.delete(:@prefix).to_s
  suff = key.delete(:@suffix).to_s
  kind = key.delete(:@kind).to_s
  free = key.delete(:@free)
  free = free.nil? ? '' : "#{Operators::Tokens::OR}#{free}"

  "#{pref}#{Markers::PATTERN}#{kind}#{Markers::PATTERN_BEGIN}" <<
    (key.map { |k, v| "#{k}#{Operators::Tokens::ASSIGN}#{v}" }
      .join(Operators::Tokens::OR) + free + Markers::PATTERN_END + suff)
end