Add string renaming CLI
This commit is contained in:
parent
cbb334c7ea
commit
49d592bfca
@ -86,6 +86,7 @@ def create_app(test_config=None):
|
|||||||
app.register_blueprint(indices.blueprint)
|
app.register_blueprint(indices.blueprint)
|
||||||
|
|
||||||
from . import lang
|
from . import lang
|
||||||
|
lang.add_translation_commands(app)
|
||||||
|
|
||||||
@app.route("/translations", methods=["GET", "POST"])
|
@app.route("/translations", methods=["GET", "POST"])
|
||||||
def strings_translation():
|
def strings_translation():
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from flask import session
|
import click
|
||||||
|
from flask import Flask, session, current_app
|
||||||
|
|
||||||
LanguageMapping = dict[str, str]
|
LanguageMapping = dict[str, str]
|
||||||
|
|
||||||
@ -32,3 +34,45 @@ def localize(text_id: str, language: dict[str, str] = None) -> str:
|
|||||||
preference = session.get("language", "ja")
|
preference = session.get("language", "ja")
|
||||||
language = LANGUAGES.get(preference)
|
language = LANGUAGES.get(preference)
|
||||||
return language[text_id]
|
return language[text_id]
|
||||||
|
|
||||||
|
|
||||||
|
@click.command("add-string")
|
||||||
|
@click.argument("string")
|
||||||
|
def add_string(string):
|
||||||
|
"""Add a new string to the translations."""
|
||||||
|
for path in Path("kanken_online/static/lang").glob("*.json"):
|
||||||
|
with open(path) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
data[string] = ""
|
||||||
|
with open(path, mode="w") as f:
|
||||||
|
json.dump(data, f, ensure_ascii=False, indent=0)
|
||||||
|
|
||||||
|
@click.command("set-string")
|
||||||
|
@click.argument("lang")
|
||||||
|
@click.argument("string")
|
||||||
|
def set_string(lang, string, value):
|
||||||
|
"""Set the value of a string for a particular language's translation file."""
|
||||||
|
path = Path("kanken_online/static/lang", f"{lang}.json")
|
||||||
|
with open(path) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
data[string] = value
|
||||||
|
with open(path, mode="w") as f:
|
||||||
|
json.dump(data, f, ensure_ascii=False, indent=0)
|
||||||
|
|
||||||
|
@click.command("rename-string")
|
||||||
|
@click.argument("string")
|
||||||
|
@click.argument("new_name")
|
||||||
|
def rename_string(string, new_name):
|
||||||
|
"""Rename a string in the translations files."""
|
||||||
|
for path in Path("kanken_online/static/lang").glob("*.json"):
|
||||||
|
with open(path) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
data[new_name] = data[string]
|
||||||
|
del data[string]
|
||||||
|
with open(path, mode="w") as f:
|
||||||
|
json.dump(data, f, ensure_ascii=False, indent=0)
|
||||||
|
|
||||||
|
def add_translation_commands(app: Flask):
|
||||||
|
app.cli.add_command(add_string)
|
||||||
|
app.cli.add_command(set_string)
|
||||||
|
app.cli.add_command(rename_string)
|
||||||
|
Loading…
Reference in New Issue
Block a user