-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimport.py
executable file
·125 lines (99 loc) · 4.61 KB
/
import.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
import os
import sys
import pandas as pd
import xml.etree.ElementTree as ET
import xml.dom.minidom
def print_help():
help_message = """
Usage:
python import.py [INPUT_XLSX_FILE] [OUTPUT_APP_SRC_MAIN_RES_FOLDER]
Description:
This script converts an Excel file containing string translations into
Android-compatible strings.xml files.
- INPUT_XLSX_FILE: Path to the Excel file to process (default: 'translations.xlsx').
- OUTPUT_APP_SRC_MAIN_RES_FOLDER: Path to the output directory where the
strings.xml files will be generated into ('values-{LANGUAGE}' subfolders) (default: './app/src/main/res/').
Notes:
- The Excel file should have the following columns:
* "Key": The name of the string resource.
* "Translatable": A boolean indicating whether the string is translatable.
* Language columns ("values-{LANGUAGE}"): Columns representing different language translations.
- Plural keys should have the format `key_PLURALS_{quantity}` (e.g., `app_name_PLURALS_one`).
- Non-translatable strings will include the attribute translatable="false" in the generated XML files.
Example:
python import.py translations.xlsx ./app/src/main/res/
"""
print(help_message)
def read_excel_file(input_file):
print(f"Loading Excel file: {input_file}")
try:
df = pd.read_excel(input_file, keep_default_na=False, na_values=[''])
print(f"Excel file loaded successfully. Found {len(df)} rows and {len(df.columns)} columns.")
return df
except Exception as e:
print(f"Error loading Excel file: {e}")
sys.exit(1)
def pretty_print_xml(element):
xml_str = ET.tostring(element, 'utf-8')
dom = xml.dom.minidom.parseString(xml_str)
return dom.toprettyxml(indent=" ")
def process_strings(df, output_dir):
for column in df.columns:
if column not in ["Key", "Translatable"]:
print(f"Processing language: {column}")
root = ET.Element("resources")
for _, row in df.iterrows():
string_name = row["Key"]
string_value = row[column]
is_not_translatable = row["Translatable"] == False
if should_skip_row(column, string_value, is_not_translatable, df):
continue
if "_PLURALS_" in string_name:
handle_plurals(root, string_name, string_value)
else:
handle_regular_string(root, string_name, string_value, is_not_translatable)
write_xml_to_file(root, column, output_dir)
return len(df.columns)
def should_skip_row(column, string_value, is_not_translatable, df):
if is_not_translatable and column != df.columns[2]: # Default language check
return True
if pd.isna(string_value) or (string_value.startswith("Key:") and "_PLURALS_" in string_value):
return True
return False
def handle_plurals(root, string_name, string_value):
plural_base_name = string_name.split("_PLURALS_")[0]
plural_quantity = string_name.split("_PLURALS_")[1]
plural_group_elem = root.find(f".//plurals[@name='{plural_base_name}']")
if plural_group_elem is None:
plural_group_elem = ET.Element("plurals", name=plural_base_name)
root.append(plural_group_elem)
item_elem = ET.SubElement(plural_group_elem, "item", quantity=plural_quantity)
item_elem.text = string_value
def handle_regular_string(root, string_name, string_value, is_not_translatable):
string_attributes = {"name": string_name}
if is_not_translatable:
string_attributes["translatable"] = "false"
string_element = ET.Element("string", string_attributes)
string_element.text = string_value
root.append(string_element)
def write_xml_to_file(root, column, output_dir):
print(f"Writing strings.xml for language: {column}")
pretty_xml = pretty_print_xml(root)
directory = f"{output_dir}/{column}"
os.makedirs(directory, exist_ok=True)
with open(f"{directory}/strings.xml", "w", encoding="utf-8") as file:
file.write(pretty_xml)
def main():
if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
print_help()
sys.exit(0)
default_file = "translations.xlsx"
input_file = sys.argv[1] if len(sys.argv) > 1 else default_file
output_dir = sys.argv[2] if len(sys.argv) > 2 else "./app/src/main/res/"
print("Starting the string conversion process...")
df = read_excel_file(input_file)
amt_files = process_strings(df, output_dir)
print(f"Import completed. Generated {amt_files} strings.xml files. Check '{output_dir}' for the output.")
if __name__ == "__main__":
main()