From d1744e672afe5aabeb83ddc2aed11ab93ab80b81 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Mon, 14 Oct 2024 17:31:07 +0100 Subject: [PATCH] Add theme and language select functionality --- kanken_online/__init__.py | 20 ++++++++++++++------ kanken_online/lang.py | 23 ++++++++++++++++++----- kanken_online/templates/options.html | 16 +++++++++++++++- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/kanken_online/__init__.py b/kanken_online/__init__.py index 82f40b2..65463e2 100644 --- a/kanken_online/__init__.py +++ b/kanken_online/__init__.py @@ -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/") 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 \ No newline at end of file diff --git a/kanken_online/lang.py b/kanken_online/lang.py index 94bf30a..209a43d 100644 --- a/kanken_online/lang.py +++ b/kanken_online/lang.py @@ -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] diff --git a/kanken_online/templates/options.html b/kanken_online/templates/options.html index 8350068..028b4e6 100644 --- a/kanken_online/templates/options.html +++ b/kanken_online/templates/options.html @@ -5,5 +5,19 @@ {% endblock %} {% block content %} - Blahdy blah +
+ + + + + +
+ +
{% endblock %} \ No newline at end of file