Module: IosLocalizer
- Defined in:
- lib/ios_localizer.rb,
lib/ios_localizer/version.rb
Defined Under Namespace
Classes: HelperMethods
Constant Summary collapse
- VERSION =
"0.0.1.2"
Class Method Summary collapse
Class Method Details
.localize(key, proj_dir, source, skip) ⇒ Object
41 42 43 44 45 46 47 48 49 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ios_localizer.rb', line 41 def IosLocalizer.localize(key, proj_dir, source, skip) h = HelperMethods.new source_strings_file = proj_dir + "/" + source + ".lproj/Localizable.strings" #Get languages lurl = 'https://www.googleapis.com/language/translate/v2/languages?key=' + key ldata = h.getDataFromURL (lurl) if ldata.has_key? "error" if ldata["error"]["errors"][0]["reason"] == "keyInvalid" puts "ERROR: Invalid API Key" exit else puts "ERROR: web service error" exit end end if ldata.count == 0 puts "Invalid API key" exit (1) end languages = Array.new ldata["data"]["languages"].each do |language| unless language["language"].eql? source unless skip.include? language["language"] lproj_dir = proj_dir + "/" +language["language"] +".lproj" if h.directory_exists?(lproj_dir) languages << language["language"] end end end end #Generate source language file gen_strings_command = "find " + proj_dir + "/ -name *.m -print0 | xargs -0 genstrings -o " + proj_dir + "/" + source + ".lproj" system(gen_strings_command) #Get words that need to be translated words = Array.new comment_str = "/*" src = File.open(source_strings_file, "rb:UTF-16LE") while (line = src.gets) line = line.encode('UTF-8') if h.should_line_be_translated(line) words << h.extract_word(line) end end #Prepare Translate Query turl = "https://www.googleapis.com/language/translate/v2?key=" + key + "&source=" + source words.each do |word| turl += "&q=" + CGI::escape(word) end #Translate Words & write new file coder = HTMLEntities.new languages.each do |lang| lang_strings_file = proj_dir + "/" + lang + ".lproj/Localizable.strings" dest = File.open(lang_strings_file, "w") translate_url = turl + "&target=" + lang tdata = h.getDataFromURL(translate_url) i = 0 src = File.open(source_strings_file, "rb:UTF-16LE") while (line = src.gets) line = line.encode('UTF-8') if h.should_line_be_translated(line) extracted_word = (h.extract_word(line)) line = line.reverse.sub(extracted_word.reverse, coder.decode(tdata["data"]["translations"][i]["translatedText"]).reverse).reverse dest.syswrite(line) i += 1 else dest.syswrite(line) end end end end |