forked from element-hq/element-x-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateLocalazyConfig.py
executable file
·105 lines (92 loc) · 3.63 KB
/
generateLocalazyConfig.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
#!/usr/bin/env python3
# Copyright 2024 New Vector Ltd.
#
# SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
# Please see LICENSE files in the repository root for full details.
import json
import sys
# Read the config.json file
with open('./tools/localazy/config.json', 'r') as f:
config = json.load(f)
allFiles = sys.argv[1] == "1"
# Convert a module name to a path
# Ex: ":features:verifysession:impl" => "features/verifysession/impl"
def convertModuleToPath(name):
return name[1:].replace(":", "/")
# Regex that will be excluded from the Android project, you may add items here if necessary.
regexToAlwaysExclude = [
"Notification",
".*_ios"
]
baseAction = {
"type": "android",
# Replacement done in all string values
"replacements": {
"...": "…"
},
"params": {
"force_underscore": "yes"
}
}
# Store all regex specific to module, to exclude the corresponding key from the common string module
allRegexToExcludeFromMainModule = []
# All actions that will be serialized in the localazy config
allActions = []
# Iterating on the config
for entry in config["modules"]:
# Create action for the default language
excludeRegex = regexToAlwaysExclude
if "excludeRegex" in entry:
excludeRegex += entry["excludeRegex"]
action = baseAction | {
"output": convertModuleToPath(entry["name"]) + "/src/main/res/values/localazy.xml",
"includeKeys": list(map(lambda i: "REGEX:" + i, entry["includeRegex"])),
"excludeKeys": list(map(lambda i: "REGEX:" + i, excludeRegex)),
"conditions": [
"equals: ${langAndroidResNoScript}, en | equals: ${file}, content.json"
]
}
# print(action)
allActions.append(action)
# Create action for the translations
if allFiles:
actionTranslation = baseAction | {
"output": convertModuleToPath(entry["name"]) + "/src/main/res/values-${langAndroidResNoScript}/translations.xml",
"includeKeys": list(map(lambda i: "REGEX:" + i, entry["includeRegex"])),
"excludeKeys": list(map(lambda i: "REGEX:" + i, excludeRegex)),
"conditions": [
"!equals: ${langAndroidResNoScript}, en | equals: ${file}, content.json"
]
}
allActions.append(actionTranslation)
allRegexToExcludeFromMainModule.extend(entry["includeRegex"])
# Append configuration for the main string module: default language
mainAction = baseAction | {
"output": "libraries/ui-strings/src/main/res/values/localazy.xml",
"excludeKeys": list(map(lambda i: "REGEX:" + i, allRegexToExcludeFromMainModule + regexToAlwaysExclude)),
"conditions": [
"equals: ${langAndroidResNoScript}, en | equals: ${file}, content.json"
]
}
# print(mainAction)
allActions.append(mainAction)
if allFiles:
# Append configuration for the main string module: translations
mainActionTranslation = baseAction | {
"output": "libraries/ui-strings/src/main/res/values-${langAndroidResNoScript}/translations.xml",
"excludeKeys": list(map(lambda i: "REGEX:" + i, allRegexToExcludeFromMainModule + regexToAlwaysExclude)),
"conditions": [
"!equals: ${langAndroidResNoScript}, en | equals: ${file}, content.json"
]
}
allActions.append(mainActionTranslation)
# Generate the configuration for localazy
result = {
"readKey": "a7876306080832595063-aa37154bb3772f6146890fca868d155b2228b492c56c91f67abdcdfb74d6142d",
"conversion": {
"actions": allActions
}
}
# Json serialization
with open('./tools/localazy/localazy.json', 'w') as json_file:
json.dump(result, json_file, indent=4, sort_keys=True)