class I18nScrewdriver::Translation

Constants

Error

Attributes

options[RW]
text[RW]

Public Class Methods

application_frames(backtrace) click to toggle source
# File lib/i18n_screwdriver/translation.rb, line 27
def self.application_frames(backtrace)
  backtrace.select{ |path| path.starts_with?(::Rails.root.to_s) }
end
emit_warning(message) click to toggle source
# File lib/i18n_screwdriver/translation.rb, line 22
def self.emit_warning(message)
  raise Error, message unless ::Rails.env.production?
  ::Rails.logger.warn(%|I18nScrewdriver: #{message}\n#{application_frames(caller).join("\n")}|)
end
new(text, **options, &block) click to toggle source
Calls superclass method
# File lib/i18n_screwdriver/translation.rb, line 7
def self.new(text, **options, &block)
  translation = super(options[:raw] ? text : I18nScrewdriver.translate(text, **options))
  translation.text = text
  translation.options = options

  if block
    urls = Array(block.call)
    urls_to_interpolate_count = translation.scan(/<<.+?>>/).count
    emit_warning("invalid number of urls specified (#{urls.count} <> #{urls_to_interpolate_count})") unless urls.count == urls_to_interpolate_count
    translation.linkify(block.binding, urls)
  end

  translation
end

Public Instance Methods

linkify(binding, urls) click to toggle source
# File lib/i18n_screwdriver/translation.rb, line 31
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 Instance Methods

keep_html_safety() { || ... } click to toggle source
# File lib/i18n_screwdriver/translation.rb, line 44
def keep_html_safety
  html_safe = @html_safe
  yield
  @html_safe = html_safe
  self
end