Skip to content

Commit

Permalink
Use Flask-Babel
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyEye-FAST committed Jan 27, 2024
1 parent eb22d5e commit 24b3f77
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 26 deletions.
3 changes: 1 addition & 2 deletions .flaskenv
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
FLASK_DEBUG = 1
BABEL_DEFAULT_LOCALE = zh
FLASK_DEBUG = 1
39 changes: 18 additions & 21 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,35 @@
from os import getenv
from datetime import date

from flask import Flask, render_template, request, send_from_directory
from flask import Flask, g, render_template, request, send_from_directory
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from flask_babel import Babel, gettext

from flask_babel import Babel, lazy_gettext, format_date
from babel.dates import get_timezone_name, get_timezone

from base import data, is_valid_key, get_translation

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

babel = Babel(flask_app)

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 get_locale():
"""语言选择器"""
user = getattr(g, "user", None)
if user is not None:
return user.locale
return request.accept_languages.best_match(["zh", "en"])


babel = Babel(flask_app, locale_selector=get_locale, timezone_selector=get_timezone)


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

source_string = StringField(gettext("query_input_label"))
submit = SubmitField(gettext("query_button"))
source_string = StringField(lazy_gettext("Source string content to be queried: "))
submit = SubmitField(lazy_gettext("QUERY"))


@flask_app.route("/", methods=["GET", "POST"])
Expand Down Expand Up @@ -68,7 +64,8 @@ def index():
keys=keys,
translation=selected_translation,
date_str=date.today(),
date_str_zh=date.today().strftime("%Y年%#m月%#d日(UTC)"),
date_str_t=format_date(date.today()),
timezone_str=get_timezone_name(get_timezone(), locale=get_locale()),
)


Expand All @@ -79,4 +76,4 @@ def favicon():


if __name__ == "__main__":
flask_app.run()
flask_app.run(debug=True)
2 changes: 2 additions & 0 deletions babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[python: app.py]
[jinja2: **/templates/**.html]
1 change: 1 addition & 0 deletions base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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)
print("语言文件读取成功。")

# 读取补充字符串
if not IGNORE_SUPPLEMENTS:
Expand Down
31 changes: 31 additions & 0 deletions messages.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Translations template for PROJECT.
# Copyright (C) 2024 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2024.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-01-27 20:32+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.14.0\n"

#: app.py:22
msgid "Source string content to be queried:"
msgstr ""

#: app.py:23
msgid "QUERY"
msgstr ""

#: templates/index.html:23
msgid "Select translation key:"
msgstr ""

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flask==3.0.1
flask_babel==4.0.0
flask_wtf==1.2.1
fonttools==4.47.2
python-dotenv==1.0.1
Expand Down
8 changes: 5 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{{ form.hidden_tag() }}
{{ form.source_string.label }}{{ form.source_string(autocomplete="off") }}
{% if input_value %}
<label for="options">选择本地化键名:</label>
<label for="options">{{ _("Select translation key:") }}</label>
<select name="options" id="options">
{% for option in keys %}
{% if option == key %}
Expand Down Expand Up @@ -67,8 +67,10 @@
{% endif %}

<div class="minecraft-title">
Minecraft中文标准译名<br><span style="font-size: smaller;"><time datetime="{{ date_str }}">{{ date_str_zh
}}</time></span>
Minecraft中文标准译名<br>
<span style="font-size: smaller;">
<time datetime="{{ date_str }}">{{ date_str_t }}</time> - {{ timezone_str }}
</span>
</div>

</body>
Expand Down
Binary file added translations/zh_CN/LC_MESSAGES/messages.mo
Binary file not shown.
32 changes: 32 additions & 0 deletions translations/zh_CN/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Chinese (Simplified, China) translations for Minecraft Chinese Standard Translations.
# Copyright (C) 2024 SkyEye_FAST
# This file is distributed under the same license as the Minecraft Chinese Standard Translations project.
# SkyEye_FAST <[email protected]>, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: Minecraft Chinese Standard Translations\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2024-01-27 20:32+0800\n"
"PO-Revision-Date: 2024-01-27 20:32+0800\n"
"Last-Translator: SkyEye_FAST <[email protected]>\n"
"Language: zh_Hans_CN\n"
"Language-Team: zh_Hans_CN <[email protected]>\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.14.0\n"

#: app.py:22
msgid "Source string content to be queried: "
msgstr "查询的源字符串内容:"

#: app.py:23
msgid "QUERY"
msgstr "查询"

#: templates/index.html:23
msgid "Select translation key:"
msgstr "选择本地化键名:"

0 comments on commit 24b3f77

Please sign in to comment.