Skip to content

Instantly share code, notes, and snippets.

@mmdock
Last active June 18, 2019 22:16
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 mmdock/d870086648a45354242485a80af80b77 to your computer and use it in GitHub Desktop.
Save mmdock/d870086648a45354242485a80af80b77 to your computer and use it in GitHub Desktop.
A simple file to parse out the value strings from a given key:value separated localization file (assuming 1 per line) using a given separator character. Basically a line splitter for a given file.
#!/usr/bin/env python
import codecs
import optparse
import os
import re
"""
This small script extracts values from a simple key:value localization file.
"""
# the default local file if no options are provided
default_inputName = "./Localizable.strings"
default_separator = "="
def openWithProperEncoding(path):
if not os.path.isfile(path):
return []
try:
f = codecs.open(path, 'r', 'utf-16')
lines = f.read().splitlines()
f.close()
except UnicodeError:
f = codecs.open(path, 'r', 'utf-8')
lines = f.read().splitlines()
f.close()
return lines
def findValues(path, sep):
keys = []
keyTable = {}
for line in openWithProperEncoding(path):
m = line.split(sep)
print(m)
if m and len(m) > 1:
source = m[0]
keys.append(source)
keyTable[source] = m[1].strip()
return (keys, keyTable)
def findKeys(path, sep):
reString = re.compile(r'\s*((\\.|.)+?)\s*%s\s*"(.+?)"(;|\n)'%sep)
keys = []
keyTable = {}
for line in openWithProperEncoding(path):
m = reString.search(line)
if m:
source = m.groups()[0]
keys.append(source)
keyTable[source] = m.groups()[2].strip()
return (keys, keyTable)
def extractStrings(lc1, sep):
error = False
keys, keyTable = findValues(lc1, sep)
for key in keys:
print(keyTable.get(key))
def parseOptions():
usage = """usage: %prog [options]
prep extracts the language strings in need of a new translation from the base language's key:value file.
The output will be the string values only. Save output to your desired destination file."""
parser = optparse.OptionParser(usage)
parser.set_defaults(preprocessedFile=default_inputName, sep=default_separator)
parser.add_option("-f", "--filename", dest="preprocessedFile", type="str",
help = "The localization filename to extract strings from.")
parser.add_option("-s", "--sep", dest="sep", type="str",
help = "The key:value separator component. example: <Key> = <String> then \"=\" is the separator.")
options, arguments = parser.parse_args()
return options
def main():
options = parseOptions()
if options.preprocessedFile and options.sep:
extractStrings(options.preprocessedFile, options.sep)
if __name__ == "__main__":
main()
@mmdock
Copy link
Author

mmdock commented Jun 18, 2019

Wrote this because I was asked to get all the strings from a yml and and iOS app's strings file so that we could translated it all into a new language, and our localization db service, Onesky, only lets you download the Localization formatted with the Keys. (We used Unbabel for translations instead of OneSky because it was greatly less expensive to do so, but we stored the values in OneSky because we could integrate that into our devops flows via Fastlane, etc). Would take too long to manually edit everything. So, wrote this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment