Add theme and language select functionality

This commit is contained in:
Kiril Kovachev 2024-10-14 17:31:07 +01:00
parent 152a9bc950
commit d1744e672a
3 changed files with 47 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import os
from flask import Flask, render_template, g
from flask import Flask, redirect, render_template, g, request, session
from pathlib import Path
from .auth import login_required
@ -31,11 +31,19 @@ def create_app(test_config=None):
@app.route("/about")
def about_page():
return render_template("about.html")
def update_settings(form):
session["language"] = form["language"]
session["theme"] = form["theme"]
return redirect("/options")
@app.route("/options")
@app.route("/options", methods=["GET", "POST"])
@login_required
def options():
return render_template("options.html")
if request.method == "GET":
return render_template("options.html")
else:
return update_settings(request.form)
@app.route("/kanji/<kanji>")
def kanji_page(kanji: str):
@ -78,9 +86,9 @@ def create_app(test_config=None):
from . import lang
def use_english(text_id: str):
return lang.localize(text_id, lang.JAPANESE)
app.jinja_env.globals.update(localize=use_english)
# def use_english(text_id: str):
# return lang.localize(text_id, lang.JAPANESE)
app.jinja_env.globals.update(localize=lang.localize)
return app

View File

@ -1,3 +1,5 @@
from flask import session
EXISTING_STRINGS = {
"kanken_online",
"options",
@ -47,6 +49,10 @@ ENGLISH = {
"radical_index": "Radical index",
"indivisible_index": "Indivisible kanji index",
"phonetic_series": "Phonetic series",
"dark_theme": "Dark theme",
"light_theme": "Light theme",
"language": "Language",
"theme": "Color settings"
}
JAPANESE = {
@ -74,17 +80,24 @@ JAPANESE = {
"radical_index": "部首索引",
"indivisible_index": "不可分漢字索引",
"phonetic_series": "諧声域索引",
"dark_theme": "ダークモード",
"light_theme": "ライトモード",
"language": "言語",
"theme": "色設定"
}
LANGUAGES = [
ENGLISH,
JAPANESE
]
LANGUAGES = {
"en": ENGLISH,
"ja": JAPANESE
}
# assert all(all(key in lang for key in EXISTING_STRINGS) for lang in LANGUAGES) # Ensure all strings are mapped for all existing languages
# assert not [key for lang in LANGUAGES for key in lang if ((key in lang) and (key not in EXISTING_STRINGS))]
# assert not any((((key in lang) and (key not in EXISTING_STRINGS)) for key in lang) for lang in LANGUAGES) # Ensure no languages have strings not specified by the main index
def localize(text_id: str, language: dict[str, str]) -> str:
def localize(text_id: str, language: dict[str, str] = None) -> str:
if language is None:
preference = session.get("language", "en")
language = LANGUAGES.get(preference)
return language[text_id]

View File

@ -5,5 +5,19 @@
{% endblock %}
{% block content %}
Blahdy blah
<form method="post">
<label for="language-select">{{ localize("language") }}</label>
<select name="language" id="language-select">
<option value="ja">日本語</option>
<option value="en">English</option>
</select>
<label for="theme-select">{{ localize("theme") }}</label>
<select name="theme" id="theme-select">
<option value="dark">{{ localize("dark_theme") }}</option>
<option value="light">{{ localize("light_theme") }}</option>
</select>
<hr>
<button type="submit">Okay</button>
</form>
{% endblock %}