Sha256: 2bd12167f43fdeb1aba718b2a6ad8ec95b67927a3f66b0c681ff5336f2485a76

Contents?: true

Size: 829 Bytes

Versions: 1

Compression:

Stored size: 829 Bytes

Contents

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

    def self.new(text, options = {}, &block)
      super(I18n.translate(I18nScrewdriver.for_key(text), options)).tap do |translation|
        translation.text = text
        translation.options = options
        translation.linkify(block.binding, *block.call) if block
      end
    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

1 entries across 1 versions & 1 rubygems

Version Path
i18n_screwdriver-2.2.0 lib/i18n_screwdriver/translation.rb