forked from ecosia/ios-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecosify-strings.py
72 lines (55 loc) · 2.13 KB
/
ecosify-strings.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
#!/usr/bin/env python3
import os
import glob
import re
def _valid_directory(arg, parser):
if os.path.isdir(arg):
return arg
parser.error("Directory does not exist: {}".format(arg))
def ecosify_dir(dir_path):
for filename in glob.glob(dir_path + '/**/*.strings', recursive=True):
print(filename)
if filename.endswith(".strings") and not filename.__contains__("Ecosia"):
ecosify_translations(filename)
continue
else:
print("File extension not valid {}".format(filename))
continue
def ecosify_translations(file_path):
print("Replacing strings in strings file {}".format(file_path))
with open (file_path, 'r' ) as f:
try:
lines = f.readlines()
except Exception:
print('cannot open:' + file_path)
return
newlines = []
brandnames = ['firefoksa', 'firefoxen', 'firefoxu', 'firefoxe', 'firefoxban', 'firefoksie', 'firefox', 'mozilla']
for line in lines:
parts = line.split('=')
if len(parts) > 1:
value = parts[1]
for name in brandnames:
value = re.sub(name, 'Ecosia', value, flags=re.IGNORECASE)
newlines.append(parts[0] + '=' + value)
elif line.strip().endswith(';'):
# some translation values break lines and are ophaned -> we need to replace values there too
for name in brandnames:
line = re.sub(name, 'Ecosia', line, flags=re.IGNORECASE)
newlines.append(line)
else:
newlines.append(line)
# writing to file
with open(file_path, 'w') as f:
f.writelines(newlines)
f.close()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Replace Firefox and Mozilla strings with Ecosia",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('android_source', type=lambda arg: _valid_directory(arg, parser),
help="The android project's src folder")
args = parser.parse_args()
ecosify_dir(args.android_source)