Created
September 22, 2018 20:06
-
-
Save faruktoptas/f84445ac057d8bb25141eadd61109629 to your computer and use it in GitHub Desktop.
Generate Android/iOS localization files from a JSON file
This file contains 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
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