class It::Parser

Parses the string and replaces all interpolations accordingly.

Constants

INTERPOLATION_REGEXP

Attributes

options[R]
string[R]

Public Class Methods

backend_options(options) click to toggle source
# File lib/it/parser.rb, line 8
def self.backend_options(options)
  options.with_indifferent_access.slice(:default, :locale, :scope)
end
new(string, options) click to toggle source
# File lib/it/parser.rb, line 12
def initialize(string, options)
  @string = string
  @options = options
end

Public Instance Methods

process() click to toggle source
# File lib/it/parser.rb, line 17
def process
  handle_pluralization
  escape_string

  # For deep nesting, we repeat the process until we have no interpolations anymore
  while contains_interpolation?
    string.gsub!(INTERPOLATION_REGEXP) do |interpolation|
      Interpolation.call(interpolation, options)
    end
  end
  string.html_safe
end

Private Instance Methods

contains_interpolation?() click to toggle source
# File lib/it/parser.rb, line 32
def contains_interpolation?
  string =~ INTERPOLATION_REGEXP
end
escape_string() click to toggle source
# File lib/it/parser.rb, line 46
def escape_string
  @string = String.new(ERB::Util.h(string))
end
handle_pluralization() click to toggle source
# File lib/it/parser.rb, line 36
def handle_pluralization
  return if !string.is_a?(Hash) || !options.key?('count')

  @string = I18n.backend.send(:pluralize, locale, string, options['count'])
end
locale() click to toggle source
# File lib/it/parser.rb, line 42
def locale
  options['locale'] || I18n.locale
end