Skip to content

Commit

Permalink
Add basic translation script
Browse files Browse the repository at this point in the history
Merge pull request #106 from Batres3/main
  • Loading branch information
kudrykv committed Jan 16, 2024
2 parents a321d94 + 503ff79 commit 9ad25ff
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 1 deletion.
6 changes: 5 additions & 1 deletion single.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fi

nakedname=$(echo "${CFG}" | rev | cut -d, -f1 | cut -d'/' -f 1 | cut -d'.' -f 2-99 | rev)

if [ -n "${TRANSLATION}" ]; then
python3 translate.py ${TRANSLATION}
fi

_passes=(1)

if [[ -n "${PASSES}" ]]; then
Expand All @@ -27,7 +31,7 @@ if [[ -n "${PASSES}" ]]; then
fi

for _ in "${_passes[@]}"; do
pdflatex \
xelatex \
-file-line-error \
-interaction=nonstopmode \
-synctex=1 \
Expand Down
174 changes: 174 additions & 0 deletions translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
40 changes: 40 additions & 0 deletions translations/german.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"January": "Januar",
"February": "Februar",
"March": "M盲rz",
"April": "April",
"May": "Mai",
"June": "Juni",
"July": "Juli",
"August": "August",
"September": "September",
"October": "Oktober",
"November": "November",
"December": "Dezember",
"Monday": "Montag",
"Tuesday": "Dienstag",
"Wednesday": "Mittwoch",
"Thursday": "Donnerstag",
"Friday": "Freitag",
"Saturday": "Samstag",
"Sunday": "Sonntag",
"Mon": "Mo",
"Tue": "Di",
"Wed": "Mi",
"Thu": "Do",
"Fri": "Fr",
"Sat": "Sa",
"Sun": "So",
"Week": "Woche",
"Notes": "Notizen",
"Note": "Notiz",
"Notes Index": "Notizverzeichnis",
"All notes": "Alle Notizen",
"Schedule": "Zeitplan",
"Top priorities": "h枚chste Priorit盲t",
"More": "mehr",
"Reflect": "Reflektieren",
"Things I'm grateful for": "Dinge, f眉r die ich dankbar bin",
"The best thing that happened today": "Das Beste, was heute passiert ist",
"Daily log": "Tagebuch"
}
40 changes: 40 additions & 0 deletions translations/spanish.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"January": "Enero",
"February": "Febrero",
"March": "Marzo",
"April": "Abril",
"May": "Mayo",
"June": "Junio",
"July": "Julio",
"August": "Agosto",
"September": "Septiembre",
"October": "Octubre",
"November": "Noviembre",
"December": "Diciembre",
"Monday": "Lunes",
"Tuesday": "Martes",
"Wednesday": "Mi茅rcoles",
"Thursday": "Jueves",
"Friday": "Viernes",
"Saturday": "S谩bado",
"Sunday": "Domingo",
"Mon": "Lun",
"Tue": "Mar",
"Wed": "Mi茅r",
"Thu": "Jue",
"Fri": "Vie",
"Sat": "Sab",
"Sun": "Dom",
"Week": "Semana",
"Notes": "Notas",
"Note": "Nota",
"Notes Index": "脥ndice de notas",
"All notes": "Todas las notas",
"Schedule": "Horario",
"Top priorities": "Prioridades",
"More": "M谩s",
"Reflect": "Reflexi贸n",
"Things I'm grateful for": "Cosas por las que estoy agradecido",
"The best thing that happened today": "Lo mejor que ha pasado hoy",
"Daily log": "Registro diario"
}

0 comments on commit 9ad25ff

Please sign in to comment.