Skip to content

Instantly share code, notes, and snippets.

@pepicrft
Last active December 26, 2015 17:39
Show Gist options
  • Save pepicrft/7188957 to your computer and use it in GitHub Desktop.
Save pepicrft/7188957 to your computer and use it in GitHub Desktop.
NSLocalizedFinder finds all localized strings in your project and exports all of them into a valid .strings file
#!/usr/bin/env ruby
# This Script finds all NSLocalizedString in your project and exports them in a new file
# Detecting the number of arguments
abort("Error: #{ARGV.length} arguments passed / 2 Required") unless ARGV.length == 2
# Printing arguments
puts "Project folder: #{ARGV[0]}"
puts "Output file: #{ARGV[1]}"
# Checking if directory exist
abort("Error: Directory #{ARGV[0]} doesn't exist") unless File.directory?(ARGV[0])
# Opening output file
output = File.open( ARGV[1] , 'w' )
output << "// Localizable file generated using NSLocalizedFinder \n"
output << "// Tool created by @pepibumur github.com/pepibumur \n\n"
# Finding files
expressions = []
files = Dir.glob("#{ARGV[0]}**/*")
# Finding localized string
files.each { |file|
next if File.directory?(file)
begin
File.read(file).scan(/NSLocalizedString\(\s*\@\"([^,]*)\"\s*\,[^)]*\)/) { |localized_string|
expressions << localized_string[0] unless expressions.include?(localized_string[0])
}
rescue StandardError
next
end
}
# Saving to output file
expressions.each { |expression|
output << "\"#{expression}\"=\"\";\n"
}
# Closing output file
output.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment