1
1
#!/usr/bin/env python3
2
2
"""
3
3
Check .po files for presence of specific language patterns in translated strings.
4
+ Optionally delete matched translations.
4
5
Languages currently checked: Russian, Polish, Ukrainian.
5
6
"""
6
7
import argparse
@@ -59,30 +60,36 @@ def should_ignore(text):
59
60
return False
60
61
61
62
62
- def find_matches_in_po (po_path , pattern ):
63
+ def find_matches_in_po (po_path , pattern , delete_matches = False ):
63
64
"""
64
65
Search for matches in translated strings of a PO file.
65
66
Skips entries with empty translations or containing ignored words.
67
+ Optionally delete matched translations.
66
68
"""
67
69
matches = []
68
70
if not pattern :
69
71
return matches
70
72
71
73
po = polib .pofile (po_path )
74
+ modified = False
75
+
72
76
for entry in po :
73
77
# Skip if there is no translation at all
74
78
if not entry .msgstr .strip ():
75
79
continue
76
80
81
+ # Skip if contains ignored word
77
82
if should_ignore (entry .msgstr ):
78
83
continue
79
84
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
81
90
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 ()
86
93
return matches
87
94
88
95
@@ -100,7 +107,12 @@ def main():
100
107
parser .add_argument (
101
108
"--no-ukrainian" ,
102
109
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." ,
104
116
)
105
117
106
118
args = parser .parse_args ()
@@ -125,7 +137,7 @@ def main():
125
137
print (f"Warning: { p } not found." )
126
138
127
139
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 ):
129
141
print (f"{ po_path } :{ linenum } : { text } " )
130
142
131
143
0 commit comments