-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocale_sync.py
63 lines (47 loc) · 2.09 KB
/
locale_sync.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
# Simple script to sync en-GB.ftl to other locales for easier locale development
# Must be ran after updates to en-GB.ftl
import os
LOCALE_DIRECTORY = "./locales"
MAIN_LOCALE_FILE = "en-GB.ftl"
def parse_locale(raw_text):
messages = {}
current_heading = ""
for line in raw_text.splitlines():
if len(line) > 0:
if line[0] == "#": # Treat individual comments as headings
current_heading = line
else:
line = line.split("#")[0] # Ignore comments
key, value = line.split(" = ")
messages[key] = [value, current_heading]
return messages
with open(os.path.join(LOCALE_DIRECTORY, MAIN_LOCALE_FILE)) as f:
text = f.read()
refrence_messages = parse_locale(text)
changes_were_made = False # Keep track if changes were made
# Iterate through all locale files to sync them
for file in os.listdir(LOCALE_DIRECTORY):
if file != MAIN_LOCALE_FILE:
try:
with open(os.path.join(LOCALE_DIRECTORY, file)) as f:
text = f.read()
new_messages = parse_locale(text)
diff = set(refrence_messages) - set(new_messages) # Subtract the new messages from the refrence messages
if len(diff) != 0:
for value in diff:
header = refrence_messages[value][1]
header_pos = text.find(header)
if header_pos == -1:
header_pos = len(text)
else:
header_pos = header_pos + len(header)
new_value = f"\n{value} = {refrence_messages[value][0]} # TODO: Translate"
text = text[:header_pos] + new_value + text[header_pos:]
print(f"{file} +{len(diff)}")
with open(os.path.join(LOCALE_DIRECTORY, file), "w") as f:
changes_were_made = True
f.write(text)
except IsADirectoryError:
print(f"Ignoring '{file}' as it is a directory.")
if not changes_were_made:
print("No changes were made.")