Commit 07c3955e authored by Jakob Lerch's avatar Jakob Lerch
Browse files

integrate translation into __main__.py

parent 94dcfe37
......@@ -5,12 +5,10 @@ from pathlib import Path
import requests
import utils
from pad import Etherpad, HedgedocNote, Pad
from translation import DeepL, LibreTranslate, Translation
# TODO: scheint nur bei fem-instanz zu funktionieren. bei libretranslate.de kackt das ab
# TODO: auch kompatibel mit deepl machen
# TODO: besseren algorithmus für die übersetzung ausdenken (working sets)
if __name__ == "__main__":
......@@ -33,21 +31,50 @@ if __name__ == "__main__":
else:
raise ValueError("config.conf: 'kind' in [source] is not valid")
src_lang = src_conf["language"]
# create dst pad
# ..other than etherpad is not supported as destination yet
# TODO: support HedgeDoc
dst_conf = config["destination"]
dst_pad = Etherpad(dst_conf["url"], api_keys["default"]["etherpad_key"])
dst_lang = dst_conf["language"]
dst_pad: Pad
if dst_conf["kind"] == "hedgedoc":
dst_pad = HedgedocNote(dst_conf["url"])
elif dst_conf["kind"] == "etherpad":
dst_pad = Etherpad(
dst_conf["url"], api_keys["default"]["etherpad_key"])
else:
raise ValueError("config.conf: 'kind' in [destination] is not valid")
# create translation
# TODO: check if "language" is valid language code
src_lang = config["source"]["language"]
dst_lang = config["destination"]["language"]
t: Translation
# TODO: support DeepL
if config["translation"]["service"] == "libretranslate":
t = LibreTranslate(
config["translation"]["service_url"], api_keys["libretranslate_key"])
elif config["translation"]["service"] == "deepl":
t = DeepL(
config["translation"]["service_url"], api_keys["libretranslate_key"])
else:
raise ValueError(
"config.conf: 'service' in [translation] is not valid")
# do translation
# ..initial translation
dst_pad.write(utils.translate_src_to_dst(url=config["main"]["libretranslate_service_url"], src_string=src_pad.read(),
src_lang=src_lang, dst_lang=dst_lang, api_key=api_keys["default"]["libretranslate_key"]))
dst_pad.write(t.translate(src_pad.read(), src_lang, dst_lang))
# ..further translation
while True:
# TODO: think of algorithm / try naiv method, if this works, do it
dst_pad.write(utils.translate_src_to_dst(url=config["main"]["libretranslate_service_url"], src_string=src_pad.read(
), src_lang=src_lang, dst_lang=dst_lang, api_key=api_keys["default"]["libretranslate_key"]))
# TODO:
# check what changed in the source pad
# if change was just an appending
# ..translate appended part and append it to the destination pad
# else (if change was not an appending):
# ..in which line was last change?
# ..translate this line and some lines above and below, replace them in the destination pad
dst_pad.write(t.translate(
src_pad.read(), src_lang, dst_lang))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment