[feat/flask] Reworked locales system to use separate .ini file for each language

This commit is contained in:
2025-03-25 14:45:21 +02:00
parent ab629e3709
commit 952796182d
11 changed files with 222 additions and 186 deletions

30
app.py
View File

@@ -1,14 +1,28 @@
from flask import Flask, render_template, request, url_for, redirect
import os
from datetime import date
from configparser import ConfigParser
locale = ConfigParser()
with open('locale.ini', encoding='utf-8') as f:
locale.read_file(f)
from flask import Flask, render_template, url_for, redirect
cp = ConfigParser()
app = Flask(__name__)
print(locale)
def load_all_locales():
locales = {}
locales_dir = 'locales'
for filename in os.listdir(locales_dir):
if filename.endswith('.ini'):
lang = os.path.splitext(filename)[0]
cp = ConfigParser()
with open(os.path.join(locales_dir, filename), encoding='utf-8') as f:
cp.read_file(f)
locales[lang] = {section: dict(cp[section]) for section in cp.sections()}
return locales
locales = load_all_locales()
@app.route("/")
def home():
@@ -16,11 +30,13 @@ def home():
@app.route("/<lang>")
def index(lang):
return render_template(f'{lang}.html', locale=locale, lang=lang, year=date.today().year) # TODO: Check for vulnerability
return render_template(f'{lang}.html', locale=locales[lang], lang=lang, year=date.today().year) # TODO: Check for vulnerability
@app.route("/<lang>/download")
def download_page(lang):
return render_template('tmpl/download.htm', locale=locale, lang=lang)
return render_template('tmpl/download.htm', locale=locales[lang], lang=lang, year=date.today().year)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)