Add theme and language select functionality
This commit is contained in:
parent
152a9bc950
commit
d1744e672a
@ -1,5 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from flask import Flask, render_template, g
|
from flask import Flask, redirect, render_template, g, request, session
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from .auth import login_required
|
from .auth import login_required
|
||||||
|
|
||||||
@ -32,10 +32,18 @@ def create_app(test_config=None):
|
|||||||
def about_page():
|
def about_page():
|
||||||
return render_template("about.html")
|
return render_template("about.html")
|
||||||
|
|
||||||
@app.route("/options")
|
def update_settings(form):
|
||||||
|
session["language"] = form["language"]
|
||||||
|
session["theme"] = form["theme"]
|
||||||
|
return redirect("/options")
|
||||||
|
|
||||||
|
@app.route("/options", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def options():
|
def options():
|
||||||
|
if request.method == "GET":
|
||||||
return render_template("options.html")
|
return render_template("options.html")
|
||||||
|
else:
|
||||||
|
return update_settings(request.form)
|
||||||
|
|
||||||
@app.route("/kanji/<kanji>")
|
@app.route("/kanji/<kanji>")
|
||||||
def kanji_page(kanji: str):
|
def kanji_page(kanji: str):
|
||||||
@ -78,9 +86,9 @@ def create_app(test_config=None):
|
|||||||
|
|
||||||
from . import lang
|
from . import lang
|
||||||
|
|
||||||
def use_english(text_id: str):
|
# def use_english(text_id: str):
|
||||||
return lang.localize(text_id, lang.JAPANESE)
|
# return lang.localize(text_id, lang.JAPANESE)
|
||||||
app.jinja_env.globals.update(localize=use_english)
|
app.jinja_env.globals.update(localize=lang.localize)
|
||||||
|
|
||||||
|
|
||||||
return app
|
return app
|
@ -1,3 +1,5 @@
|
|||||||
|
from flask import session
|
||||||
|
|
||||||
EXISTING_STRINGS = {
|
EXISTING_STRINGS = {
|
||||||
"kanken_online",
|
"kanken_online",
|
||||||
"options",
|
"options",
|
||||||
@ -47,6 +49,10 @@ ENGLISH = {
|
|||||||
"radical_index": "Radical index",
|
"radical_index": "Radical index",
|
||||||
"indivisible_index": "Indivisible kanji index",
|
"indivisible_index": "Indivisible kanji index",
|
||||||
"phonetic_series": "Phonetic series",
|
"phonetic_series": "Phonetic series",
|
||||||
|
"dark_theme": "Dark theme",
|
||||||
|
"light_theme": "Light theme",
|
||||||
|
"language": "Language",
|
||||||
|
"theme": "Color settings"
|
||||||
}
|
}
|
||||||
|
|
||||||
JAPANESE = {
|
JAPANESE = {
|
||||||
@ -74,17 +80,24 @@ JAPANESE = {
|
|||||||
"radical_index": "部首索引",
|
"radical_index": "部首索引",
|
||||||
"indivisible_index": "不可分漢字索引",
|
"indivisible_index": "不可分漢字索引",
|
||||||
"phonetic_series": "諧声域索引",
|
"phonetic_series": "諧声域索引",
|
||||||
|
"dark_theme": "ダークモード",
|
||||||
|
"light_theme": "ライトモード",
|
||||||
|
"language": "言語",
|
||||||
|
"theme": "色設定"
|
||||||
}
|
}
|
||||||
|
|
||||||
LANGUAGES = [
|
LANGUAGES = {
|
||||||
ENGLISH,
|
"en": ENGLISH,
|
||||||
JAPANESE
|
"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 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 [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
|
# 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]
|
return language[text_id]
|
||||||
|
@ -5,5 +5,19 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% 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 %}
|
{% endblock %}
|
Loading…
Reference in New Issue
Block a user