mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-24 08:51:00 +00:00
Locale selector UI works
This commit is contained in:
parent
aa5936dae5
commit
754ac65b9e
7 changed files with 71 additions and 18 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
1.3.8
|
||||
1.3.9
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
4
libretranslate/locales/it/meta.json
Normal file
4
libretranslate/locales/it/meta.json
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"name": "Italian",
|
||||
"reviewed": true
|
||||
}
|
|
@ -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{
|
||||
|
|
|
@ -62,7 +62,7 @@
|
|||
<img src="{{ url_for('static', filename='icon.svg') }}" alt="LibreTranslate" class="logo">
|
||||
<span>LibreTranslate</span>
|
||||
</a>
|
||||
<ul class="right hide-on-med-and-down top-nav position-relative">
|
||||
<ul id="nav" class="right hide-on-med-and-down top-nav position-relative">
|
||||
{% set menulinks %}
|
||||
<li><a href="{{ swagger_url }}">{{ _h("API Docs") }}</a></li>
|
||||
{% if get_api_key_link %}
|
||||
|
@ -72,14 +72,13 @@
|
|||
{% if api_keys %}
|
||||
<li><a class="noline" href="javascript:setApiKey()" title="{{ _h('Set API Key') }}"><i class="material-icons">vpn_key</i></a></li>
|
||||
{% endif %}
|
||||
<li><a class="noline" href="#" 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 class="locale-panel">
|
||||
<li class="locale-panel" id="locale-panel">
|
||||
<select id="locales">
|
||||
<option value="en">English</option>
|
||||
<option value="it">Italian</option>
|
||||
{% for l in available_locales %}<option value="{{ l['code'] }}" {{ 'selected' if current_locale == l['code'] else ''}}>{{ l['name'] }}</option>{% endfor %}
|
||||
</select>
|
||||
<a href="#">Improve this language<i class="material-icons">create</i></a>
|
||||
<a href="#TODO">{{ _h("Edit") }}<i class="material-icons">create</i></a>
|
||||
</li>
|
||||
{% endset %}
|
||||
{{ menulinks }}
|
||||
|
|
|
@ -6,7 +6,7 @@ import polib
|
|||
import json
|
||||
from babel.messages.frontend import main as pybabel
|
||||
from libretranslate.language import load_languages, improve_translation_formatting
|
||||
from libretranslate.locales import get_available_locales, swag_eval
|
||||
from libretranslate.locales import get_available_locale_codes, swag_eval
|
||||
from translatehtml import translate_html
|
||||
from libretranslate.app import get_version, create_app
|
||||
from libretranslate.main import get_args
|
||||
|
@ -58,10 +58,8 @@ if __name__ == "__main__":
|
|||
"-o", messagespot, "libretranslate"]
|
||||
pybabel()
|
||||
|
||||
|
||||
|
||||
lang_codes = [l.code for l in languages if l != "en"]
|
||||
lang_codes = ["it"] # TODO REMOVE
|
||||
lang_codes = ["it", "fr"] # TODO REMOVE
|
||||
|
||||
# Init/update
|
||||
for l in lang_codes:
|
||||
|
@ -72,10 +70,20 @@ if __name__ == "__main__":
|
|||
sys.argv = ["", cmd, "-i", messagespot, "-d", locales_dir, "-l", l]
|
||||
pybabel()
|
||||
|
||||
meta_file = os.path.join(locales_dir, l, "meta.json")
|
||||
if not os.path.isfile(meta_file):
|
||||
with open(meta_file, 'w') as f:
|
||||
f.write(json.dumps({
|
||||
'name': next((lang.name for lang in languages if lang.code == l)),
|
||||
'reviewed': False
|
||||
}, indent=4))
|
||||
print("Wrote %s" % meta_file)
|
||||
|
||||
# Automatically translate strings with libretranslate
|
||||
# when a language model is available and a string is empty
|
||||
|
||||
locales = get_available_locales()
|
||||
locales = get_available_locale_codes(only_reviewed=False)
|
||||
print(locales)
|
||||
for locale in locales:
|
||||
if locale == 'en':
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue