1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-18 08:27:03 +02:00
LibreTranslate/libretranslate/locales.py
2023-01-05 13:12:35 -05:00

34 lines
1.0 KiB
Python

import os
import json
from functools import cache
from flask_babel import gettext as _
from flask_babel import lazy_gettext as _lazy
from markupsafe import escape, Markup
@cache
def get_available_locales():
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'))]
# Javascript code should use _e instead of _
def gettext_escaped(text, **variables):
return json.dumps(_(text, **variables))
# 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
return Markup(s if not v else s % v)