Skip to content

Instantly share code, notes, and snippets.

@sonsongithub
Created July 2, 2013 05:50
Show Gist options
  • Select an option

  • Save sonsongithub/5907037 to your computer and use it in GitHub Desktop.

Select an option

Save sonsongithub/5907037 to your computer and use it in GitHub Desktop.
This code is an alternative to Lingua that does not work for Xcode 4.6.3....
#!/usr/bin/ruby
require 'optparse'
require 'kconv'
def update(filepath, dictionary)
puts filepath
fr = File::open(filepath, "r")
localizedSource = fr.read
fr.close
localizedDictionary = {}
localizedSource.scan(/\"(.+?)\" = \"(.+?)\";/).each{|result|
localizedDictionary[result[0]] = result[1]
}
new_entries = []
dictionary.each_pair{|k, v|
if localizedDictionary[k] == nil
new_entries.push(k)
end
}
fw = File::open(filepath, "w")
localizedDictionary.each_pair{|k, v|
fw.write("/* No comment provided by engineer. */\n")
fw.write(sprintf("\"%s\" = \"%s\";\n\n", k, v))
}
new_entries.each{|k|
fw.write("/* New entry */\n")
fw.write(sprintf("\"%s\" = \"%s\";\n\n", k, k))
}
fw.close
end
def main
argv = {}
OptionParser.new do |opt|
opt.on('--srcroot VALUE') do |v|
argv[:srcroot] = v
end
opt.parse!(ARGV)
end
p argv
if !argv[:srcroot]
return
end
path = argv[:srcroot] + "/**/*.m"
files = ""
Dir.glob(path).each{|file|
files = files + " " + file
}
puts files
`genstrings #{files} -o /tmp`
stringsSource = File::open("/tmp/Localizable.strings", "r").read
puts stringsSource = stringsSource.toutf8
dictionary = {}
stringsSource.scan(/\"(.+?)\" = \"(.+?)\";/).each{|result|
dictionary[result[0]] = result[1]
}
searchPath = argv[:srcroot] + "/**/Localizable.strings"
Dir.glob(searchPath).each{|stringsPath|
update(stringsPath, dictionary)
}
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment