您的位置:首页 > 其它

多国语言strings.xml与excel表格互相转换

2015-05-09 08:16 423 查看
翻译只熟悉excel表格,因此做了互转的工具

#-*- coding=utf-8 -*-

import xml.etree.ElementTree
import sys

def fetchMap(path):
strmap={}
for line in file(path):
line=line.strip()
if '<string' in line:
pos0=line.find('name="')
pos1=line.find('"', pos0+6)
pos2=line.find('>', pos1)
pos3=line.find('</string>', pos2)
val=line[pos2+1:pos3].decode('utf8').encode("gb2312")
if "," in val: val='"%s"'%val
strmap[line[pos0+6:pos1]] = val
#    strmap={}
#    e = xml.etree.ElementTree.parse(path).getroot()
#    for item in e.findall('string'):
#        if item.text!=None:
#            strmap[item.attrib["name"]] = (item.text).encode("gb2312")
#        else:
#            strmap[item.attrib["name"]] = ""
return strmap

strmap=fetchMap('values/strings.xml')
strmap2=fetchMap('values-zh-rCN/strings.xml')

f_handler=open('out.csv', 'w')
sys.stdout=f_handler
for k,v in strmap.items():
if k in strmap2:
print "%s,%s,%s,"%(k,v,strmap2[k])
else:
print "%s,%s,,"%(k,v)


#-*- coding=utf-8 -*-

import sys
import csv
import os

def fetchMap(path):
strmap={}
for line in file(path):
line=line.strip()
if '<string' in line:
pos0=line.find('name="')
pos1=line.find('"', pos0+6)
pos2=line.find('>', pos1)
pos3=line.find('</string>', pos2)
val=line[pos2+1:pos3].decode('utf8').encode("gb2312")
if "," in val: val='"%s"'%val
strmap[line[pos0+6:pos1]] = val
return strmap

strmap=fetchMap('values/strings.xml')
csvmap={}
with open('strs.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
csvmap[row['id']]=row
#sys.stdout=f_handler

f0=file("values/strings1.xml", 'w')
if not os.path.exists('values-zh-rCN'): os.mkdir('values-zh-rCN')
f1=file("values-zh-rCN/strings.xml", 'w')
if not os.path.exists('values-pt-rPT'): os.mkdir('values-pt-rPT')
f2=file("values-pt-rPT/strings.xml", 'w')
if not os.path.exists('values-zh-rTW'): os.mkdir('values-zh-rTW')
f3=file("values-zh-rTW/strings.xml", 'w')
if not os.path.exists('values-ko-rKR'): os.mkdir('values-ko-rKR')
f4=file("values-ko-rKR/strings.xml", 'w')
for line in file('values/strings.xml'):
if '<string' in line:
pos0=line.find('name="')
pos1=line.find('"', pos0+6)
pos2=line.find('>', pos1)
pos3=line.find('</string>', pos2)
val=line[pos2+1:pos3].decode('utf8').encode("gb2312")
if "," in val: val='"%s"'%val
key=line[pos0+6:pos1]
if key not in csvmap: continue
row=csvmap[key]
#        'zh-rCN': '', 'en': '', 'pt-pt': '', 'zh-tw': '', 'ko-kr':
if len(row['en'])==0: continue
f0.write((line[:pos2+1]+row['en']+line[pos3:]))

if len(row['zh-rCN'])!=0:
f1.write((line[:pos2+1]+row['zh-rCN']+line[pos3:]))

if len(row['pt-pt'])!=0:
f2.write((line[:pos2+1]+row['pt-pt']+line[pos3:]))

if len(row['zh-tw'])!=0:
f3.write((line[:pos2+1]+row['zh-tw']+line[pos3:]))

if len(row['ko-kr'])!=0:
f4.write((line[:pos2+1]+row['ko-kr']+line[pos3:]))
else:
f0.write(line)
f1.write(line)
f2.write(line)
f3.write(line)
f4.write(line)
f0.close()
os.rename('values/string1.xml','values/string.xml')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: