LT_LOCALE_LINK_TEMPLATE support

This commit is contained in:
Piero Toffanin 2023-01-05 14:36:50 -05:00
parent b26a3931a2
commit bfbe848c01
3 changed files with 23 additions and 3 deletions

View file

@ -19,7 +19,8 @@ from flask_babel import Babel
from libretranslate import flood, remove_translated_files, security from libretranslate import flood, remove_translated_files, security
from libretranslate.language import detect_languages, improve_translation_formatting from libretranslate.language import detect_languages, improve_translation_formatting
from libretranslate.locales import _, _lazy, get_available_locales, gettext_escaped, gettext_html, lazy_swag from libretranslate.locales import (_, _lazy, get_available_locales, gettext_escaped,
gettext_html, lazy_swag, get_alternate_locale_links)
from .api_keys import Database, RemoteDatabase from .api_keys import Database, RemoteDatabase
from .suggestions import Database as SuggestionsDatabase from .suggestions import Database as SuggestionsDatabase
@ -285,7 +286,9 @@ 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 url_prefix=args.url_prefix,
current_locale=get_locale(),
alternate_locales=get_alternate_locale_links()
) )
@bp.route("/static/js/app.js") @bp.route("/static/js/app.js")

View file

@ -13,6 +13,21 @@ def get_available_locales():
return ['en'] + [os.path.basename(d) for d in dirs if os.path.isdir(os.path.join(d, 'LC_MESSAGES'))] return ['en'] + [os.path.basename(d) for d in dirs if os.path.isdir(os.path.join(d, 'LC_MESSAGES'))]
@cache
def get_alternate_locale_links():
tmpl = os.environ.get("LT_LOCALE_LINK_TEMPLATE")
if tmpl is None:
return []
locales = get_available_locales()
result = []
for l in locales:
link = tmpl.replace("{LANG}", l)
if l == 'en':
link = link.replace("en.", "")
result.append({ 'link': link,'lang': l })
return result
# Javascript code should use _e instead of _ # Javascript code should use _e instead of _
def gettext_escaped(text, **variables): def gettext_escaped(text, **variables):
return json.dumps(_(text, **variables)) return json.dumps(_(text, **variables))

View file

@ -1,8 +1,10 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="{{ current_locale }}">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
{% for al in alternate_locales %}<link rel="alternate" hreflang="{{ al.lang }}" href="{{ al.link }}" />
{% endfor %}
<title>LibreTranslate - {{ _h("Free and Open Source Machine Translation API") }}</title> <title>LibreTranslate - {{ _h("Free and Open Source Machine Translation API") }}</title>
<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.') }}">