Class: I18nScrewdriver::Translation

Inherits:
String
  • Object
show all
Defined in:
lib/i18n_screwdriver/translation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/i18n_screwdriver/translation.rb', line 3

def options
  @options
end

#textObject

Returns the value of attribute text.



3
4
5
# File 'lib/i18n_screwdriver/translation.rb', line 3

def text
  @text
end

Class Method Details

.new(text, options = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/i18n_screwdriver/translation.rb', line 5

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, "too many urls specified (#{urls.count} <> #{urls_to_interpolate_count})" unless urls.last.is_a?(Hash)
      translation = new(translation % urls.last, :raw => true)
    end
    translation.linkify(block.binding, urls)
  end

  translation
end

Instance Method Details

#linkify(binding, urls) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/i18n_screwdriver/translation.rb', line 24

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