Skip to content

Commit cc01eda

Browse files
committed
Add delete command
1 parent 6f07e31 commit cc01eda

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

scripts/check_langs_in_po.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
"""
33
Check .po files for presence of specific language patterns in translated strings.
4+
Optionally delete matched translations.
45
Languages currently checked: Russian, Polish, Ukrainian.
56
"""
67
import argparse
@@ -59,30 +60,36 @@ def should_ignore(text):
5960
return False
6061

6162

62-
def find_matches_in_po(po_path, pattern):
63+
def find_matches_in_po(po_path, pattern, delete_matches=False):
6364
"""
6465
Search for matches in translated strings of a PO file.
6566
Skips entries with empty translations or containing ignored words.
67+
Optionally delete matched translations.
6668
"""
6769
matches = []
6870
if not pattern:
6971
return matches
7072

7173
po = polib.pofile(po_path)
74+
modified = False
75+
7276
for entry in po:
7377
# Skip if there is no translation at all
7478
if not entry.msgstr.strip():
7579
continue
7680

81+
# Skip if contains ignored word
7782
if should_ignore(entry.msgstr):
7883
continue
7984

80-
texts = [entry.msgstr]
85+
if pattern.search(entry.msgstr):
86+
matches.append((po_path, entry.linenum, entry.msgstr))
87+
if delete_matches:
88+
entry.msgstr = ""
89+
modified = True
8190

82-
for text in texts:
83-
if text and pattern.search(text):
84-
matches.append((po_path, entry.linenum, text))
85-
break # avoid multiple reports for the same entry
91+
if delete_matches and modified:
92+
po.save()
8693
return matches
8794

8895

@@ -100,7 +107,12 @@ def main():
100107
parser.add_argument(
101108
"--no-ukrainian",
102109
action="store_true",
103-
help="Disable Ukrainian pattern checking."
110+
help="Disable Ukrainian pattern checking.",
111+
)
112+
parser.add_argument(
113+
"--delete-matches",
114+
action="store_true",
115+
help="Delete msgstr of matched entries.",
104116
)
105117

106118
args = parser.parse_args()
@@ -125,7 +137,7 @@ def main():
125137
print(f"Warning: {p} not found.")
126138

127139
for path in paths:
128-
for po_path, linenum, text in find_matches_in_po(path, pattern):
140+
for po_path, linenum, text in find_matches_in_po(path, pattern, args.delete_matches):
129141
print(f"{po_path}:{linenum}: {text}")
130142

131143

0 commit comments

Comments
 (0)