From d457ab0b17e568caf8d3abcae38a716075673ca9 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 12 Mar 2020 18:56:18 +1000 Subject: [PATCH] add helper to duplicate a fluent string --- qt/po/scripts/duplicate-string.py | 96 +++++++++++++++++++++++++++++++ rslib/ftl/media-check.ftl | 4 +- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 qt/po/scripts/duplicate-string.py diff --git a/qt/po/scripts/duplicate-string.py b/qt/po/scripts/duplicate-string.py new file mode 100644 index 000000000..ee0b681cf --- /dev/null +++ b/qt/po/scripts/duplicate-string.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- +import os +import json +import re +import sys +import copy +import shutil +from fluent.syntax import parse, serialize +from fluent.syntax.ast import Message, TextElement, Identifier, Pattern, Junk + +# clone an existing ftl string as a new key, optionally modifying it +# eg: +# $ python duplicate-string.py /path/to/templates/media-check.ftl window-title check-media-action + +ftl_filename, old_key, new_key = sys.argv[1:] + +# # split up replacements +# replacements = [] +# for repl in repls.split(","): +# if not repl: +# continue +# replacements.append(repl.split("=")) + +# add file as prefix to key +prefix = os.path.splitext(os.path.basename(ftl_filename))[0] +old_key = f"{prefix}-{old_key}" +new_key = f"{prefix}-{new_key}" + +# def transform_string(msg): +# for (old, new) in replacements: +# msg = msg.replace(old, f"{new}") +# # strip leading/trailing whitespace +# return msg.strip() + + +def dupe_key(fname, old, new): + if not os.path.exists(fname): + return + + orig = open(fname).read() + obj = parse(orig) + for ent in obj.body: + if isinstance(ent, Junk): + raise Exception(f"file had junk! {fname} {ent}") + + # locate the existing key + for item in obj.body: + if isinstance(item, Message): + if item.id.name == old: + item2 = copy.deepcopy(item) + item2.id.name = new + obj.body.append(item2) + break + # print(item.id.name) + # print(item.value.elements) + # for e in item.value.elements: + # print(e.value) + + modified = serialize(obj, with_junk=True) + # escape leading dots + modified = re.sub(r"(?ms)^( +)\.", '\\1{"."}', modified) + + # ensure the resulting serialized file is valid by parsing again + obj = parse(modified) + for ent in obj.body: + if isinstance(ent, Junk): + raise Exception(f"introduced junk! {fname} {ent}") + + # it's ok, write it out + open(fname, "w").write(modified) + + +i18ndir = os.path.join(os.path.dirname(ftl_filename), "..") +langs = os.listdir(i18ndir) + +for lang in langs: + if lang == "template": + # template + ftl_path = ftl_filename + else: + # translation + ftl_path = ftl_filename.replace("templates", lang) + ftl_dir = os.path.dirname(ftl_path) + + if not os.path.exists(ftl_dir): + continue + + dupe_key(ftl_path, old_key, new_key) + +# copy file from repo into src +srcdir = os.path.join(i18ndir, "..", "..") +src_filename = os.path.join(srcdir, os.path.basename(ftl_filename)) +shutil.copy(ftl_filename, src_filename) + +print("done") diff --git a/rslib/ftl/media-check.ftl b/rslib/ftl/media-check.ftl index 9cdde6d0e..02b52e23b 100644 --- a/rslib/ftl/media-check.ftl +++ b/rslib/ftl/media-check.ftl @@ -1,5 +1,7 @@ ## Shown at the top of the media check screen +media-check-window-title = Check Media + # the number of files, and the total space used by files # that have been moved to the trash folder. eg, # "Trash folder: 3 files, 3.47MB" @@ -62,4 +64,4 @@ media-check-render-latex = Render LaTeX media-check-empty-trash = Empty Trash # button to move deleted files from the trash back into the media folder media-check-restore-trash = Restore Deleted -media-check-window-title = Check Media +media-check-check-media-action = Check Media