iOS internationalization using Android xml file to oc multilingual file

Multiple languages need to be added to the project. Android character files are all put in. xml, which is well organized. iOS only needs to convert xml files into OC string files to use
Using Python 3 to write a script, you can modify the xml file and OC file, and then automatically read and write the xml file to OC file

  • This demo is not perfect enough. At present, only one language can be written at a time. It will be improved later if there is demand

  • In the project, xmltodict is used to read XML. The installation of xmltodict is very simple. You only need to input one line of command in the command line

    pip3 install xmltodict

Because Python 3 is used, pip3 is used. If Python 2 is used, pip can be used directly
About the installation of xmltodict

Project code

Main project code
Read XML

def readXML(path):
    ocString = []
    with open(path) as fd:

        obj = xmltodict.parse(
        resources =  obj["resources"]
        strings = resources["string"]
        nameKey = "@name"
        valueKey = "#text"
        for item in strings:
            value = ""
            key = ""
            for k, v in item.items():
                if k == nameKey:
                    key = v
                if k == valueKey:
                    value = v

            singleStr = "\""+key + "\"" + " = " + "\"" + value + "\"" + ";" + "\n"
    return ocString

Write in

def writeFileWithList(lines, filePath):
    with open(filePath, 'a') as f:
        for itme in lines:


import CCReadXMLToList
import CCWriteListToFile

directoryXML = "/Users/aaa/Desktop/String"  # xml path
fileNameXML = ['strings', 'strings1','strings2','strings3','strings4'] # xml file name
suffixXML = ".xml"

directoryOC = "/Users/aaa/Desktop/backup/test/test" #oc file path
fileNameOC = ["zh-Hans.lproj"]
suffixOC = "InfoPlist.strings"

pathOC =  directoryOC + "/" + fileNameOC[0] + "/" + suffixOC # The path of oc

for name in fileNameXML:
    path = directoryXML + "/" + name + suffixXML
    firstLine = "//"  + name + "\n\n\n"   # Android is divided into modules. I am going to write oc in a string file. In order to distinguish them, I added the file name and three line breaks before each Android module
    CCWriteListToFile.writeFileWithList([firstLine], pathOC)
    # result = CCWriteListToFile.readXML(path)
    result = CCReadXMLToList.readXML(path)
    CCWriteListToFile.writeFileWithList(result, pathOC)

Keywords: xml Android Python iOS

Added by Whetto on Wed, 01 Jan 2020 11:02:17 +0200