2023-01-05 18:12:35 +00:00
|
|
|
import json
|
2023-07-09 10:29:11 +00:00
|
|
|
import os
|
2023-01-06 17:02:45 +00:00
|
|
|
from functools import lru_cache
|
2023-07-09 10:29:11 +00:00
|
|
|
|
2023-01-04 22:54:07 +00:00
|
|
|
from flask_babel import gettext as _
|
2023-01-05 18:12:35 +00:00
|
|
|
from flask_babel import lazy_gettext as _lazy
|
2023-07-09 10:29:11 +00:00
|
|
|
from markupsafe import Markup, escape
|
2023-01-05 18:12:35 +00:00
|
|
|
|
2023-01-04 20:36:26 +00:00
|
|
|
|
2023-01-06 17:02:45 +00:00
|
|
|
@lru_cache(maxsize=None)
|
2023-01-06 20:27:04 +00:00
|
|
|
def get_available_locales(only_reviewed=True, sort_by_name=False):
|
2023-01-04 20:36:26 +00:00
|
|
|
locales_dir = os.path.join(os.path.dirname(__file__), 'locales')
|
|
|
|
dirs = [os.path.join(locales_dir, d) for d in os.listdir(locales_dir)]
|
|
|
|
|
2023-01-06 20:27:04 +00:00
|
|
|
res = [{'code': 'en', 'name': 'English', 'reviewed': True}]
|
2023-01-06 15:27:39 +00:00
|
|
|
|
|
|
|
for d in dirs:
|
2023-01-06 20:27:04 +00:00
|
|
|
if d == 'en':
|
|
|
|
continue
|
|
|
|
|
2023-01-06 15:27:39 +00:00
|
|
|
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:
|
2023-01-06 20:27:04 +00:00
|
|
|
res.append({'code': os.path.basename(d), 'name': j.get('name', ''), 'reviewed': j.get('reviewed', False)})
|
|
|
|
|
|
|
|
if sort_by_name:
|
|
|
|
res.sort(key=lambda s: s['name'])
|
2023-01-06 15:27:39 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2023-01-06 17:02:45 +00:00
|
|
|
@lru_cache(maxsize=None)
|
2023-01-06 15:27:39 +00:00
|
|
|
def get_available_locale_codes(only_reviewed=True):
|
|
|
|
return [l['code'] for l in get_available_locales(only_reviewed=only_reviewed)]
|
2023-01-04 22:54:07 +00:00
|
|
|
|
2023-01-06 17:02:45 +00:00
|
|
|
@lru_cache(maxsize=None)
|
2023-01-05 19:36:50 +00:00
|
|
|
def get_alternate_locale_links():
|
|
|
|
tmpl = os.environ.get("LT_LOCALE_LINK_TEMPLATE")
|
|
|
|
if tmpl is None:
|
|
|
|
return []
|
2023-07-23 11:24:15 +00:00
|
|
|
|
2023-01-06 15:27:39 +00:00
|
|
|
locales = get_available_locale_codes()
|
2023-01-05 19:36:50 +00:00
|
|
|
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
|
|
|
|
|
2023-01-04 22:54:07 +00:00
|
|
|
# Javascript code should use _e instead of _
|
|
|
|
def gettext_escaped(text, **variables):
|
2023-01-05 18:12:35 +00:00
|
|
|
return json.dumps(_(text, **variables))
|
2023-01-04 22:54:07 +00:00
|
|
|
|
|
|
|
# HTML should be escaped using _h instead of _
|
|
|
|
def gettext_html(text, **variables):
|
|
|
|
# Translate text without args
|
|
|
|
s = str(escape(_(text)))
|
|
|
|
|
|
|
|
v = {}
|
|
|
|
if variables:
|
|
|
|
for k in variables:
|
|
|
|
if hasattr(variables[k], 'unescape'):
|
|
|
|
v[k] = variables[k].unescape()
|
|
|
|
else:
|
|
|
|
v[k] = Markup(variables[k])
|
|
|
|
|
|
|
|
# Variables are assumed to be already escaped and thus safe
|
2023-01-05 19:07:39 +00:00
|
|
|
return Markup(s if not v else s % v)
|
|
|
|
|
|
|
|
def swag_eval(swag, func):
|
|
|
|
# Traverse the swag spec structure
|
|
|
|
# and call func on summary and description keys
|
|
|
|
for k in swag:
|
|
|
|
if k in ['summary', 'description'] and isinstance(swag[k], str) and swag[k] != "":
|
|
|
|
swag[k] = func(swag[k])
|
|
|
|
elif k == 'tags' and isinstance(swag[k], list):
|
|
|
|
swag[k] = [func(v) for v in swag[k]]
|
|
|
|
elif isinstance(swag[k], dict):
|
|
|
|
swag_eval(swag[k], func)
|
2023-01-06 22:53:36 +00:00
|
|
|
elif isinstance(swag[k], list) and k != 'consumes':
|
2023-01-06 21:35:07 +00:00
|
|
|
for i in swag[k]:
|
|
|
|
if isinstance(i, str):
|
|
|
|
func(i)
|
|
|
|
elif isinstance(i, dict):
|
|
|
|
swag_eval(i, func)
|
2023-01-05 19:07:39 +00:00
|
|
|
|
|
|
|
return swag
|
|
|
|
|
|
|
|
def lazy_swag(swag):
|
|
|
|
return swag_eval(swag, _lazy)
|