Class: Xlocalize::Executor
- Inherits:
-
Object
- Object
- Xlocalize::Executor
- Defined in:
- lib/xlocalize/executor.rb
Instance Method Summary collapse
- #download(wti, locales) ⇒ Object
- #export_master(wti, project, target, excl_prefix, master_lang) ⇒ Object
- #import(locales) ⇒ Object
- #import_plurals_if_needed(locale) ⇒ Object
- #import_xliff(locale) ⇒ Object
- #locale_file_name(locale) ⇒ Object
- #plurals_file_name(locale) ⇒ Object
- #purelyze(locale, target, excl_prefix, project) ⇒ Object
Instance Method Details
#download(wti, locales) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/xlocalize/executor.rb', line 98 def download(wti, locales) begin locales.each do |locale| puts "Downloading translations for #{locale}" translations = wti.pull(locale) File.open("#{locale}.xliff", "w") {|file| file.write(translations['xliff']) puts "Done saving xliff.".green } if !translations['plurals'].nil? File.open("#{locale}_plurals.yaml", "w") {|file| file.write(translations['plurals']) puts "Done saving plurals.".green } end end rescue => err puts err.to_s.red end end |
#export_master(wti, project, target, excl_prefix, master_lang) ⇒ Object
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 47 48 |
# File 'lib/xlocalize/executor.rb', line 19 def export_master(wti, project, target, excl_prefix, master_lang) master_file_name = locale_file_name(master_lang) # hacky way to finish xcodebuild -exportLocalizations script, because # since Xcode7.3 & OS X Sierra script hangs even though it produces # xliff output # http://www.openradar.me/25857436 File.delete(master_file_name) if File.exist?(master_file_name) system "xcodebuild -exportLocalizations -localizationPath ./ -project #{project} & sleep 0" while !File.exist?(master_file_name) do sleep(1) end purelyze(master_lang, target, excl_prefix, project) # Pushing master file to WebtranslateIt begin puts "Uploading master file to WebtranslateIt" file = File.open(master_file_name, 'r') plurals_path = plurals_file_name(master_lang) plurals_file = File.exist?(plurals_path) ? File.open(plurals_path, 'r') : nil wti.push_master(file, plurals_file) puts "Done.".green rescue => err puts err.to_s.red ensure file.close unless file.nil? plurals_file.close unless plurals_file.nil? end if !wti.nil? end |
#import(locales) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/xlocalize/executor.rb', line 174 def import(locales) puts 'Importing translations' locales.each do |locale| import_xliff(locale) import_plurals_if_needed(locale) puts "Done #{locale}".green end end |
#import_plurals_if_needed(locale) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/xlocalize/executor.rb', line 155 def import_plurals_if_needed(locale) plurals_fname = "#{locale}_plurals.yaml" return if !File.exist?(plurals_fname) plurals_yml = YAML.load_file(plurals_fname) plurals_yml[locale].each do |fname, trans_units| content = {} trans_units.each do |key, vals| content[key] = { "NSStringLocalizedFormatKey" => "%\#@value@", "value" => vals.merge({ "NSStringFormatSpecTypeKey" => "NSStringPluralRuleType", "NSStringFormatValueTypeKey" => "d" }) } end File.open(fname, 'w') { |f| f.write content.to_plist } end end |
#import_xliff(locale) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/xlocalize/executor.rb', line 121 def import_xliff(locale) doc = Nokogiri::XML(open("#{locale}.xliff")) doc.xpath("//xmlns:file").each do |node| file_name = node["original"] parts = file_name.split('/') name = "" parts.each_with_index do |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 end File.open(name, "w") do |file| (node > "body > trans-unit").each do |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 end end |
#locale_file_name(locale) ⇒ Object
15 16 17 |
# File 'lib/xlocalize/executor.rb', line 15 def locale_file_name(locale) return "#{locale}.xliff" end |
#plurals_file_name(locale) ⇒ Object
11 12 13 |
# File 'lib/xlocalize/executor.rb', line 11 def plurals_file_name(locale) return locale_file_name(locale) << '_plurals.yml' end |
#purelyze(locale, target, excl_prefix, project) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 |
# File 'lib/xlocalize/executor.rb', line 50 def purelyze(locale, target, excl_prefix, project) locale_file_name = locale_file_name(locale) 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 "Filtering plurals" plurals = {} doc.xpath("//xmlns:file").each { |node| fname = node["original"] next if !fname.end_with?(".strings") fname_stringsdict = fname << 'dict' file_full_path = Pathname.new(project).split.first.to_s << '/' << fname_stringsdict next if !File.exist?(file_full_path) Plist::parse_xml(file_full_path).each do |key, val| values = val["value"] transl = values.select { |k, v| ['zero', 'one', 'few', 'other'].include?(k) } plurals[fname_stringsdict] = {key => transl} sel = 'body > trans-unit[id="' << key << '"]' node.css(sel).remove end } 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') { |f| f.write(doc.to_xml) } if !plurals.empty? puts "Writing plurals to plurals YAML file" File.open(plurals_file_name(locale), 'w') { |f| f.write({locale => plurals}.to_yaml) } end end |