Skip to content

Instantly share code, notes, and snippets.

@kueda
Created May 19, 2014 21:32
Show Gist options
  • Save kueda/4047eb9b0288200d3769 to your computer and use it in GitHub Desktop.
Save kueda/4047eb9b0288200d3769 to your computer and use it in GitHub Desktop.
Find strings that need translating in an xcode project.
#!/usr/bin/env ruby
require 'rubygems'
require 'trollop'
require 'nokogiri'
require 'tmpdir'
opts = Trollop::options do
banner <<-EOS
Find strings that need translating in an xcode project.
Installation:
* drop this file in $PATH
* make sure the following gems are installed
* Ruby
* Rubygems
* Trollop
* Nokogiri
Usage:
cd path/to/dir/with/files/needing/transation/
xcode_pending_translations.rb
where [options] are:
EOS
opt :debug, "Print debug statements", :type => :boolean, :short => "-d"
end
OPTS = opts
def run(cmd)
puts "* Running #{cmd}" if OPTS[:debug]
system cmd
end
def load_translatable_hashes_from_paths(paths)
paths.inject({}) do |memo,path|
basename = "#{path.gsub(/[^A-z0-9]/, '-')}-#{Time.now.to_i}"
xml_plist_path = File.join(Dir::tmpdir, "#{basename}.xml")
run "plutil -convert xml1 -o - #{path} > #{xml_plist_path}"
doc = Nokogiri::XML(File.open(xml_plist_path))
if OPTS[:debug]
puts "* Generated plist"
end
translations = doc.search('//key').inject({}) do |m,key|
m[key.text] = key.next_element.text
m
end
memo[path] = translations
memo
end
end
def translations_for_pattern(pattern)
paths = Dir.glob("**/#{pattern}")
path_translations = load_translatable_hashes_from_paths(paths)
all_translations = {}
path_translations.each do |path,translations|
translations.each do |k,v|
all_translations[k] ||= v
end
end
path_translations.each do |path, translations|
puts "****************************************"
puts "* #{path}"
puts
all_translations.each do |k,v|
puts "\"#{k}\" = \"#{v}\";" unless translations[k]
end
puts
puts
end
end
%w(MainStoryboard.strings Localizable.strings InfoPlist.strings).each do |pattern|
translations_for_pattern(pattern)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment