From 58350d868c186b371783ca883c7e7d2d9326517f Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 6 Jan 2023 11:50:51 -0500 Subject: [PATCH] Language selector working --- libretranslate/app.py | 24 ++++++++++++++++++------ libretranslate/templates/app.js.template | 2 +- libretranslate/templates/index.html | 21 +++++++++++++++------ requirements.txt | 1 + 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index 24ec170..e877579 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -9,9 +9,10 @@ from timeit import default_timer import argostranslatefiles from argostranslatefiles import get_supported_formats from flask import (abort, Blueprint, Flask, jsonify, render_template, request, - Response, send_file, url_for) + Response, send_file, url_for, session) from flask_swagger import swagger from flask_swagger_ui import get_swaggerui_blueprint +from flask_session import Session from translatehtml import translate_html from werkzeug.utils import secure_filename from werkzeug.exceptions import HTTPException @@ -277,6 +278,10 @@ def create_app(args): if args.disable_web_ui: abort(404) + langcode = request.args.get('lang') + if langcode and langcode in get_available_locale_codes(not args.debug): + session.update(preferred_lang=langcode) + return render_template( "index.html", gaId=args.ga_id, @@ -286,19 +291,19 @@ def create_app(args): web_version=os.environ.get("LT_WEB") is not None, version=get_version(), swagger_url=SWAGGER_URL, - url_prefix=args.url_prefix, - available_locales=[{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales()], + available_locales=[{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales(not args.debug)], current_locale=get_locale(), alternate_locales=get_alternate_locale_links() ) - @bp.route("/static/js/app.js") + @bp.route("/js/app.js") @limiter.exempt def appjs(): if args.disable_web_ui: abort(404) return render_template("app.js.template", + url_prefix=args.url_prefix, get_api_key_link=args.get_api_key_link) @bp.get("/languages") @@ -991,6 +996,11 @@ def create_app(args): return jsonify({"success": True}) app = Flask(__name__) + + app.config["SESSION_TYPE"] = "filesystem" + app.config["SESSION_FILE_DIR"] = os.path.join("db", "sessions") + Session(app) + if args.debug: app.config["TEMPLATES_AUTO_RELOAD"] = True if args.url_prefix: @@ -1009,12 +1019,14 @@ def create_app(args): def spec(): return jsonify(lazy_swag(swag)) - app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales' babel = Babel(app) @babel.localeselector def get_locale(): - return request.accept_languages.best_match(get_available_locale_codes()) + override_lang = request.headers.get('X-Override-Accept-Language') + if override_lang and override_lang in get_available_locale_codes(): + return override_lang + return session.get('preferred_lang', request.accept_languages.best_match(get_available_locale_codes())) app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html) diff --git a/libretranslate/templates/app.js.template b/libretranslate/templates/app.js.template index f94bbf0..a8fed99 100644 --- a/libretranslate/templates/app.js.template +++ b/libretranslate/templates/app.js.template @@ -1,6 +1,6 @@ // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0 // API host/endpoint -var BaseUrl = window.location.protocol + "//" + window.location.host + url_prefix ; +var BaseUrl = window.location.protocol + "//" + window.location.host + "{{ url_prefix }}" ; var htmlRegex = /<(.*)>.*?|<(.*)\/>/; document.addEventListener('DOMContentLoaded', function(){ var sidenavElems = document.querySelectorAll('.sidenav'); diff --git a/libretranslate/templates/index.html b/libretranslate/templates/index.html index 185ce43..deead3f 100644 --- a/libretranslate/templates/index.html +++ b/libretranslate/templates/index.html @@ -9,15 +9,12 @@ - - + @@ -75,7 +72,7 @@
  • language
  • - {% for l in available_locales %}{% endfor %} {{ _h("Edit") }}create @@ -83,6 +80,18 @@ {% endset %} {{ menulinks }} +