前提:在项目中,尤其是制作定制项目,经常需要做多国语言的翻译。对比传统的肉眼对比,通过一个简单的脚本进行对比可以有效的提高对比效率。
本工具基于Pyhon,配合.bat批处理工具,可以有效的进行字符串对比。
对比逻辑代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Android国际化: 将xml文件中对应的字符串解析到excel中
import xml.dom.minidom
from xml.dom import minidom
from xlwt import Workbook
import codecs
import sys
import os
def operatertempname):
#新建一个workbook
book = Workbookencoding='utf-8')
sheet = book.add_sheet'Android')
#打开xml
xmldoc = xml.dom.minidom.parse'xmlSource.xml')
code = xmldoc.getElementsByTagName'string')
#打开文件2
xmldoc2 = xml.dom.minidom.parse'xmlCompare.xml')
code2 = xmldoc2.getElementsByTagName'string')
#create new xml
doc = minidom.Document)
#add root element
resources = doc.createElement'resources')
doc.appendChildresources)
for node in code:
for item in node.childNodes:
stringName=node.getAttribute'name')
stringValue= item.data
for node2 in code2:
for item2 in node2.childNodes:
#获取name字段
stringName2=node2.getAttribute'name')
stringValue2= item2.data
#对比替换
ifstringName==stringName2):
printstringName2)
printstringValue2)
stringValue = stringValue2
print"
")
break
text_element=doc.createElement'string')
text_element.setAttribute'name',stringName)
text_element.appendChilddoc.createTextNodestringValue))
resources.appendChildtext_element)
xmlFile = "xmlEnd.xml"
printxmlFile)
f = codecs.openxmlFile,'w',encoding='utf-8')
f.writedoc.toprettyxmlindent=' '))
f.close)
def main):
#tempName = sys.argv[1]
#tempName = tempName.strip'.xml')
tempName="test"
print"find:"+tempName)
operatertempName)
if__name__ == "__main__"):
main)
批处理代码:
@echo off & setlocal EnableDelayedExpansion
rem 执行脚本
call python xml2xml.py
备注:
1.xmlSource 是需要更新的原始文件。
2.xmlCompare是提供匹配更新的文件。
3.xmlEnd是最终匹配完成之后的文件(可删除)。
4.按规定命名好之后,执行translate2xml.bat即可完成字符串替换。
