Dump JSON translations from translation form

This commit is contained in:
Kiril Kovachev 2024-10-15 18:46:59 +01:00
parent 9cbf356444
commit bc23f2d1b8
2 changed files with 23 additions and 17 deletions

View File

@ -1,5 +1,6 @@
import json
import os
from flask import Flask, redirect, render_template, g, request, session
from flask import Flask, redirect, render_template, g, request, session, url_for
from pathlib import Path
from .auth import login_required
@ -74,21 +75,6 @@ def create_app(test_config=None):
def kotoba_page(kotoba: str):
return render_template("kotoba.html", kotoba=kotoba)
@app.route("/translations", methods=["GET", "POST"])
def strings_translation():
if request.method == "GET":
strings = {}
for language, language_data in lang.LANGUAGES.items():
strings[language] = {
string: translation for string, translation in language_data.items()
}
return render_template("translations.html", strings=strings)
else:
# TODO
print(request.form)
return redirect("/translations")
from . import database
database.initialize_app(app)
@ -101,6 +87,26 @@ def create_app(test_config=None):
from . import lang
@app.route("/translations", methods=["GET", "POST"])
def strings_translation():
if request.method == "GET":
strings = {}
for language, language_data in lang.LANGUAGES.items():
strings[language] = {
string: translation for string, translation in language_data.items()
}
return render_template("translations.html", strings=strings)
else:
strings = {}
for i, language in enumerate(lang.LANGUAGES):
strings[language] = {
string: request.form.getlist(string)[i] for string in request.form
}
with open(Path(app.root_path, "static", "lang", f"{language}.json"), mode="w") as f:
json.dump(strings[language], f, ensure_ascii=False, indent=0)
return redirect("/translations")
# def use_english(text_id: str):
# return lang.localize(text_id, lang.JAPANESE)
app.jinja_env.globals.update(localize=lang.localize)

View File

@ -10,7 +10,7 @@
<h2>{{ language }}</h2>
{% for string, translation in data.items() %}
<label for="{{ string }}">{{ string }}</label>
<input type="text" id="{{ string }}" value="{{ translation }}">
<input type="text" id="{{ string }}" name="{{ string }}" value="{{ translation }}">
<br>
{% endfor %}
{% endfor %}