zoukankan      html  css  js  c++  java
  • 字符串对比工具--适用于多国语言的字符串翻译

    前提:在项目中,尤其是制作定制项目,经常需要做多国语言的翻译。对比传统的肉眼对比,通过一个简单的脚本进行对比可以有效的提高对比效率。

    本工具基于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 operater(tempname):
    	#新建一个workbook
    	book = Workbook(encoding='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.appendChild(resources)
    
    	
    	
    	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
    				#对比替换
    	     			if(stringName==stringName2):
    	     				print(stringName2)
    	     				print(stringValue2)
    	     				stringValue = stringValue2
    	     				print("
    ")
    	     				break
    	     	text_element=doc.createElement('string')
    	     	text_element.setAttribute('name',stringName)
    	     	text_element.appendChild(doc.createTextNode(stringValue))
    	     	resources.appendChild(text_element)
    	xmlFile = "xmlEnd.xml"
    	print(xmlFile)
    	f = codecs.open(xmlFile,'w',encoding='utf-8')
    	f.write(doc.toprettyxml(indent='    '))
    	f.close()
    
    
    def main():
    	#tempName = sys.argv[1]
    	#tempName = tempName.strip('.xml')
    	tempName="test"
    	print("find:"+tempName)
    	operater(tempName)
    
    if(__name__ == "__main__"):
        main()
    

      

      批处理代码:

    @echo off & setlocal EnableDelayedExpansion
    rem 执行脚本
    call python xml2xml.py

        备注:

    1.xmlSource 是需要更新的原始文件。
    2.xmlCompare是提供匹配更新的文件。
    3.xmlEnd是最终匹配完成之后的文件(可删除)。
    4.按规定命名好之后,执行translate2xml.bat即可完成字符串替换。
  • 相关阅读:
    python3初识selenium
    [lucene系列笔记3]用socket把lucene做成一个web服务
    [lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能
    [lucene系列笔记1]lucene6的安装与配置(Windows系统)
    JAVA SOCKET
    Python3 urlparse
    Windows Socket 编程_ 简单的服务器/客户端程序
    linux软件包管理
    linux用户及权限管理
    docker搭建私有仓库
  • 原文地址:https://www.cnblogs.com/jianggest/p/string_compare.html
Copyright © 2011-2022 走看看