-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_changer.py
64 lines (48 loc) · 2.52 KB
/
term_changer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import os
import polib
import argparse
def process(input_file, source_singular, source_plural, new_singular, new_plural):
"""
Process PO file replacing source terms with new terms.
Args:
input_file (str): Path to the PO file
source_singular (str): Original singular term to be replaced (e.g., 'group' or 'person')
source_plural (str): Original plural term to be replaced (e.g., 'groups' or 'people')
new_singular (str): New singular term to use as replacement
new_plural (str): New plural term to use as replacement
"""
po = polib.pofile(input_file, encoding='utf-8')
for entry in po:
if not entry.msgstr:
entry.msgstr = entry.msgid
msgstr = entry.msgstr
has_template = False
template_content = None
# Check for template variables
template_pattern = re.compile(r'{\s*(' + re.escape(source_singular) + '|' +
re.escape(source_plural) + r')\s*}')
if template_match := template_pattern.search(msgstr):
has_template = True
template_content = template_match.group()
msgstr = msgstr.replace(template_content, '###########')
# Replace singular forms (both capitalized and lowercase)
msgstr = re.sub(rf'\b{source_singular.capitalize()}\b', new_singular.capitalize(), msgstr)
msgstr = re.sub(rf'\b{source_singular.lower()}\b', new_singular.lower(), msgstr)
# Replace plural forms (both capitalized and lowercase)
msgstr = re.sub(rf'\b{source_plural.capitalize()}\b', new_plural.capitalize(), msgstr)
msgstr = re.sub(rf'\b{source_plural.lower()}\b', new_plural.lower(), msgstr)
if has_template:
msgstr = msgstr.replace('###########', template_content)
entry.msgstr = msgstr
po.save(input_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Update terms in PO file')
parser.add_argument('path', help='Path to the PO file')
parser.add_argument('source_singular', help='Original singular term to replace (e.g., "group" or "person")')
parser.add_argument('source_plural', help='Original plural term to replace (e.g., "groups" or "people")')
parser.add_argument('new_singular', help='New singular term for replacement')
parser.add_argument('new_plural', help='New plural term for replacement')
args = parser.parse_args()
process(args.path, args.source_singular, args.source_plural,
args.new_singular, args.new_plural)