From b26a3931a2800d3a0fafc736f827c7824d9fe236 Mon Sep 17 00:00:00 2001
From: Piero Toffanin <pt@masseranolabs.com>
Date: Thu, 5 Jan 2023 14:07:39 -0500
Subject: [PATCH] Translate swagger

---
 libretranslate/app.py                         |   4 +-
 libretranslate/locales.py                     |  18 +-
 .../locales/it/LC_MESSAGES/messages.po        | 160 +++++++++++++++++-
 update_locales.py                             |  23 ++-
 4 files changed, 195 insertions(+), 10 deletions(-)

diff --git a/libretranslate/app.py b/libretranslate/app.py
index ff33bd6..53e8d01 100644
--- a/libretranslate/app.py
+++ b/libretranslate/app.py
@@ -19,7 +19,7 @@ from flask_babel import Babel
 
 from libretranslate import flood, remove_translated_files, security
 from libretranslate.language import detect_languages, improve_translation_formatting
-from libretranslate.locales import _, _lazy, get_available_locales, gettext_escaped, gettext_html
+from libretranslate.locales import _, _lazy, get_available_locales, gettext_escaped, gettext_html, lazy_swag
 
 from .api_keys import Database, RemoteDatabase
 from .suggestions import Database as SuggestionsDatabase
@@ -1003,7 +1003,7 @@ def create_app(args):
     @app.route(API_URL)
     @limiter.exempt
     def spec():
-        return jsonify(swag)
+        return jsonify(lazy_swag(swag))
 
 
     app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales'
diff --git a/libretranslate/locales.py b/libretranslate/locales.py
index c93adfa..39ae708 100644
--- a/libretranslate/locales.py
+++ b/libretranslate/locales.py
@@ -31,4 +31,20 @@ def gettext_html(text, **variables):
                 v[k] = Markup(variables[k])
 
     # Variables are assumed to be already escaped and thus safe
-    return Markup(s if not v else s % v)
\ No newline at end of file
+    return Markup(s if not v else s % v)
+
+def swag_eval(swag, func):
+    # Traverse the swag spec structure
+    # and call func on summary and description keys
+    for k in swag:
+        if k in ['summary', 'description'] and isinstance(swag[k], str) and swag[k] != "":
+            swag[k] = func(swag[k])
+        elif k == 'tags' and isinstance(swag[k], list):
+            swag[k] = [func(v) for v in swag[k]]
+        elif isinstance(swag[k], dict):
+            swag_eval(swag[k], func)
+
+    return swag
+
+def lazy_swag(swag):
+    return swag_eval(swag, _lazy)
\ No newline at end of file
diff --git a/libretranslate/locales/it/LC_MESSAGES/messages.po b/libretranslate/locales/it/LC_MESSAGES/messages.po
index 85fdbbb..e95d548 100644
--- a/libretranslate/locales/it/LC_MESSAGES/messages.po
+++ b/libretranslate/locales/it/LC_MESSAGES/messages.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: LibreTranslate 1.3.8\n"
 "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
-"POT-Creation-Date: 2023-01-05 13:11-0500\n"
+"POT-Creation-Date: 2023-01-05 14:02-0500\n"
 "PO-Revision-Date: 2023-01-05 13:11-0500\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: it <LL@li.org>\n"
@@ -235,6 +235,160 @@ msgstr "Ucraina"
 msgid "Vietnamese"
 msgstr "Vietnamita"
 
+#: libretranslate/locales/.swag.py:1
+msgid "Retrieve list of supported languages"
+msgstr "Recuperare l'elenco delle lingue supportate"
+
+#: libretranslate/locales/.swag.py:2
+msgid "List of languages"
+msgstr "Elenco delle lingue"
+
+#: libretranslate/locales/.swag.py:3
+#, fuzzy
+msgid "translate"
+msgstr "Traduttore"
+
+#: libretranslate/locales/.swag.py:4
+msgid "Translate text from a language to another"
+msgstr "Tradurre testo da una lingua a un'altra"
+
+#: libretranslate/locales/.swag.py:5 libretranslate/templates/index.html:183
+msgid "Translated text"
+msgstr "Tradotto testo"
+
+#: libretranslate/locales/.swag.py:6
+#, fuzzy
+msgid "Invalid request"
+msgstr "Richiesta non valida: file vuoto"
+
+#: libretranslate/locales/.swag.py:7
+#, fuzzy
+msgid "Translation error"
+msgstr "traduzione"
+
+#: libretranslate/locales/.swag.py:8
+#, fuzzy
+msgid "Slow down"
+msgstr "Rallenta:"
+
+#: libretranslate/locales/.swag.py:9
+msgid "Banned"
+msgstr "Banati"
+
+#: libretranslate/locales/.swag.py:10
+msgid "Translate file from a language to another"
+msgstr "Tradurre file da una lingua a un'altra"
+
+#: libretranslate/locales/.swag.py:11
+#, fuzzy
+msgid "Translated file"
+msgstr "Traduci file"
+
+#: libretranslate/locales/.swag.py:12
+msgid "Detect the language of a single text"
+msgstr "Rileva la lingua di un singolo testo"
+
+#: libretranslate/locales/.swag.py:13
+msgid "Detections"
+msgstr "Rilevazioni"
+
+#: libretranslate/locales/.swag.py:14
+msgid "Detection error"
+msgstr "Errore di rilevamento"
+
+#: libretranslate/locales/.swag.py:15
+msgid "Retrieve frontend specific settings"
+msgstr "Recuperare le impostazioni specifiche di frontend"
+
+#: libretranslate/locales/.swag.py:16
+msgid "frontend settings"
+msgstr "impostazioni di frontend"
+
+#: libretranslate/locales/.swag.py:17
+#, fuzzy
+msgid "frontend"
+msgstr "impostazioni di frontend"
+
+#: libretranslate/locales/.swag.py:18
+msgid "Submit a suggestion to improve a translation"
+msgstr "Inviare un suggerimento per migliorare una traduzione"
+
+#: libretranslate/locales/.swag.py:19
+msgid "Success"
+msgstr "Successo"
+
+#: libretranslate/locales/.swag.py:20
+#, fuzzy
+msgid "Not authorized"
+msgstr "Non autorizzato"
+
+#: libretranslate/locales/.swag.py:21
+msgid "feedback"
+msgstr "feedback"
+
+#: libretranslate/locales/.swag.py:22
+msgid "Language code"
+msgstr "Codice linguistico"
+
+#: libretranslate/locales/.swag.py:23
+msgid "Human-readable language name (in English)"
+msgstr "Nome di lingua leggibile dall'uomo (in inglese)"
+
+#: libretranslate/locales/.swag.py:24
+msgid "Supported target language codes"
+msgstr "Codici di lingua target supportati"
+
+#: libretranslate/locales/.swag.py:25
+#, fuzzy
+msgid "Translated text(s)"
+msgstr "Tradotto testo"
+
+#: libretranslate/locales/.swag.py:26
+msgid "Error message"
+msgstr "Messaggio di errore"
+
+#: libretranslate/locales/.swag.py:27
+msgid "Reason for slow down"
+msgstr "Ragione per rallentare"
+
+#: libretranslate/locales/.swag.py:28
+#, fuzzy
+msgid "Translated file url"
+msgstr "Traduci file"
+
+#: libretranslate/locales/.swag.py:29
+msgid "Confidence value"
+msgstr "Valore di fiducia"
+
+#: libretranslate/locales/.swag.py:30
+msgid "Character input limit for this language (-1 indicates no limit)"
+msgstr "Limite di ingresso per questa lingua (-1 non indica limiti)"
+
+#: libretranslate/locales/.swag.py:31
+msgid "Frontend translation timeout"
+msgstr "Tempo di traduzione Frontend"
+
+#: libretranslate/locales/.swag.py:32
+msgid "Whether the API key database is enabled."
+msgstr "Se il database chiave API è abilitato."
+
+#: libretranslate/locales/.swag.py:33
+msgid "Whether an API key is required."
+msgstr "Se è richiesta una chiave API."
+
+#: libretranslate/locales/.swag.py:34
+msgid "Whether submitting suggestions is enabled."
+msgstr "Se presentare suggerimenti è abilitato."
+
+#: libretranslate/locales/.swag.py:35
+#, fuzzy
+msgid "Supported files format"
+msgstr "Formati di file supportati:"
+
+#: libretranslate/locales/.swag.py:36
+msgid "Whether submission was successful"
+msgstr "Se la presentazione è stata di successo"
+
 #: libretranslate/templates/app.js.template:31
 #: libretranslate/templates/app.js.template:279
 msgid "Copy text"
@@ -360,10 +514,6 @@ msgstr "Traduzione"
 msgid "Text to translate"
 msgstr "Testo da tradurre"
 
-#: libretranslate/templates/index.html:183
-msgid "Translated text"
-msgstr "Tradotto testo"
-
 #: libretranslate/templates/index.html:191
 msgid "Cancel"
 msgstr "Annulla"
diff --git a/update_locales.py b/update_locales.py
index f22cc4f..864e411 100755
--- a/update_locales.py
+++ b/update_locales.py
@@ -6,9 +6,11 @@ import polib
 import json
 from babel.messages.frontend import main as pybabel
 from libretranslate.language import load_languages, improve_translation_formatting
-from libretranslate.locales import get_available_locales
+from libretranslate.locales import get_available_locales, swag_eval
 from translatehtml import translate_html
-from libretranslate.app import get_version
+from libretranslate.app import get_version, create_app
+from libretranslate.main import get_args
+from flask_swagger import swagger
 
 # Update strings
 if __name__ == "__main__":
@@ -30,6 +32,23 @@ if __name__ == "__main__":
             f.write("_(%s)\n" % json.dumps(l.name))
     print("Wrote %s" % langs_file)
 
+    # Dump swagger strings
+    args = get_args()
+    app = create_app(args)
+    swag = swagger(app)
+
+    swag_strings = []
+    def add_swag_string(s):
+        if not s in swag_strings:
+            swag_strings.append(s)
+    swag_eval(swag, add_swag_string)
+
+    swag_file = os.path.join(locales_dir, ".swag.py")
+    with open(swag_file, 'w') as f:
+        for ss in swag_strings:
+            f.write("_(%s)\n" % json.dumps(ss))
+    print("Wrote %s" % swag_file)
+
     messagespot = os.path.join(locales_dir, "messages.pot")
     print("Updating %s" % messagespot)
     sys.argv = ["", "extract", "-F", "babel.cfg", "-k", "_e _h",