Skip to content

Instantly share code, notes, and snippets.

@faruktoptas
Created September 22, 2018 20:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faruktoptas/f84445ac057d8bb25141eadd61109629 to your computer and use it in GitHub Desktop.
Save faruktoptas/f84445ac057d8bb25141eadd61109629 to your computer and use it in GitHub Desktop.
Generate Android/iOS localization files from a JSON file
import sys,json,os, collections
infile = 'res.json'
def __main__():
if (len(sys.argv) < 3):
print "Usage: \npython gen.py [android | ios] outfile"
sys.exit()
type = sys.argv[1]
if (type != "android" and type != "ios" ):
print "Unknown type: " + type
sys.exit()
outfile = sys.argv[2]
generate(type, outfile)
def validate():
keys = []
with open(infile) as data_file:
data = json.load(data_file)
keys = []
for item in data:
if (keys.co)
def generate(os, outfile):
data = []
out = ""
with open(infile) as data_file:
data = json.load(data_file)
count = 0
for item in data:
if (len(item[os]) >0 ):
count += 1
if (os == "android"):
out += formatAndroid(item)
elif (os == "ios"):
out += formatIos(item)
w = open(outfile, 'w')
w.write(out)
w.close()
print outfile + " created with " + str(count) + " strings"
def formatAndroid(item):
return '<string name="' + item["key"].lower() + '">"'+item["android"]+'"</string>\n'
def formatIos(item):
return '"' + item["key"].lower() + '" = "'+item["ios"]+'";\n'
#generate("android")
#generate("ios")
__main__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment