From 754ac65b9eebe60c40909060585cfc3ce2de121b Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 6 Jan 2023 10:27:39 -0500 Subject: [PATCH] Locale selector UI works --- VERSION | 2 +- libretranslate/app.py | 5 +++-- libretranslate/locales.py | 25 ++++++++++++++++++++++--- libretranslate/locales/it/meta.json | 4 ++++ libretranslate/static/css/main.css | 22 ++++++++++++++++++++++ libretranslate/templates/index.html | 11 +++++------ update_locales.py | 20 ++++++++++++++------ 7 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 libretranslate/locales/it/meta.json diff --git a/VERSION b/VERSION index e05cb33..d4c4950 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.8 +1.3.9 diff --git a/libretranslate/app.py b/libretranslate/app.py index 44e685d..24ec170 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -19,7 +19,7 @@ from flask_babel import Babel from libretranslate import flood, remove_translated_files, security from libretranslate.language import detect_languages, improve_translation_formatting -from libretranslate.locales import (_, _lazy, get_available_locales, gettext_escaped, +from libretranslate.locales import (_, _lazy, get_available_locales, get_available_locale_codes, gettext_escaped, gettext_html, lazy_swag, get_alternate_locale_links) from .api_keys import Database, RemoteDatabase @@ -287,6 +287,7 @@ def create_app(args): 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()], current_locale=get_locale(), alternate_locales=get_alternate_locale_links() ) @@ -1013,7 +1014,7 @@ def create_app(args): babel = Babel(app) @babel.localeselector def get_locale(): - return request.accept_languages.best_match(get_available_locales()) + return request.accept_languages.best_match(get_available_locale_codes()) app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html) diff --git a/libretranslate/locales.py b/libretranslate/locales.py index 5f51ca0..8c308b9 100644 --- a/libretranslate/locales.py +++ b/libretranslate/locales.py @@ -7,11 +7,30 @@ from flask_babel import lazy_gettext as _lazy from markupsafe import escape, Markup @cache -def get_available_locales(): +def get_available_locales(only_reviewed=True): 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'))] + res = [{'code': 'en', 'name': 'English'}] + + for d in dirs: + meta_file = os.path.join(d, 'meta.json') + if os.path.isdir(os.path.join(d, 'LC_MESSAGES')) and os.path.isfile(meta_file): + try: + with open(meta_file) as f: + j = json.loads(f.read()) + except Exception as e: + print(e) + continue + + if j.get('reviewed') or not only_reviewed: + res.append({'code': os.path.basename(d), 'name': j.get('name', '')}) + + return res + +@cache +def get_available_locale_codes(only_reviewed=True): + return [l['code'] for l in get_available_locales(only_reviewed=only_reviewed)] @cache def get_alternate_locale_links(): @@ -19,7 +38,7 @@ def get_alternate_locale_links(): if tmpl is None: return [] - locales = get_available_locales() + locales = get_available_locale_codes() result = [] for l in locales: link = tmpl.replace("{LANG}", l) diff --git a/libretranslate/locales/it/meta.json b/libretranslate/locales/it/meta.json new file mode 100644 index 0000000..d978bc7 --- /dev/null +++ b/libretranslate/locales/it/meta.json @@ -0,0 +1,4 @@ +{ + "name": "Italian", + "reviewed": true +} \ No newline at end of file diff --git a/libretranslate/static/css/main.css b/libretranslate/static/css/main.css index 3300596..3fdcc25 100644 --- a/libretranslate/static/css/main.css +++ b/libretranslate/static/css/main.css @@ -52,6 +52,21 @@ h3.header { box-shadow: 0 2px 2px 0 rgb(0 0 0 / 14%), 0 3px 1px -2px rgb(0 0 0 / 12%), 0px 4px 5px 0 rgb(0 0 0 / 20%); } +.locale-panel{ + display: none; +} + +#nav:hover #change-language:hover + .locale-panel, +#nav-mobile:hover #change-language:hover + .locale-panel{ + display: block; +} + +#nav:hover .locale-panel:hover, +#nav-mobile .locale-panel:hover{ + display: block; +} + + .locale-panel select{ display: block; height: 32px; @@ -65,6 +80,12 @@ h3.header { padding: 0; margin-top: 6px; text-align: right; + text-decoration: none; + height: 28px; +} + +.locale-panel a:hover{ + text-decoration: underline; } .locale-panel a i.material-icons{ @@ -84,6 +105,7 @@ h3.header { #nav-mobile .locale-panel{ color: rgba(0,0,0,0.87); padding: 0 32px; + padding-top: 12px; } #nav-mobile .locale-panel a{ diff --git a/libretranslate/templates/index.html b/libretranslate/templates/index.html index ac41953..185ce43 100644 --- a/libretranslate/templates/index.html +++ b/libretranslate/templates/index.html @@ -62,7 +62,7 @@ LibreTranslate -