Skip to content

Commit

Permalink
Use WTForms and Flask-WTF
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyEye-FAST committed Jan 27, 2024
1 parent d76c749 commit f3f3897
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .flaskenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FLASK_DEBUG = 1
BABEL_DEFAULT_LOCALE = zh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__/
.idea
.vercel
.env
98 changes: 30 additions & 68 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,52 @@
# -*- encoding: utf-8 -*-
"""Minecraft中文标准译名查询网页,使用Flask编写的后端框架"""

import json
import re
from pathlib import Path
from os import getenv
from datetime import date
from flask import Flask, render_template, request, send_from_directory

LANG_DIR = Path(__file__).resolve().parent / "lang"

# 读取语言文件
print("开始读取语言文件。")
file_list = [
"en_us.json",
"zh_cn.json",
"zh_hk.json",
"zh_tw.json",
"lzh.json",
]
data = {}
for file in file_list:
with open(LANG_DIR / file, "r", encoding="utf-8") as f:
data[file.split(".", maxsplit=1)[0]] = json.load(f)

# 读取补充字符串
with open(LANG_DIR / "supplements.json", "r", encoding="utf-8") as f:
supplements = json.load(f)
for lang in ["zh_cn", "zh_hk", "zh_tw", "lzh"]:
data[lang].update(supplements[lang])
print(f"已补充{len(supplements['zh_cn'])}条字符串。")
from flask import Flask, render_template, request, send_from_directory
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField

from base import data, is_valid_key, get_translation

flask_app = Flask(__name__)
flask_app.config["SECRET_KEY"] = getenv("SECRET_KEY", "dev")

page_lang = {
"zh_cn": {
"lang_name": "简体中文(中国大陆)",
"query_input_label": "查询的源字符串内容:",
"translation_key_select_label": "选择本地化键名:",
"query_button": "查询",
},
"en_us": {
"lang_name": "English (United States)",
"query_input_label": "Source string content to be queried: ",
"translation_key_select_label": "Select translation key:",
"query_button": "QUERY",
},
}

def is_valid_key(translation_key: str):
"""判断是否为有效键名"""

prefixes = (
"block.",
"item.minecraft.",
"entity.minecraft.",
"biome.",
"effect.minecraft.",
"enchantment.minecraft.",
"trim_pattern.",
"upgrade.",
)

if (
translation_key.startswith(prefixes)
and not re.match(
r"(block\.minecraft\.|item\.minecraft\.|entity\.minecraft\.)[^.]*\.",
translation_key,
)
and translation_key
not in ["block.minecraft.set_spawn", "entity.minecraft.falling_block_type"]
and "pottery_shard" not in translation_key
):
return True

# 匹配进度键名
if re.match(r"advancements\.(.*)\.title", translation_key):
return True

return False
class QueryForm(FlaskForm):
"""查询表单"""


def get_translation(query_str: str):
"""在语言文件中匹配含有输入内容的源字符串"""
translation = {}
for k, v in data["en_us"].items():
if query_str.lower() in v.lower():
element = {lang: content.get(k, "?") for lang, content in data.items()}
translation[k] = element
return translation
source_string = StringField("查询的源字符串内容:")
submit = SubmitField("查询")


@flask_app.route("/", methods=["GET", "POST"])
def index():
"""主页面"""
form = QueryForm()

query_str = form.source_string.data
selected_option = request.form.get("options", "")
query_str = request.form.get("query-input", "")
if not query_str:
selected_option = "" # 清空下拉列表选择项

if request.method == "POST":
if form.validate_on_submit():
translation = get_translation(query_str)
keys = [k for k in translation if is_valid_key(k)]
selected_translation = translation.get(selected_option, {})
Expand All @@ -97,6 +58,7 @@ def index():

return render_template(
"index.html",
form=form,
source=source_str,
key=selected_option,
input_value=query_str,
Expand All @@ -114,4 +76,4 @@ def favicon():


if __name__ == "__main__":
flask_app.run(debug=True)
flask_app.run()
87 changes: 87 additions & 0 deletions base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- encoding: utf-8 -*-
"""基础文件"""

import re
import json
import sys
import tomllib as tl
from pathlib import Path

# 当前绝对路径
P = Path(__file__).resolve().parent

# 加载配置
CONFIG_DIR = P / "configuration.toml"
if not CONFIG_DIR.exists():
print("\n无法找到配置文件,请将配置文件放置在与此脚本同级的目录下。")
sys.exit()
with open(CONFIG_DIR, "rb") as f:
config = tl.load(f)

LANG_DIR = P / config["language_folder"]
IGNORE_SUPPLEMENTS = config["ignore_supplements"]

# 读取语言文件
print("开始读取语言文件。")
file_list = [
"en_us.json",
"zh_cn.json",
"zh_hk.json",
"zh_tw.json",
"lzh.json",
]
data = {}
for file in file_list:
with open(LANG_DIR / file, "r", encoding="utf-8") as f:
data[file.split(".", maxsplit=1)[0]] = json.load(f)

# 读取补充字符串
if not IGNORE_SUPPLEMENTS:
with open(LANG_DIR / "supplements.json", "r", encoding="utf-8") as f:
supplements = json.load(f)
for lang in ["zh_cn", "zh_hk", "zh_tw", "lzh"]:
data[lang].update(supplements[lang])
print(f"已补充{len(supplements['zh_cn'])}条字符串。")


def is_valid_key(translation_key: str):
"""判断是否为有效键名"""

prefixes = (
"block.",
"item.minecraft.",
"entity.minecraft.",
"biome.",
"effect.minecraft.",
"enchantment.minecraft.",
"trim_pattern.",
"upgrade.",
)

if (
translation_key.startswith(prefixes)
and not re.match(
r"(block\.minecraft\.|item\.minecraft\.|entity\.minecraft\.)[^.]*\.",
translation_key,
)
and translation_key
not in ["block.minecraft.set_spawn", "entity.minecraft.falling_block_type"]
and "pottery_shard" not in translation_key
):
return True

# 匹配进度键名
if re.match(r"advancements\.(.*)\.title", translation_key):
return True

return False


def get_translation(query_str: str):
"""在语言文件中匹配含有输入内容的源字符串"""
translation = {}
for k, v in data["en_us"].items():
if query_str.lower() in v.lower():
element = {lang: content.get(k, "?") for lang, content in data.items()}
translation[k] = element
return translation
5 changes: 5 additions & 0 deletions configuration.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 语言文件文件夹
language_folder = "lang"

# 是否忽略补充字符串
ignore_supplements = true
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
Flask==3.0.0
Flask==3.0.1
flask_wtf==1.2.1
fonttools==4.47.2
python-dotenv==1.0.1
Requests==2.31.0
WTForms==3.1.2
6 changes: 3 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<form class="form" action="/" method="post">
<div class="container">
<label for="query-input">查询的源字符串内容:</label>
<input type="text" name="query-input" id="query-input" value="{{ input_value }}" autocomplete="off">
{{ form.hidden_tag() }}
{{ form.source_string.label }}{{ form.source_string(autocomplete="off") }}
{% if input_value %}
<label for="options">选择本地化键名:</label>
<select name="options" id="options">
Expand All @@ -32,7 +32,7 @@
</select>
{% endif %}
</div>
<input class="submit" type="submit" value="查询">
{{ form.submit(class="submit") }}
</form>

{% if translation %}
Expand Down
12 changes: 12 additions & 0 deletions wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- encoding: utf-8 -*-
"""手动设置环境变量并导入程序实例"""

from pathlib import Path
from dotenv import load_dotenv
from app import flask_app

dotenv_path = Path(__file__).resolve().parent / ".env"
if dotenv_path.exists():
load_dotenv(dotenv_path)

app = flask_app

0 comments on commit f3f3897

Please sign in to comment.