l10n: add en, de, ja and fr
This commit is contained in:
@@ -1,8 +1,62 @@
|
||||
from os import system as console
|
||||
from flask import Flask, render_template
|
||||
from configparser import ConfigParser
|
||||
from flask import (
|
||||
Flask,
|
||||
g,
|
||||
request,
|
||||
render_template,
|
||||
)
|
||||
|
||||
|
||||
translations_cache = {}
|
||||
|
||||
def load_translations(lang):
|
||||
if lang not in translations_cache:
|
||||
translations_cache[lang] = {}
|
||||
|
||||
try:
|
||||
config = ConfigParser()
|
||||
config.read(f'locale/{lang}.ini')
|
||||
for section in config.sections():
|
||||
translations_cache[lang][section] = dict(config.items(section))
|
||||
except:
|
||||
pass
|
||||
|
||||
return translations_cache[lang]
|
||||
|
||||
|
||||
def get_locale():
|
||||
return request.accept_languages.best_match(('en', 'ru', 'de', 'fr', 'ja'), 'en')
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
g.locale = get_locale()
|
||||
g.translations = load_translations(g.locale)
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_translations():
|
||||
def translate(text, **kwargs):
|
||||
if ":" in text:
|
||||
section, key = text.split(":", 1)
|
||||
else:
|
||||
section, key = "common", text
|
||||
|
||||
template = g.translations \
|
||||
.get(section, {}) \
|
||||
.get(key, f"${section}: {key}$")
|
||||
|
||||
try:
|
||||
return template.format(**kwargs)
|
||||
except:
|
||||
return template
|
||||
|
||||
return {'_': translate}
|
||||
|
||||
|
||||
if app.debug:
|
||||
console("sass static/style/main.scss static/style/main.css")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user