Class: Xlocalize::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/xlocalize/executor.rb

Instance Method Summary collapse

Instance Method Details

#download(wti, locales) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xlocalize/executor.rb', line 54

def download(wti, locales)
  begin
    locales.each do |locale|
      puts "Downloading localized file for #{locale} translation"
      File.open("#{locale}.xliff", "w") {|file|
        wti.pull(file, locale)
        puts "Done.".green
      }
    end
  rescue => err
    puts err.to_s.red
  end
end

#export_master(wti, project, target, excl_prefix, master_lang) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xlocalize/executor.rb', line 12

def export_master(wti, project, target, excl_prefix, master_lang)
  master_file_name = locale_file_name(master_lang)
  
  system "xcodebuild -exportLocalizations -localizationPath ./ -project #{project}"
  purelyze(master_file_name, target, excl_prefix)

  # Pushing master file to WebtranslateIt
  begin
    puts "Uploading master file to WebtranslateIt"
    File.open(master_file_name, "r") {|file|
      wti.push_master(file)
      puts "Done.".green
    }
  rescue => err
    puts err.to_s.red
  end if !wti.nil?
end

#import(locales) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xlocalize/executor.rb', line 68

def import(locales)
  locales.each do |locale|
    doc = Nokogiri::XML(open("#{locale}.xliff"))

    doc.xpath("//xmlns:file").each { |node|
      file_name = node["original"]
      parts = file_name.split('/')
      name = ""
      parts.each_with_index {|part, idx|
        name += "/" if idx > 0
        if part.end_with?(".lproj")
          name += "#{locale}.lproj"
        elsif idx+1 == parts.count
          # TODO: join all parts till the last '.'
          name += "#{part.split('.')[0]}.strings"
        else
          name += part
        end
      }
      
      File.open(name, "w") {|file|
        (node > "body > trans-unit").each {|trans_unit|
          key = trans_unit["id"]
          target = (trans_unit > "target").text
          note = (trans_unit > "note").text
          note = "(No Commment)" if note.length <= 0
          
          file.write "/* #{note} */\n"
          file.write "\"#{key}\" = #{target.inspect};\n\n"
        }
      }

    }
  end
end

#locale_file_name(locale) ⇒ Object



8
9
10
# File 'lib/xlocalize/executor.rb', line 8

def locale_file_name(locale)
  return "#{locale}.xliff"
end

#purelyze(locale_file_name, target, excl_prefix) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/xlocalize/executor.rb', line 30

def purelyze(locale_file_name, target, excl_prefix)
  target_prefix = "#{target}/"
  doc = Nokogiri::XML(open(locale_file_name))

  puts "Removing all files not matching required targets"
  doc.xpath("//xmlns:file").each { |node|
    fname = node["original"]
    node.remove if !fname.start_with?(target_prefix) || !fname.include?(".lproj/")
  }

  puts "Removing trans-unit's having reserverd prefix in their sources"
  doc.xpath("//xmlns:source").each { |node|
    node.parent.remove if node.content.start_with?(excl_prefix)
  }

  puts "Removing all files having no trans-unit elements after removal"
  doc.xpath("//xmlns:body").each { |node|
    node.parent.remove if node.elements.count == 0
  }

  puts "Writing modified XLIFF file to #{locale_file_name}"
  File.open(locale_file_name, "w") {|file| file.write(doc.to_xml) }
end