Skip to content

Instantly share code, notes, and snippets.

@imesut
Created December 26, 2023 11:27
Show Gist options
  • Save imesut/ee435d13bd42c3aece8b118da640c446 to your computer and use it in GitHub Desktop.
Save imesut/ee435d13bd42c3aece8b118da640c446 to your computer and use it in GitHub Desktop.
A script to fetch localization from a tabular file format (Excel/CSV) and prepare iOS String Catalog compatible JSON.
import pandas as pd
import json
def prepareCSV():
fullList = []
df = pd.read_json("swiper.json")
strings = df["strings"]
print(strings)
for localization_key in strings.keys():
if strings[localization_key].keys().__contains__("localizations"):
contents = strings[localization_key]["localizations"]
row = [localization_key]
for lang in contents.keys(): #available langs
row.append(contents[lang]["stringUnit"]["value"])
print(row)
fullList.append(row)
pd.DataFrame(fullList).to_csv("export.csv")
# prepareCSV()
def updateLocalization():
imp = pd.read_csv("import.csv")
output = {}
strings = {}
cols = imp.keys()
rowCount = len(imp[cols[0]])
for i in range(rowCount):
localizationKey = imp[cols[0]][i]
print(localizationKey)
strings[localizationKey] = {"localizations" : {}}
for col in cols[1:]:
strings[localizationKey]["localizations"][col] = {"stringUnit" : {"state" : "translated", "value" : imp[col][i]}}
output["sourceLanguage"] = "en"
output["strings"] = strings
output["version"] = "1.0"
# Writing to sample.json
with open("output.json", "w", encoding='utf8') as outfile:
outfile.write(json.dumps(output, indent=2, ensure_ascii=False))
print()
# print(df)
updateLocalization()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment