Sha256: 1b78c099d464604c45339244577efd164abc02b605cb4bd2f24df44d598931ac

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module I18nScrewdriver
  class Translation < ActiveSupport::SafeBuffer
    attr_accessor :text, :options

    def self.new(text, options = {}, &block)
      translation = super(options[:raw] ? text : I18n.translate(I18nScrewdriver.for_key(text), options))
      translation.text = text
      translation.options = options

      if block
        urls = Array(block.call)
        urls_to_interpolate_count = translation.scan(/<<.+?>>/).count
        raise ArgumentError, "too few urls specified" if urls.count < urls_to_interpolate_count
        if urls.count > urls_to_interpolate_count
          raise ArgumentError, "expected extra url to be a hash intended for variable interpolation" unless urls.last.is_a?(Hash)
          translation = new(translation % urls.last, :raw => true)
        end
        translation.linkify(block.binding, urls)
      end

      translation
    end

    def linkify(binding, urls)
      context = binding ? eval('self', binding) : self
      keep_html_safety do
        gsub!(/<<.+?>>/).each_with_index do |text, index|
          context.instance_eval do
            link_to(text[2..-3], *urls[index])
          end
        end
      end
    end

    private

    def keep_html_safety
      html_safe = @html_safe
      yield
      @html_safe = html_safe
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i18n_screwdriver-3.0.1 lib/i18n_screwdriver/translation.rb
i18n_screwdriver-3.0.0 lib/i18n_screwdriver/translation.rb