Skip to content

Instantly share code, notes, and snippets.

@tkusano
Last active November 2, 2024 15:02
Show Gist options
  • Save tkusano/b86bce1b825a154056bd92bb6fd438f8 to your computer and use it in GitHub Desktop.
Save tkusano/b86bce1b825a154056bd92bb6fd438f8 to your computer and use it in GitHub Desktop.
update messages.po for bluesky-social/social-app
#! /usr/bin/env python3
from pathlib import Path
import re
import sys
RE_MSG = re.compile("(\\#\\~ )?(msgctxt|msgid) \"(.*)\"")
def extract_idx(block: str) -> tuple[bool, str]:
lines_in_block = block.split(sep="\n")
msgctxt = ''
msgid = ''
is_obsoleted : bool = False
for line in lines_in_block:
res = RE_MSG.match(line)
if res is not None:
if res[1] is not None:
is_obsoleted = True
if res[2] == "msgctxt":
msgctxt = res[3]
elif res[2] == "msgid":
msgid = res[3]
return (is_obsoleted, msgid + "|" + msgctxt)
def print_as_obsoleted(block: str) -> None:
lines_in_block = block.split(sep="\n")
for line in lines_in_block:
if line.startswith("#"):
print(line)
else:
print("#~ " + line)
def do_process(old_file: str, new_file: str) -> None:
old_content = Path(old_file).read_text().rstrip()
old_blocks = old_content.split(sep="\n\n")
header = old_blocks.pop(0)
old_block_idx = dict()
for new_block in old_blocks:
(is_obsoleted, msgkey) = extract_idx(new_block)
old_block_idx[msgkey] = (is_obsoleted, new_block)
new_content = Path(new_file).read_text().rstrip()
new_blocks = new_content.split(sep="\n\n")
new_blocks.pop(0)
print(header)
for new_block in new_blocks:
(is_obsoleted, msgkey) = extract_idx(new_block)
if msgkey in old_block_idx:
print("")
(_, old_block) = old_block_idx[msgkey]
if not is_obsoleted:
print(old_block)
else:
print_as_obsoleted(old_block)
else:
print("")
print(new_block)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("usage: {} old-messages.po intl-extracted-messages.po".format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
do_process(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment