[9ef3b1]: Misc / check_po.py  Maximize  Restore  History

Download this file

182 lines (154 with data), 4.2 kB

  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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# -*- coding: utf8 -*-
__version__ = '$Id$'
# author: Michał Niklas, michal.niklas@wp.pl
USAGE = """Ensure translation follows the original (leading blanks, quotes etc.)
Usage: check_po.py [file_name]
"""
# translated Polish texts
"""
#. Resource IDs: (191)
msgid " Field is too long."
msgstr " Pole jest zbyt długie."
"""
import sys
import glob
import re
import string
RE_START = re.compile('^([^\w&]+)\w+')
RE_STOP = re.compile('(\W)$')
def check_re(rx, s1, s2, line_warns):
"""checks if regular expresion match from s1 is equal to match from s2"""
rx1 = rx.search(s1)
if rx1:
rx2 = rx.search(s2)
if not rx2:
line_warns.append('re1(%s)' % rx.pattern)
return 1
if rx1.group(1) != rx2.group(1):
line_warns.append('re2(%s)' % rx.pattern)
return 1
return 0
def check_count(msgid, msgstr, s, line_warns):
"""check if count of 's' is the same"""
r = 0
if msgid.count(s) != msgstr.count(s):
line_warns.append('cnt(%s)' % (s))
r += 1
return r
def check_quotes(msgid, msgstr, line_warns):
"""check if quote count is the same"""
return check_count(msgid, msgstr, '"', line_warns)
def get_patterns(s):
"""get %s patterns from s"""
patterns = []
in_pattern = 0
patt = ''
pattern_end = string.letters + string.whitespace
for c in s:
if in_pattern:
if c in pattern_end:
patt += c
patterns.append(patt)
patt = ''
in_pattern = 0
continue
if c == '%':
in_pattern = 1
if in_pattern:
patt += c
if patt:
patterns.append(patt)
return patterns
def check_cpatterns(msgid, msgstr, line_warns):
"""check if %s, %d etc patterns from msgid matches patterns from msgstr"""
patts1 = get_patterns(msgid)
patts2 = get_patterns(msgstr)
if len(patts1) != len(patts2):
line_warns.append('cpatterns1')
return 1
patts1.sort()
patts2.sort()
if patts1 != patts2:
line_warns.append('cpatterns2')
return 1
return 0
def check_accelerator(msgid, msgstr, line_warns):
"""check if accelrator &x is the same in both strings"""
msgid = msgid.lower()
msgstr = msgstr.lower()
p1 = msgid.find('&')
if p1 >= 0:
a1 = msgid[p1:p1 + 2]
p2 = msgstr.find('&')
if p2 < 0 and len(a1) > 1:
if a1[-1] in msgstr:
# warn if there is no accelerator in translated version
# but "accelerated" letter is available
line_warns.append('acc1')
return 1
else:
a2 = msgstr[p2:p2 + 2]
if a1 != a2:
#line_warns.append('acc2')
return 0 # ok they can be different
return 0
def check_str(msgid, msgstr, line_warns):
"""checks is msgstr had the same leading blanks, etc"""
warn_cnt = 0
if msgid and msgstr:
warn_cnt += check_re(RE_START, msgid, msgstr, line_warns)
warn_cnt += check_re(RE_STOP, msgid, msgstr, line_warns)
warn_cnt += check_quotes(msgid, msgstr, line_warns)
warn_cnt += check_cpatterns(msgid, msgstr, line_warns)
warn_cnt += check_accelerator(msgid, msgstr, line_warns)
for t in ('\\r', '\\n', '\\t', '|', ' ', '\\\"', 'ÂŤ', 'Âť'):
warn_cnt += check_count(msgid, msgstr, t, line_warns)
for c in string.digits:
warn_cnt += check_count(msgid, msgstr, c, line_warns)
return warn_cnt
def check_po(fn):
"""checks .po file"""
warn_cnt = 0
item_cnt = 0
empty_item_cnt = 0
f = open(fn, 'r')
lines = f.readlines()
f.close()
msgid = ''
msgstr = ''
line_no = 0
for line in lines:
line_no += 1
line = line.strip()
if line.startswith('msgid '):
item_cnt += 1
msgid = line[6:]
msgstr = ''
if line.startswith('msgstr '):
msgstr = line[7:]
msgid = msgid.strip('"')
msgstr = msgstr.strip('"')
if not msgstr:
empty_item_cnt += 1
else:
line_warns = []
wc = check_str(msgid, msgstr, line_warns)
if wc:
print('%s:%d:%s: [%s] [%s]' % (fn, line_no, ','.join(line_warns), msgid, msgstr))
warn_cnt += wc
msgstr = ''
print('%s: items: %d; empty items: %d; warning cnt: %d' % (fn, item_cnt, empty_item_cnt, warn_cnt))
if '--version' in sys.argv:
print(__version__)
elif '--help' in sys.argv:
print(USAGE)
else:
ac = 0
for fn in sys.argv[1:]:
if not fn.startswith('-'):
check_po(fn)
ac += 1
if ac < 1:
for fn in glob.glob('pwsafe_*.po'):
check_po(fn)

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks