1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-17 13:00:11 +01:00

Translate swagger

This commit is contained in:
Piero Toffanin 2023-01-05 14:07:39 -05:00
parent edd3d6931d
commit b26a3931a2
4 changed files with 195 additions and 10 deletions

View File

@ -19,7 +19,7 @@ from flask_babel import Babel
from libretranslate import flood, remove_translated_files, security from libretranslate import flood, remove_translated_files, security
from libretranslate.language import detect_languages, improve_translation_formatting 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 .api_keys import Database, RemoteDatabase
from .suggestions import Database as SuggestionsDatabase from .suggestions import Database as SuggestionsDatabase
@ -1003,7 +1003,7 @@ def create_app(args):
@app.route(API_URL) @app.route(API_URL)
@limiter.exempt @limiter.exempt
def spec(): def spec():
return jsonify(swag) return jsonify(lazy_swag(swag))
app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales' app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales'

View File

@ -32,3 +32,19 @@ def gettext_html(text, **variables):
# Variables are assumed to be already escaped and thus safe # Variables are assumed to be already escaped and thus safe
return Markup(s if not v else s % v) 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)

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: LibreTranslate 1.3.8\n" "Project-Id-Version: LibreTranslate 1.3.8\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\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" "PO-Revision-Date: 2023-01-05 13:11-0500\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: it <LL@li.org>\n" "Language-Team: it <LL@li.org>\n"
@ -235,6 +235,160 @@ msgstr "Ucraina"
msgid "Vietnamese" msgid "Vietnamese"
msgstr "Vietnamita" 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:31
#: libretranslate/templates/app.js.template:279 #: libretranslate/templates/app.js.template:279
msgid "Copy text" msgid "Copy text"
@ -360,10 +514,6 @@ msgstr "Traduzione"
msgid "Text to translate" msgid "Text to translate"
msgstr "Testo da tradurre" msgstr "Testo da tradurre"
#: libretranslate/templates/index.html:183
msgid "Translated text"
msgstr "Tradotto testo"
#: libretranslate/templates/index.html:191 #: libretranslate/templates/index.html:191
msgid "Cancel" msgid "Cancel"
msgstr "Annulla" msgstr "Annulla"

View File

@ -6,9 +6,11 @@ import polib
import json import json
from babel.messages.frontend import main as pybabel from babel.messages.frontend import main as pybabel
from libretranslate.language import load_languages, improve_translation_formatting 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 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 # Update strings
if __name__ == "__main__": if __name__ == "__main__":
@ -30,6 +32,23 @@ if __name__ == "__main__":
f.write("_(%s)\n" % json.dumps(l.name)) f.write("_(%s)\n" % json.dumps(l.name))
print("Wrote %s" % langs_file) 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") messagespot = os.path.join(locales_dir, "messages.pot")
print("Updating %s" % messagespot) print("Updating %s" % messagespot)
sys.argv = ["", "extract", "-F", "babel.cfg", "-k", "_e _h", sys.argv = ["", "extract", "-F", "babel.cfg", "-k", "_e _h",