1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-18 08:27:03 +02:00

Language selector working

This commit is contained in:
Piero Toffanin 2023-01-06 11:50:51 -05:00
parent 754ac65b9e
commit 58350d868c
4 changed files with 35 additions and 13 deletions

View File

@ -9,9 +9,10 @@ from timeit import default_timer
import argostranslatefiles import argostranslatefiles
from argostranslatefiles import get_supported_formats from argostranslatefiles import get_supported_formats
from flask import (abort, Blueprint, Flask, jsonify, render_template, request, 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 import swagger
from flask_swagger_ui import get_swaggerui_blueprint from flask_swagger_ui import get_swaggerui_blueprint
from flask_session import Session
from translatehtml import translate_html from translatehtml import translate_html
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
@ -277,6 +278,10 @@ def create_app(args):
if args.disable_web_ui: if args.disable_web_ui:
abort(404) 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( return render_template(
"index.html", "index.html",
gaId=args.ga_id, gaId=args.ga_id,
@ -286,19 +291,19 @@ def create_app(args):
web_version=os.environ.get("LT_WEB") is not None, web_version=os.environ.get("LT_WEB") is not None,
version=get_version(), version=get_version(),
swagger_url=SWAGGER_URL, swagger_url=SWAGGER_URL,
url_prefix=args.url_prefix, available_locales=[{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales(not args.debug)],
available_locales=[{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales()],
current_locale=get_locale(), current_locale=get_locale(),
alternate_locales=get_alternate_locale_links() alternate_locales=get_alternate_locale_links()
) )
@bp.route("/static/js/app.js") @bp.route("/js/app.js")
@limiter.exempt @limiter.exempt
def appjs(): def appjs():
if args.disable_web_ui: if args.disable_web_ui:
abort(404) abort(404)
return render_template("app.js.template", return render_template("app.js.template",
url_prefix=args.url_prefix,
get_api_key_link=args.get_api_key_link) get_api_key_link=args.get_api_key_link)
@bp.get("/languages") @bp.get("/languages")
@ -991,6 +996,11 @@ def create_app(args):
return jsonify({"success": True}) return jsonify({"success": True})
app = Flask(__name__) app = Flask(__name__)
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_FILE_DIR"] = os.path.join("db", "sessions")
Session(app)
if args.debug: if args.debug:
app.config["TEMPLATES_AUTO_RELOAD"] = True app.config["TEMPLATES_AUTO_RELOAD"] = True
if args.url_prefix: if args.url_prefix:
@ -1009,12 +1019,14 @@ def create_app(args):
def spec(): def spec():
return jsonify(lazy_swag(swag)) return jsonify(lazy_swag(swag))
app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales' app.config["BABEL_TRANSLATION_DIRECTORIES"] = 'locales'
babel = Babel(app) babel = Babel(app)
@babel.localeselector @babel.localeselector
def get_locale(): 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) app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html)

View File

@ -1,6 +1,6 @@
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
// API host/endpoint // 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 = /<(.*)>.*?|<(.*)\/>/; var htmlRegex = /<(.*)>.*?|<(.*)\/>/;
document.addEventListener('DOMContentLoaded', function(){ document.addEventListener('DOMContentLoaded', function(){
var sidenavElems = document.querySelectorAll('.sidenav'); var sidenavElems = document.querySelectorAll('.sidenav');

View File

@ -9,15 +9,12 @@
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}"> <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<meta name="description" content="{{ _h('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.') }}"> <meta name="description" content="{{ _h('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.') }}">
<meta name="keywords" content="{{ _h('translation') }},{{ _h('api') }}"> <meta name="keywords" content="{{ _h('translation') }},{{ _h('api') }}">
<script type="text/javascript">
var url_prefix = "{{ url_prefix }}"
</script>
<link rel="preload" href="{{ url_for('static', filename='icon.svg') }}" as="image" /> <link rel="preload" href="{{ url_for('static', filename='icon.svg') }}" as="image" />
<link rel="preload" href="{{ url_for('static', filename='js/vue@2.js') }}" as="script"> <link rel="preload" href="{{ url_for('static', filename='js/vue@2.js') }}" as="script">
<link rel="preload" href="{{ url_for('static', filename='js/materialize.min.js') }}" as="script"> <link rel="preload" href="{{ url_for('static', filename='js/materialize.min.js') }}" as="script">
<link rel="preload" href="{{ url_for('static', filename='js/prism.min.js') }}" as="script"> <link rel="preload" href="{{ url_for('static', filename='js/prism.min.js') }}" as="script">
<link rel="preload" href="{{ url_for('static', filename='js/app.js') }}?v={{ version }}" as="script"> <link rel="preload" href="js/app.js?v={{ version }}" as="script">
<link rel="preload" href="{{ url_for('static', filename='css/materialize.min.css') }}" as="style"/> <link rel="preload" href="{{ url_for('static', filename='css/materialize.min.css') }}" as="style"/>
<link rel="preload" href="{{ url_for('static', filename='css/material-icons.css') }}" as="style"/> <link rel="preload" href="{{ url_for('static', filename='css/material-icons.css') }}" as="style"/>
@ -75,7 +72,7 @@
<li id="change-language"><a class="noline" href="javascript:void(0)" title="{{ _h('Change language') }}"><i class="material-icons">language</i></a> <li id="change-language"><a class="noline" href="javascript:void(0)" title="{{ _h('Change language') }}"><i class="material-icons">language</i></a>
</li> </li>
<li class="locale-panel" id="locale-panel"> <li class="locale-panel" id="locale-panel">
<select id="locales"> <select id="locales" onchange="changeLocale(this)">
{% for l in available_locales %}<option value="{{ l['code'] }}" {{ 'selected' if current_locale == l['code'] else ''}}>{{ l['name'] }}</option>{% endfor %} {% for l in available_locales %}<option value="{{ l['code'] }}" {{ 'selected' if current_locale == l['code'] else ''}}>{{ l['name'] }}</option>{% endfor %}
</select> </select>
<a href="#TODO">{{ _h("Edit") }}<i class="material-icons">create</i></a> <a href="#TODO">{{ _h("Edit") }}<i class="material-icons">create</i></a>
@ -83,6 +80,18 @@
{% endset %} {% endset %}
{{ menulinks }} {{ menulinks }}
</ul> </ul>
<script>
var localeLinks = {
{% for al in alternate_locales %}"{{ al.lang }}": "{{ al.link }}"{% if not loop.last %},{% endif %}
{% endfor %}
};
function changeLocale(slt){
var lang = slt.value;
if (localeLinks[lang]) location.href = localeLinks[lang];
else location.href = '?lang=' + slt.value;
}
</script>
<ul id="nav-mobile" class="sidenav"> <ul id="nav-mobile" class="sidenav">
{{ menulinks }} {{ menulinks }}
@ -325,6 +334,6 @@
// @license-end // @license-end
</script> </script>
<script src="{{ url_for('static', filename='js/prism.min.js') }}"></script> <script src="{{ url_for('static', filename='js/prism.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/app.js') }}?v={{ version }}"></script> <script src="js/app.js?v={{ version }}"></script>
</body> </body>
</html> </html>

View File

@ -4,6 +4,7 @@ flask-swagger==0.2.14
flask-swagger-ui==4.11.1 flask-swagger-ui==4.11.1
Flask-Limiter==2.6.3 Flask-Limiter==2.6.3
Flask-Babel==2.0.0 Flask-Babel==2.0.0
Flask-Session==0.4.0
waitress==2.1.2 waitress==2.1.2
expiringdict==1.2.2 expiringdict==1.2.2
LTpycld2==0.42 LTpycld2==0.42