Skip to content

Instantly share code, notes, and snippets.

@kueda
Created May 19, 2014 21:30
Show Gist options
  • Select an option

  • Save kueda/a0742d08ae5bcc03637e to your computer and use it in GitHub Desktop.

Select an option

Save kueda/a0742d08ae5bcc03637e to your computer and use it in GitHub Desktop.

Revisions

  1. kueda created this gist May 19, 2014.
    46 changes: 46 additions & 0 deletions xibstrings.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #!/usr/bin/env ruby
    require 'rubygems'
    require 'trollop'
    require 'nokogiri'
    require 'tmpdir'

    opts = Trollop::options do
    banner <<-EOS
    Spit out text from a .xib or .storyboard file as it appears in xcode. Useful
    for diffing and working with translations.
    Usage:
    ruby xibstrings.rb path/to/file.storyboard
    Dependencies:
    * Ruby
    * Rubygems
    * Trollop
    * Nokogiri
    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

    basename = "storyboardstrings-#{Time.now.to_i}"
    binary_plist_path = File.join(Dir::tmpdir, "#{basename}.strings")
    xml_plist_path = File.join(Dir::tmpdir, "#{basename}.xml")
    xib_path = ARGV[0]
    run "ibtool #{xib_path} --generate-strings #{binary_plist_path}"
    run "plutil -convert xml1 -o - #{binary_plist_path} > #{xml_plist_path}"
    doc = Nokogiri::XML(File.open(xml_plist_path))
    if OPTS[:debug]
    puts "* Generated plist"
    end
    doc.search('//key').each do |key|
    puts "\"#{key.text}\" = \"#{key.next_element.text}\";"
    end