diff --git a/telegram_epson_printer_bot/__main__.py b/telegram_epson_printer_bot/__main__.py index 1302aee..f0ef5b8 100644 --- a/telegram_epson_printer_bot/__main__.py +++ b/telegram_epson_printer_bot/__main__.py @@ -1,5 +1,6 @@ from tempfile import NamedTemporaryFile from os import environ, path +import json import logging from telegram import Update @@ -15,6 +16,20 @@ import epson_connect as ec KEY_FILE = "./keys.txt" +LOCALE_DIR = path.join(path.dirname(__file__), "lang") +LANGUAGE = environ.get("TELEGRAM_BOT_LANGUAGE", "en") + +with open(path.join(LOCALE_DIR, "en.json"), "r") as translations_file: + try: + translations: dict = json.load(translations_file) + except FileNotFoundError: + logging.warning(f"Language {LANGUAGE} not found!") + exit(1) + + +def _(key, *args): + translations.get(key, key).format(*args) + TELEGRAM_ALLOWED_CHAT_IDS = [ int(chat_id.strip()) for chat_id in environ.get("TELEGRAM_ALLOWED_CHAT_IDS", "").split(",") if chat_id.strip() @@ -30,7 +45,7 @@ async def incoming_pdf_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if (chat_id := update.message.chat_id) not in TELEGRAM_ALLOWED_CHAT_IDS: logging.warning(f"Chat {str(chat_id)} not allowed") - await update.message.reply_text(f"🚫 Questo bot non è abilitato per questa chat.\nChat ID: {str(chat_id)}") + await update.message.reply_text("🚫 " + _("chat_not_allowed", str(chat_id))) return keys_to_try = [] if path.isfile(KEY_FILE): @@ -41,7 +56,7 @@ async def incoming_pdf_handler(update: Update, context: ContextTypes.DEFAULT_TYP with NamedTemporaryFile(suffix=".pdf") as temp_file: logging.info("Downloading PDF...") - await update.message.reply_text("📃 Elaboro il tuo PDF...") + await update.message.reply_text("📃 " + _("processing_pdf")) telegram_file = await update.message.document.get_file() await telegram_file.download_to_drive(temp_file.name) logging.info("PDF downloaded") @@ -53,13 +68,11 @@ async def incoming_pdf_handler(update: Update, context: ContextTypes.DEFAULT_TYP for key in keys_to_try: if pdf_reader.decrypt(key) is not PasswordType.NOT_DECRYPTED: logging.info(f"PDF decrypted with key {key}") - await update.message.reply_text("🎉 PDF decifrato con successo!") + await update.message.reply_text("🎉 " + _("pdf_decrypted")) break else: logging.warning("No key worked") - await update.message.reply_text( - "🔐 Nessuna chiave fornita ha funzionato. Scrivi la chiave del PDF nella didascalia del messaggio." - ) + await update.message.reply_text("🔐 " + _("no_key_worked")) return pdf_writer = PdfWriter() @@ -67,7 +80,7 @@ async def incoming_pdf_handler(update: Update, context: ContextTypes.DEFAULT_TYP pdf_writer.write(temp_file.name) job_id = epson_client.printer.print(temp_file.name) - await update.message.reply_text(f"🖨️ Il PDF è ora in stampa! (ID del job: {job_id})") + await update.message.reply_text("🖨️ " + _("pdf_printing", job_id)) app: Application = ApplicationBuilder().token(environ.get("TELEGRAM_BOT_TOKEN")).build() diff --git a/telegram_epson_printer_bot/lang/de.json b/telegram_epson_printer_bot/lang/de.json new file mode 100644 index 0000000..9910b0c --- /dev/null +++ b/telegram_epson_printer_bot/lang/de.json @@ -0,0 +1,7 @@ +{ + "chat_not_allowed": "Dieser Bot ist nicht für diesen Chat aktiviert.\nChat-ID: {}", + "processing_pdf": "Verarbeite PDF...", + "pdf_decrypted": "PDF erfolgreich entschlüsselt!", + "no_key_worked": "Kein bereitgestellter Schlüssel hat funktioniert. Bitte schreiben Sie den PDF-Schlüssel in die Nachrichtenbeschriftung.", + "pdf_printing": "Das PDF wird jetzt gedruckt! (Job-ID: {})" +} \ No newline at end of file diff --git a/telegram_epson_printer_bot/lang/en.json b/telegram_epson_printer_bot/lang/en.json new file mode 100644 index 0000000..f9c0b73 --- /dev/null +++ b/telegram_epson_printer_bot/lang/en.json @@ -0,0 +1,7 @@ +{ + "chat_not_allowed": "This bot is not enabled for this chat.\nChat ID: {}", + "processing_pdf": "Processing PDF...", + "pdf_decrypted": "PDF successfully decrypted!", + "no_key_worked": "No provided key worked. Please write the PDF key in the message caption.", + "pdf_printing": "The PDF is now printing! (Job ID: {})" +} \ No newline at end of file diff --git a/telegram_epson_printer_bot/lang/it.json b/telegram_epson_printer_bot/lang/it.json new file mode 100644 index 0000000..3cd16ee --- /dev/null +++ b/telegram_epson_printer_bot/lang/it.json @@ -0,0 +1,7 @@ +{ + "chat_not_allowed": "Questo bot non è abilitato per questa chat.\nChat ID: {}", + "processing_pdf": "Elaboro il tuo PDF...", + "pdf_decrypted": "PDF decifrato con successo!", + "no_key_worked": "Nessuna chiave fornita ha funzionato. Scrivi la chiave del PDF nella didascalia del messaggio.", + "pdf_printing": "Il PDF è ora in stampa! (ID del job: {})" +} \ No newline at end of file