-
Notifications
You must be signed in to change notification settings - Fork 0
/
loco_updater.py
157 lines (113 loc) · 4.46 KB
/
loco_updater.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import shutil
import subprocess
import xml.etree.ElementTree as ET
import zipfile
import requests
import config as config
from loco_validation_rules import ExistenceRule, FrenchEmailRule
cwd = "/tmp/ink_archive"
archive_name = "downloaded.zip"
value_folders = ['values', 'values-de', 'values-es', 'values-fr', 'values-it']
project_root = config.get('loco', 'project_root')
project_path = project_root + "/src/main/res"
forbidden_sequences = ["'", "..."]
forbidden_rules = [ExistenceRule(sequence) for sequence in forbidden_sequences]
rules = [*forbidden_rules]
language_rules = {
"en": [ExistenceRule("e-mail", "Remove the hyphen")],
"fr": [FrenchEmailRule()],
"de": [
ExistenceRule("ẞ"),
ExistenceRule("gespräch", "Use 'Unterhaltung' instead"),
],
"it": [
ExistenceRule("oscuro", "In the context of a dark and light theme, use 'scuro'"),
ExistenceRule("claro", "In the context of a dark and light theme, use 'chiaro'"),
ExistenceRule("luce", "In the context of a dark and light theme, use 'chiaro'"),
ExistenceRule("thema", "In the context of a dark and light theme, use 'tema'"),
],
"es": []
}
def update_loco():
loco_key = config.get('loco', 'loco_key')
zip_url = f"https://localise.biz/api/export/archive/xml.zip?format=android&filter=android&fallback=en&order=id&key={loco_key}"
archive_path = download_zip(zip_url)
if archive_path is None:
return
print("String resources downloaded successfully")
with zipfile.ZipFile(archive_path, 'r') as zip_ref:
zip_ref.extractall(cwd)
os.chdir(cwd)
files = os.listdir('.')
os.chdir(project_root)
# Copy the strings.xml files from the archive to the project's values folder
for value_folder in value_folders:
target_file = f'{project_path}/{value_folder}/strings.xml'
source_file = f'{cwd}/{files[0]}/res/{value_folder}/strings.xml'
shutil.copy(source_file, target_file)
fix_loco_header(target_file)
print("String resources updated")
shutil.rmtree(cwd)
print("Deleting temporary downloaded strings resources")
def download_zip(zip_url):
archive_path = cwd + "/" + archive_name
response = requests.get(zip_url)
if response.status_code != 200:
print("Error: When trying to download translations received response.status_code =", response.status_code)
return None
os.makedirs(cwd, exist_ok=True)
with open(archive_path, "wb+") as f:
f.write(response.content)
return archive_path
def fix_loco_header(target_file):
result = subprocess.run(f"git diff {target_file}", stdout=subprocess.PIPE, shell=True, universal_newlines=True)
diff = result.stdout
removed_lines = []
added_lines = []
for line in diff.split("\n")[5:]:
if line[0] == "-":
removed_lines.append(line[1:])
elif line[0] == "+":
added_lines.append(line[1:])
else:
break
to_replace = "\n".join(added_lines)
replace_with = "\n".join(removed_lines)
with open(target_file, "r+") as fd:
file_content = fd.read()
fixed_file = file_content.replace(to_replace, replace_with)
fd.seek(0)
fd.write(fixed_file)
def validate_strings():
error_count = 0
for value_folder in value_folders:
current_file = f'{project_path}/{value_folder}/strings.xml'
tree = ET.parse(current_file)
parts = value_folder.split("-")
language = "en" if len(parts) < 2 else parts[-1]
for element in tree.getroot():
tag = element.tag
name = element.get("name")
value = element.text
if tag == "string":
error_count += validate_string(language, name, value)
elif tag == "plurals":
error_count += validate_plural(element, language, name)
return error_count
def validate_string(language, name, value):
error_count = 0
for rule in rules:
if rule.check(value, language, name):
error_count += 1
for language_rule in language_rules[language]:
if language_rule.check(value, language, name):
error_count += 1
return error_count
def validate_plural(plural, language, name):
error_count = 0
for element in plural:
plural_name = f"{name}-{element.get('quantity')}"
plural_value = element.text
error_count += validate_string(language, plural_name, plural_value)
return error_count