Skip to content

Commit

Permalink
po scripts: Output src references for JSON strings
Browse files Browse the repository at this point in the history
Translators can now see where the strings originate, with both the filename and a JSONPath.
  • Loading branch information
past-due committed Mar 9, 2020
1 parent ae493d0 commit 32eb4c7
Show file tree
Hide file tree
Showing 4 changed files with 6,197 additions and 1,502 deletions.
57 changes: 57 additions & 0 deletions po/aggregateParsedJson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import fileinput
import re

lines_seen = dict() # holds lines already seen (minus trailing comments), mapped to a list of source references
output_lines = list()

# See: https://stackoverflow.com/a/241506
def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)

def src_ref_comment_extractor(text, remove_prefix="// SRC:"):
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
comments = list()
for m in re.finditer(pattern, text):
if m.group(0).startswith("//"):
# found a comment
s = m.group(0).rstrip('\n')
s = s[len(remove_prefix):] if s.startswith(remove_prefix) else s
comments.append(s.strip())
return comments

with fileinput.input() as f_input:
for line in f_input:
line = line.rstrip('\n')
line_without_comments = comment_remover(line)
if line_without_comments not in lines_seen: # not a duplicate
lines_seen[line_without_comments] = src_ref_comment_extractor(line)
output_lines.append(line_without_comments)
else:
# store the additional src refs for the line
lines_seen[line_without_comments].extend(src_ref_comment_extractor(line))

MAX_COMMENT_LINES = 4
for line in sorted(output_lines, key=str.casefold):
src_ref_limit = MAX_COMMENT_LINES
sorted_src_refs = sorted(lines_seen[line], key=str.casefold)
if len(sorted_src_refs) > src_ref_limit:
src_ref_limit -= 1
for idx, src_ref in zip(range(src_ref_limit), sorted_src_refs):
if idx == 0:
print("// TRANSLATORS: ")
print("// " + src_ref)
if len(sorted_src_refs) > src_ref_limit:
print("// ... + " + str(len(sorted_src_refs) - src_ref_limit) + " refs")
print(line)

0 comments on commit 32eb4c7

Please sign in to comment.