Class: LocaleBase::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/locale_base/translator.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = []) ⇒ Translator

Initialize a new LocaleBase for translating locale files

hash The object to convert



9
10
11
# File 'lib/locale_base/translator.rb', line 9

def initialize(hash = [])
  @original = hash
end

Instance Method Details

#translate(options = {}) ⇒ Object

place track tokens translate all of them and then replace the tokens with their string equivelants



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/locale_base/translator.rb', line 16

def translate(options = {})

  tracker = TranslationTracker.new
  
  # we use divs here instead of spans so that google doesn't mess us up
  # by combining spans on requests
  @created = crazy_walk(@original) do |obj|
    obj.gsub!(/\{\{([^\}]+)\}\}/) do |m|
      "<div class='notranslate'>#{tracker.hold($1)}</div>"
    end
    # insert trackers
    tracker.track(obj)
  end

  # translate everything
  tracker.translate_all(options)
  
  # replace the tokens with the tranlated text
  @created = crazy_walk(@created) do |obj|
    obj = tracker.retrieve(obj) if obj.is_a?(TrackingToken)
    # remove divs and replace with hold text - don't let google near this stuff
    # whitespace split also needed cause google messess with our shit
    obj.gsub!('><', '> <')
    obj.gsub!(/<div\sclass='notranslate'>([^<]+)<\/div>/) do |m|
      "{{#{tracker.unhold($1)}}}"
    end
    
    obj
  end

end