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

翻译只熟悉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')


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值