-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpo2po.py
executable file
·66 lines (56 loc) · 1.92 KB
/
po2po.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
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2015,16 - Walter Bender <walter@sugarlabs.org>
# This is for the .po file.
# How to use: script.py file-old.po file-new.po
# This script will mine an old po file for any untranslated strings in a new po file.
import os
import re
import sys
import codecs
def mine_from_po_to_po(oldfilename, newfilename, ignore_case):
oldfd = codecs.open(oldfilename, "r", "UTF-8")
newfd = codecs.open(newfilename, "r", "UTF-8")
output = codecs.open('tmp.po', "w", "UTF-8")
olddict = {}
for line in oldfd:
if line[0:5] == 'msgid':
key = line[7:-2]
if line[0:6] == 'msgstr':
value = line[8:-2]
if value == '':
continue;
if ignore_case:
olddict[key.lower()] = value;
else:
olddict[key] = value;
newdict = {}
for line in newfd:
msgstr_flag = False
if line[0:5] == 'msgid':
key = line[7:-2]
if line[0:6] == 'msgstr':
value = line[8:-2]
msgstr_flag = True
if ignore_case:
if msgstr_flag and value == '' and key.lower() in olddict:
print('found a match for %s' % (key))
output.write('msgstr "%s"\n' % (olddict[key.lower()]))
else:
output.write(line);
else:
if msgstr_flag and value == '' and key in olddict:
output.write('msgstr "%s"\n' % (olddict[key]))
print('found a match for %s' % (key))
else:
output.write(line);
output.close()
if __name__ == '__main__':
if len(sys.argv) < 3:
print("po2po source target [ignorecase]")
elif len(sys.argv) == 4:
ini = mine_from_po_to_po(sys.argv[1], sys.argv[2], True)
print("Finished")
else:
ini = mine_from_po_to_po(sys.argv[1], sys.argv[2], False)
print("Finished")