Sha256: 4daecd286d180e30bf01e4f31697ac676ed63f8eaabc9e23cf5500e27fef14ee

Contents?: true

Size: 939 Bytes

Versions: 2

Compression:

Stored size: 939 Bytes

Contents

module It
  class Parser
    attr_accessor :string, :options

    INTERPOLATION_REGEXP = /%\{[^{}}]+\}/

    def initialize(string, options)
      self.string = string
      self.options = options
    end

    def process
      handle_pluralization
      escape_string

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

    private
    def has_interpolation?
      string =~ INTERPOLATION_REGEXP
    end

    def handle_pluralization
      self.string = I18n.backend.send(:pluralize, (options['locale'] || I18n.locale), string, options['count']) if string.is_a?(Hash) && options['count']
    end

    def escape_string
      self.string = String.new(ERB::Util.h(string))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
it-0.2.5 lib/it/parser.rb
it-0.2.4 lib/it/parser.rb