Created
November 12, 2012 18:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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