Skip to content

Instantly share code, notes, and snippets.

@rylev
Created November 12, 2012 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rylev/4060907 to your computer and use it in GitHub Desktop.
Save rylev/4060907 to your computer and use it in GitHub Desktop.
A simple ruby class that parses .strings files (used in OSX and iOS for translations) and coverts it to a Ruby hash.
class DotStringsParser
REGEX_QUOTED = /\"((\\\"|[^\"])+)\"/
REGEX_KEY_VALUE = /#{REGEX_QUOTED}\s*=\s*#{REGEX_QUOTED}/
def initialize
@hash = {}
end
def convert_to_array(line)
if pair_match = line.match(REGEX_KEY_VALUE)
pair_string = pair_match.to_s
pair_array = pair_string.partition(/\s*=\s*/)
pair_array.map do |index|
index.gsub!(/(^"|"$)/, "")
end
@hash[pair_array[0]] = pair_array[2]
end
end
def to_hash(file)
File.open(file, "r") do |file|
content = file.read
content.each_line do |line|
convert_to_array line
end
end
@hash
end
def self.parse
new
end
end
require 'json'
hash = DotStringsParser.parse.to_hash('/Users/ryanlevick/Sites/work/master-translation/master.strings')
print hash.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment