-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_android.py
78 lines (65 loc) · 2.3 KB
/
generate_android.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import csv
import os
import xml.etree.ElementTree as ET
import shutil
# Define the CSV file path
csv_file = './source.csv'
# Define the output folder paths for each language
output_folders = {
'en': 'values',
'as': 'values-as',
'bho': 'values-bho',
'bn': 'values-bn',
'gu': 'values-gu',
'grt': 'values-grt',
'hi': 'values-hi',
'kn': 'values-kn',
'ml': 'values-ml',
'mr': 'values-mr',
'or': 'values-or',
'pa': 'values-pa',
'ta': 'values-ta',
'te': 'values-te',
'es': 'values-es'
}
shutil.rmtree("./android_res", ignore_errors=True)
def escape_quote( str_xml: str ):
str_xml = str_xml.replace("'", "\\'")
str_xml = str_xml.replace("\n", "")
return str_xml
# Read the CSV file
with open(csv_file, 'r') as file:
reader = csv.DictReader(file)
headers = reader.fieldnames
# Iterate over each row in the CSV file
for row in reader:
# Get the name and values from the row
name = row['name']
# if ame is blank, skip this row
if name == '':
continue
# Iterate over each language and its corresponding value
for lang in headers[1:]:
value = row[lang]
if value == '':
continue
# Create the XML file path
output_folder = "./android_res/"+ output_folders[lang]
os.makedirs(output_folder, exist_ok=True)
xml_file = os.path.join(output_folder, 'strings.xml')
# If the XML file already exists, parse it and append the new <string> element
if os.path.exists(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
else:
# If the XML file doesn't exist, create a new XML tree with root element 'resources'
root = ET.Element('resources')
# Create the 'string' element with the name attribute and text value
string_elem = ET.SubElement(root, 'string')
string_elem.set('name', name)
string_elem.text = escape_quote(value)
# Write the XML tree to the file
tree = ET.ElementTree(root)
ET.indent(tree, " ") # two space tab in
tree.write(xml_file, encoding='utf-8', xml_declaration=True)
print("XML files android_res successfully!")