Add string renaming CLI

This commit is contained in:
Kiril Kovachev 2024-10-16 11:37:52 +01:00
parent cbb334c7ea
commit 49d592bfca
2 changed files with 46 additions and 1 deletions

View File

@ -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():

View File

@ -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)