Translation scripts

This commit is contained in:
Piero Toffanin 2023-01-04 12:15:18 -05:00
parent bd9a153773
commit 05900ff556
8 changed files with 71 additions and 5 deletions

2
babel.cfg Normal file
View file

@ -0,0 +1,2 @@
[python: **.py]
[jinja2: **/templates/**]

16
compile_translations.py Executable file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env python
import sys
import os
from babel.messages.frontend import main as pybabel
if __name__ == "__main__":
translations_dir = os.path.join("libretranslate", "translations")
if not os.path.isdir(translations_dir):
os.makedirs(translations_dir)
print("Compiling translations")
sys.argv = ["", "compile", "-d", translations_dir]
pybabel()

View file

@ -15,6 +15,7 @@ from flask_swagger_ui import get_swaggerui_blueprint
from translatehtml import translate_html
from werkzeug.utils import secure_filename
from werkzeug.exceptions import HTTPException
from flask_babel import Babel, gettext as _
from libretranslate import flood, remove_translated_files, security
from libretranslate.language import detect_languages, improve_translation_formatting
@ -53,7 +54,7 @@ def get_req_api_key():
def get_json_dict(request):
d = request.get_json()
if not isinstance(d, dict):
abort(400, description="Invalid JSON format")
abort(400, description=_("Invalid JSON format"))
return d
@ -121,7 +122,7 @@ def create_app(args):
# Map userdefined frontend languages to argos language object.
if args.frontend_language_source == "auto":
frontend_argos_language_source = type(
"obj", (object,), {"code": "auto", "name": "Auto Detect"}
"obj", (object,), {"code": "auto", "name": _("Auto Detect")}
)
else:
frontend_argos_language_source = next(
@ -294,6 +295,14 @@ def create_app(args):
return render_template("javascript-licenses.html")
@bp.route("/static/js/app.js")
@limiter.exempt
def appjs():
if args.disable_web_ui:
abort(404)
return render_template("app.js.template")
@bp.get("/languages")
@limiter.exempt
def langs():
@ -1002,12 +1011,18 @@ def create_app(args):
swag["info"]["version"] = get_version()
swag["info"]["title"] = "LibreTranslate"
@app.route(API_URL)
@limiter.exempt
def spec():
return jsonify(swag)
babel = Babel(app)
@babel.localeselector
def get_locale():
# TODO: populate from available locales
return request.accept_languages.best_match(['en', 'it'])
# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL)
if args.url_prefix:

View file

@ -28,7 +28,7 @@ document.addEventListener('DOMContentLoaded', function(){
detectedLangText: "",
copyTextLabel: "Copy text",
copyTextLabel: {{ _("Copy text") }},
suggestions: false,
isSuggesting: false,

View file

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LibreTranslate - Free and Open Source Machine Translation API</title>
<title>LibreTranslate - {{ _("Free and Open Source Machine Translation API") }}</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<meta name="description" content="Free and Open Source Machine Translation API. 100% self-hosted, offline capable and easy to setup. Run your own API server in just a few minutes.">
<meta name="keywords" content="translation,api">

View file

@ -0,0 +1 @@
**/*.mo

View file

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

31
update_translations.py Executable file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env python
import sys
import os
from babel.messages.frontend import main as pybabel
from libretranslate.language import load_languages
# Update strings
if __name__ == "__main__":
translations_dir = os.path.join("libretranslate", "translations")
if not os.path.isdir(translations_dir):
os.makedirs(translations_dir)
messagespot = os.path.join(translations_dir, "messages.pot")
print("Updating %s" % messagespot)
sys.argv = ["", "extract", "-F", "babel.cfg", "-o", messagespot, "libretranslate"]
pybabel()
# Load list of languages
print("Loading languages")
languages = [l.code for l in load_languages() if l != "en"]
print(languages)
languages = ["it"]
for l in languages:
cmd = "init"
if os.path.isdir(os.path.join(translations_dir, l)):
cmd = "update"
sys.argv = ["", cmd, "-i", messagespot, "-d", translations_dir, "-l", l]
pybabel()