diff --git a/kanken_online/__init__.py b/kanken_online/__init__.py index b95881e..ae6abed 100644 --- a/kanken_online/__init__.py +++ b/kanken_online/__init__.py @@ -86,6 +86,7 @@ def create_app(test_config=None): app.register_blueprint(indices.blueprint) from . import lang + lang.add_translation_commands(app) @app.route("/translations", methods=["GET", "POST"]) def strings_translation(): diff --git a/kanken_online/lang.py b/kanken_online/lang.py index 370b05f..39f861b 100644 --- a/kanken_online/lang.py +++ b/kanken_online/lang.py @@ -1,6 +1,8 @@ import json +import os from pathlib import Path -from flask import session +import click +from flask import Flask, session, current_app LanguageMapping = dict[str, str] @@ -32,3 +34,45 @@ def localize(text_id: str, language: dict[str, str] = None) -> str: preference = session.get("language", "ja") language = LANGUAGES.get(preference) 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)