forked from CatchZeng/Localizable.strings2Excel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringsXmlFileUtil.py
60 lines (46 loc) · 1.68 KB
/
StringsXmlFileUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
from Log import Log
import xml.dom.minidom
import re
class StringsXmlFileUtil:
'android strings.xml file util'
@staticmethod
def writeToFile(keys, values,directory,additional):
if not os.path.exists(directory):
os.makedirs(directory)
Log.info("Creating android file:" + directory + "/strings.xml")
fo = open(directory + "/strings.xml", "wb")
stringEncoding = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n"
fo.write(stringEncoding)
for x in range(len(keys)):
if values[x] is None or values[x] == '' :
Log.error("Key:" + keys[x] + "\'s value is None. Index:" + str(x + 1))
continue
key = keys[x].strip()
value = re.sub(r'(%\d\$)(@)', r'\1s', values[x])
content = " <string name=\"" + key + "\">" + value + "</string>\n"
fo.write(content);
if additional is not None:
fo.write(additional)
fo.write("</resources>");
fo.close()
@staticmethod
def getKeysAndValues(path):
if path is None:
Log.error('file path is None')
return
dom = xml.dom.minidom.parse(path)
root = dom.documentElement
itemlist = root.getElementsByTagName('string')
keys = []
values = []
for index in range(len(itemlist)):
item = itemlist[index]
key = item.getAttribute("name")
value = item.firstChild.data
Log.info("key:" + key + " value:" + value)
keys.append(key)
values.append(value)
return (keys,values)