diff --git a/qt/po/scripts/extract-po-string.py b/qt/po/scripts/extract-po-string.py index a3b6a322e..93e66ec91 100644 --- a/qt/po/scripts/extract-po-string.py +++ b/qt/po/scripts/extract-po-string.py @@ -10,13 +10,13 @@ from fluent.syntax.ast import Message, TextElement, Identifier, Pattern, Junk # extract a translated string from strings.json and insert it into ftl # eg: -# $ python extract-po-string.py /path/to/templates/media-check.ftl delete-unused "Delete Unused Media" "" -# $ python extract-po-string.py /path/to/templates/media-check.ftl delete-unused "%(a)s %(b)s" "%(a)s=$val1,%(b)s=$val2" +# $ python extract-po-string.py strings.json /path/to/templates/media-check.ftl delete-unused "Delete Unused Media" "" +# $ python extract-po-string.py strings.json /path/to/templates/media-check.ftl delete-unused "%(a)s %(b)s" "%(a)s=$val1,%(b)s=$val2" # # NOTE: the English text is written into the templates folder of the repo, so must be copied # into Anki's source tree -ftl_filename, key, msgid_substring, repls = sys.argv[1:] +json_filename, ftl_filename, key, msgid_substring, repls = sys.argv[1:] # split up replacements replacements = [] @@ -29,7 +29,7 @@ for repl in repls.split(","): prefix = os.path.splitext(os.path.basename(ftl_filename))[0] key = f"{prefix}-{key}" -strings = json.load(open("strings.json", "r")) +strings = json.load(open(json_filename, "r")) msgids = [] if msgid_substring in strings["en"]: @@ -51,18 +51,21 @@ else: print(f"* {c}: {id}") msgid = msgids[int(input("number to use? "))] + def transform_entry(entry): if isinstance(entry, str): - return(transform_string(entry)) + return transform_string(entry) else: return [transform_string(e) for e in entry] + def transform_string(msg): for (old, new) in replacements: msg = msg.replace(old, f"{{{new}}}") # strip leading/trailing whitespace return msg.strip() + to_insert = [] for lang in strings.keys(): entry = strings[lang].get(msgid) @@ -114,7 +117,7 @@ def add_simple_message(fname, key, message): modified = serialize(obj, with_junk=True) # escape leading dots - modified = re.sub(r"(?ms)^( +)\.", "\\1{\".\"}", modified) + modified = re.sub(r"(?ms)^( +)\.", '\\1{"."}', modified) # ensure the resulting serialized file is valid by parsing again obj = parse(modified)