My script takes a CSV file of strings and then creates an XML file for Android and a .strings file to be used for iOS. It separates each locale meaning that a file will be created for each language.
This has come in handy so far, making localising mobile apps easier. However, I am always seeking feedback so let me know what you think.
Input
The .csv is provided in the format: key, language1, language2
key,en,ru welcome_message,hello,здравствуйте thank_you_message,thank you,спасибо goodbye_message,goodbye,До свидания
Output
XML - Android
en_strings.xml
<?xml version="1.0" ?>
<resources>
<string name="welcome_message">hello</string>
<string name="thank_you_message">thank you</string>
<string name="goodbye_message">goodbye</string>
</resources>
ru_strings.xml
<?xml version="1.0" ?>
<resources>
<string name="welcome_message">здравствуйте</string>
<string name="thank_you_message">спасибо</string>
<string name="goodbye_message">До свидания</string>
</resources>
.Strings - iOS
en.strings
/* */
"welcome_message" = "hello";
/* */
"thank_you_message" = "thank you";
/* */
"goodbye_message" = "goodbye";
ru.strings
/* */
"welcome_message" = "здравствуйте";
/* */
"thank_you_message" = "спасибо";
/* */
"goodbye_message" = "До свидания";
At the moment I've just hardcoded the origin Strings.csv file and the user defines the target directory, this will be useful when integrating it into automatic builds etc.
# -*- coding: utf-8 -*-
# Script takes a csv and creates strings for Android (.xml) and iOS (.Strings).
# csv in the format [key, language1, langauge2 ......]
# usage - Python converter.py [FILEPATH]
import sys, os, getopt, csv, xml.etree.ElementTree as ET
from xml.dom import minidom
def prettify(elem):
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent="\t")
# Read in output directory
try:
sys.argv[1:]
fileDestination = sys.argv[1]
except IndexError:
print "Error: Please supply an output directory."
print "Usage: converter.py [FILEPATH]"
sys.exit()
# Create directory if it doesn't exists
if not os.path.exists(fileDestination):
os.makedirs(fileDestination)
# Read from csv
f = open('Strings.csv')
csv_f = csv.reader(f)
# Determine the number of languages from the csv
line1 = csv_f.next()
numberOfLocales = len(line1)
# Create strings for each language
for x in range(1, numberOfLocales):
#Returns to the start of the csv and ignores the first line
f.seek(0)
csv_f.next()
rowIndex = 0
# Android xml
resources = ET.Element("resources")
# Create iOS strings file
iOSFile = open(fileDestination+"/"+line1[x]+".Strings", "w+")
for row in csv_f:
++rowIndex
try:
# Write string to xml
ET.SubElement(resources, "string", name=row[0]).text = row[x].decode('utf-8')
# Write string to iOS .Strings
iOSFile.write("/* */\n")
iOSFile.write('"'+row[0]+'"'+ ' = ' + '"'+row[x]+'"' + ";\n")
iOSFile.write("\n")
except IndexError:
f.seek(0)
print "There is a problem with the csv file at row {}".format(rowIndex+1) + " with the language {}".format(line1[x])
r = list(csv_f)
print r[rowIndex]
sys.exit()
# Write to Android file
androidFile = open(fileDestination+"/"+line1[x]+"_strings.xml", "w+")
androidFile.write(prettify(resources).encode('utf-8'))
++rowIndex
, incremeneting like that is not Python – Caridorc Jul 10 at 20:03+
does nothing++
does nothing twice -> (i.e. nothing) – Caridorc Jul 10 at 20:06