From 19c3c04ca64373671eacfb8ab833421c3c3569bb Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 4 Jan 2023 17:54:07 -0500 Subject: [PATCH] Translated index.html --- libretranslate/app.py | 19 +- libretranslate/locales.py | 24 ++- .../locales/it/LC_MESSAGES/messages.po | 30 +++- libretranslate/templates/app.js.template | 4 +- libretranslate/templates/index.html | 167 +++++++++--------- .../templates/javascript-licenses.html | 22 --- update_locales.py | 7 +- 7 files changed, 144 insertions(+), 129 deletions(-) delete mode 100644 libretranslate/templates/javascript-licenses.html diff --git a/libretranslate/app.py b/libretranslate/app.py index 964a15d..65c70a3 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -19,7 +19,7 @@ from flask_babel import Babel, gettext as _ from libretranslate import flood, remove_translated_files, security from libretranslate.language import detect_languages, improve_translation_formatting -from libretranslate.locales import get_available_locales +from libretranslate.locales import get_available_locales, gettext_escaped, gettext_html from .api_keys import Database, RemoteDatabase from .suggestions import Database as SuggestionsDatabase @@ -288,14 +288,6 @@ def create_app(args): url_prefix=args.url_prefix ) - @bp.get("/javascript-licenses") - @limiter.exempt - def javascript_licenses(): - if args.disable_web_ui: - abort(404) - - return render_template("javascript-licenses.html") - @bp.route("/static/js/app.js") @limiter.exempt def appjs(): @@ -1017,15 +1009,16 @@ def create_app(args): def spec(): return jsonify(swag) + + app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales' babel = Babel(app) @babel.localeselector def get_locale(): - # TODO: populate from available locales return request.accept_languages.best_match(get_available_locales()) - def gettext_escaped(*args, **kwargs): - return _(*args, **kwargs).replace("'", "\\'") - app.jinja_env.globals.update(N_=gettext_escaped) + + + app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html) # Call factory function to create our blueprint swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL) diff --git a/libretranslate/locales.py b/libretranslate/locales.py index b3deb4c..759ef4b 100644 --- a/libretranslate/locales.py +++ b/libretranslate/locales.py @@ -1,9 +1,31 @@ import os from functools import cache +from flask_babel import gettext as _ +from markupsafe import escape, Markup @cache def get_available_locales(): locales_dir = os.path.join(os.path.dirname(__file__), 'locales') dirs = [os.path.join(locales_dir, d) for d in os.listdir(locales_dir)] - return ['en'] + [os.path.basename(d) for d in dirs if os.path.isdir(os.path.join(d, 'LC_MESSAGES'))] \ No newline at end of file + return ['en'] + [os.path.basename(d) for d in dirs if os.path.isdir(os.path.join(d, 'LC_MESSAGES'))] + +# Javascript code should use _e instead of _ +def gettext_escaped(text, **variables): + return _(text, **variables).replace("'", "\\'") + +# HTML should be escaped using _h instead of _ +def gettext_html(text, **variables): + # Translate text without args + s = str(escape(_(text))) + + v = {} + if variables: + for k in variables: + if hasattr(variables[k], 'unescape'): + v[k] = variables[k].unescape() + else: + 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 diff --git a/libretranslate/locales/it/LC_MESSAGES/messages.po b/libretranslate/locales/it/LC_MESSAGES/messages.po index 092dcc5..eeb23b3 100644 --- a/libretranslate/locales/it/LC_MESSAGES/messages.po +++ b/libretranslate/locales/it/LC_MESSAGES/messages.po @@ -7,15 +7,15 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2023-01-04 15:34-0500\n" +"POT-Creation-Date: 2023-01-04 16:34-0500\n" "PO-Revision-Date: 2023-01-04 12:27-0500\n" "Last-Translator: FULL NAME \n" -"Language-Team: it \n" "Language: it\n" +"Language-Team: it \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Generated-By: Babel 2.11.0\n" #: libretranslate/app.py:58 @@ -35,6 +35,24 @@ msgstr "Copia testo" msgid "Cannot load %(url)s" msgstr "Non riesco a caricare %(url)s" -#: libretranslate/templates/index.html:6 -msgid "Free and Open Source Machine Translation API" -msgstr "API di traduzione automatica open source" +#: libretranslate/templates/index.html:6 libretranslate/templates/index.html:26 +msgid "LibreTranslate - Free and Open Source Machine Translation API" +msgstr "API di traduzione automatica open source < ' a" + +#: libretranslate/templates/index.html:8 libretranslate/templates/index.html:30 +msgid "" +"Free and Open Source Machine Translation API. Self-hosted, offline " +"capable and easy to setup. Run your own API server in just a few minutes." +msgstr "" +"API di traduzione automatica gratuita e open source. Auto-hosted, offline" +" in grado e facile da configurare. Eseguire il proprio server API in " +"pochi minuti." + +#: libretranslate/templates/index.html:9 +msgid "translation" +msgstr "traduzione" + +#: libretranslate/templates/index.html:9 +msgid "api" +msgstr "api" + diff --git a/libretranslate/templates/app.js.template b/libretranslate/templates/app.js.template index 1aae136..57dde04 100644 --- a/libretranslate/templates/app.js.template +++ b/libretranslate/templates/app.js.template @@ -28,7 +28,7 @@ document.addEventListener('DOMContentLoaded', function(){ detectedLangText: "", - copyTextLabel: '{{ N_("Copy text") }}', + copyTextLabel: '{{ _e("Copy text") }}', suggestions: false, isSuggesting: false, @@ -69,7 +69,7 @@ document.addEventListener('DOMContentLoaded', function(){ } } } else { - self.error = '{{ N_("Cannot load %(url)s", url="/frontend/settings") }}'; + self.error = '{{ _e("Cannot load %(url)s", url="/frontend/settings") }}'; self.loading = false; } }; diff --git a/libretranslate/templates/index.html b/libretranslate/templates/index.html index cc596cb..be8e993 100644 --- a/libretranslate/templates/index.html +++ b/libretranslate/templates/index.html @@ -3,10 +3,10 @@ - LibreTranslate - {{ _("Free and Open Source Machine Translation API") }} + {{ _h("LibreTranslate - Free and Open Source Machine Translation API") }} - - + + @@ -23,11 +23,11 @@ - + - + @@ -58,28 +58,28 @@ @@ -116,7 +116,7 @@ warning

[[ error ]]

@@ -130,22 +130,22 @@
-

Translation API

+

{{ _h("Translation API") }}

- Translate from - [[ detectedLangText ]] + {{ _h("Translate from") }} + [[ detectedLangText ]]