-
Notifications
You must be signed in to change notification settings - Fork 175
/
translate.py
174 lines (142 loc) · 6.03 KB
/
translate.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from typing import Callable
import json
from sys import argv
from glob import glob
language = argv[1].lower()
TRANSLATION_FOLDER = "translations/"
file = f"{TRANSLATION_FOLDER}{language}.json"
if file in glob(f"{TRANSLATION_FOLDER}*.json"):
with open(file, "r") as f:
translation = json.load(f)
else:
raise ValueError("Requested translation is not currently supported.\nThe program will now exit.")
if any(not word.isascii() for word in translation.values()):
print("unicode found")
font_edit = r""
else:
font_edit = ""
print(f"Translating pdf to {language}")
MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
WEEKDAYS_SHORT = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
WEEK = ["Week"]
NOTES = ["Notes"]
NOTE = ["Note"]
NOTES_INDEX = ["Notes Index"]
ALL_NOTES = ["All notes"]
SCHEDULE = ["Schedule"]
PRIORITIES = ["Top priorities"]
MORE = ["More"]
REFLECT = ["Reflect"]
PHRASES = ["Things I'm grateful for", "The best thing that happened today", "Daily log"]
def handle_all() -> None:
handle_annual()
handle_quarterly()
handle_monthly()
handle_weekly()
handle_daily()
handle_daily_reflect()
handle_daily_notes()
handle_notes_indexed()
def add_identifier(keys: list[str], func: Callable[[str], str] = lambda x: x, dictionary: dict[str, str] = translation) -> dict[str, str]:
return {func(key): func(dictionary.get(key)) for key in keys}
def handle_annual() -> None:
with open("out/annual.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "{" + x + "}}")
replace |= add_identifier(NOTES, lambda x: "{" + x + "}")
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/annual.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_quarterly() -> None:
with open("out/quarterly.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "{" + x + "}}")
replace |= add_identifier(NOTES, lambda x: "{" + x + "}")
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/quarterly.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_monthly() -> None:
with open("out/monthly.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "}{" + x + "}")
replace |= add_identifier(WEEKDAYS)
replace |= add_identifier(WEEK, lambda x: "[c]{" + x)
replace |= add_identifier(NOTES, lambda x: "{" + x + "}")
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/monthly.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_weekly() -> None:
with open("out/weekly.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "}{" + x + "}")
replace |= add_identifier(WEEK, lambda x: "}{" + x)
replace |= add_identifier(WEEKDAYS, lambda x: ", " + x + "}")
replace |= add_identifier(NOTES, lambda x: "{" + x)
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/weekly.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_daily() -> None:
with open("out/daily.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "}{" + x + "}")
replace |= add_identifier(WEEK, lambda x: "}{" + x)
replace |= add_identifier(WEEKDAYS, lambda x: "}{" + x + ",")
replace |= add_identifier(WEEKDAYS_SHORT, lambda x: "}{" + x + ",")
replace |= add_identifier(SCHEDULE, lambda x: "{" + x + "\\")
replace |= add_identifier(PRIORITIES, lambda x: "{" + x + "\\")
replace |= add_identifier(NOTES, lambda x: "{" + x + " $")
replace |= add_identifier(MORE, lambda x: "{" + x + "}")
replace |= add_identifier(REFLECT, lambda x: "{" + x + "}")
replace |= add_identifier(ALL_NOTES)
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/daily.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_daily_reflect() -> None:
with open("out/daily_reflect.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "}{" + x + "}")
replace |= add_identifier(WEEK, lambda x: "}{" + x)
replace |= add_identifier(WEEKDAYS, lambda x: "}{" + x + ",")
replace |= add_identifier(WEEKDAYS_SHORT, lambda x: "}{" + x + ",")
replace |= add_identifier(REFLECT, lambda x: "{" + x + "}")
replace |= add_identifier(PHRASES)
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/daily_reflect.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_daily_notes() -> None:
with open("out/daily_notes.tex", "r") as file:
text = file.read()
replace = add_identifier(MONTHS, lambda x: "}{" + x + "}")
replace |= add_identifier(WEEK, lambda x: "}{" + x)
replace |= add_identifier(WEEKDAYS, lambda x: "}{" + x + ",")
replace |= add_identifier(WEEKDAYS_SHORT, lambda x: "}{" + x + ",")
replace |= add_identifier(NOTES, lambda x: "{" + x + "}")
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/daily_notes.tex", "w") as file:
file.write(font_edit)
file.write(text)
def handle_notes_indexed() -> None:
with open("out/notes_indexed.tex", "r") as file:
text = file.read()
replace = add_identifier(NOTES_INDEX, lambda x: "}{" + x)
replace |= add_identifier(NOTE, lambda x: "}{" + x)
for english, spanish in replace.items():
text = text.replace(english, spanish)
with open("out/notes_indexed.tex", "w") as file:
file.write(font_edit)
file.write(text)
handle_all()