class Autolang::TranslationEscaper

protects text from evil translation robots by ensuring phrases that should not be translated (Car|Engine, %{name}, …) stay untranslated

Attributes

escaped[RW]

Public Class Methods

new(text) click to toggle source
# File lib/autolang.rb, line 102
def initialize(text)
  @text = text
  self.escaped = escape_text
end

Public Instance Methods

unescape(translation) click to toggle source
# File lib/autolang.rb, line 107
def unescape(translation)
  remove_placeholders(translation)
end

Protected Instance Methods

add_placeholder(text,regex) click to toggle source

replace stuff that would get messed up in translation through a non-translateable placeholder

# File lib/autolang.rb, line 126
def add_placeholder(text,regex)
  if text =~ regex
    @placeholders << $1
    text = text.sub($1,"PH#{@placeholders.length-1}")
  end
  text
end
escape_text() click to toggle source
# File lib/autolang.rb, line 113
def escape_text
  @placeholders = []
  text = @text
  if text =~ /^([^\s]+\|)/
    @cut_off = $1
    text = text.sub($1,'')
  end
  text = add_placeholder(text, /(%\{.+\})/ )
  text = text.gsub('&','and')#& cannot be translated
end
remove_placeholders(text) click to toggle source

swap placeholders with original values

# File lib/autolang.rb, line 135
def remove_placeholders(text)
  @placeholders.each_index do |i|
    replaced = @placeholders[i]
    text = text.sub("PH#{i}",replaced)
  end
  text
end