lib/i18n_screwdriver/translation.rb in i18n_screwdriver-2.0.1 vs lib/i18n_screwdriver/translation.rb in i18n_screwdriver-2.1.0
- old
+ new
@@ -1,10 +1,30 @@
module I18nScrewdriver
- module Translation
- def _(text, options = {})
- I18n.translate(I18nScrewdriver.for_key(text), options)
+ class Translation < ActiveSupport::SafeBuffer
+ def self.new(text, options = {}, &block)
+ super(I18n.translate(I18nScrewdriver.for_key(text), options)).tap do |translation|
+ 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
-
-Object.send(:include, I18nScrewdriver::Translation)