2006-12-05 David Lodge <dave@cirt.net>
[dia.git] / po-checktrans.py
blob20d466d9b0c0bc63cc30c95d915a4bd35d56775a
1 #!/usr/bin/python
3 # This quick hack gives translation statistics (from the core translation
4 # files).
6 # Copyright (C) 2001, Cyrille Chepelov <chepelov@calixo.net>
8 # This quick hack is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This quick hack is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this quick hack; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 import string,os,sys,math
25 collen = 1
27 def NoneStr(n):
28 if n is None: return (" " * maxlanglen)
29 return n
31 def slurp_po(fname):
32 """returns a tuple (language_name,ids,translations) of what has
33 been translated."""
34 base,ext = os.path.splitext(os.path.basename(fname))
35 language_name = base
37 idents = 0
38 translations = 0
40 st = ""
41 id = ""
43 fuzzy = 0
44 prev_fuzzy = 0
45 fuzzies = 0
47 fd = open(fname,"r")
48 while 1:
49 s = fd.readline()
50 if not s: break
52 if s[0] == '#':
53 if s.find('fuzzy') >= 0:
54 fuzzy = 1
56 s = string.strip(string.split(s,'#')[0])
57 if not s: continue # empty or comment line.
59 sl = string.split(s)
60 if sl[0] == "msgid":
61 #print "id",id,"st",st
62 if st:
63 if prev_fuzzy:
64 fuzzies = fuzzies + 1
65 translations = translations + 1
66 id = sl[0]
67 st = ""
68 del sl[0]
69 elif sl[0] == "msgstr":
70 #print "id",id,"st",st
71 if st:
72 idents = idents + 1
73 id = sl[0]
74 st = ""
75 del sl[0]
76 prev_fuzzy = fuzzy
77 fuzzy = 0
79 for k in sl:
80 if k != '""':
81 st = st + k
82 #print "translations:",translations,"idents:",idents
83 return (language_name, idents, translations,fuzzies)
85 if len(sys.argv)<3:
86 print "Usage: %s <package.pot> <lang.po> ..." % sys.argv[0]
87 print
88 print " <package.pot>: file name of the identifier reference to check."
89 print " <lang.po>: file name of the translation to check"
90 sys.exit(1)
92 def maxlen(a,b):
93 if len(a) > len(b):
94 return a
95 return b
97 (t,idents,n,f) = slurp_po(sys.argv[1])
98 del t,n,f
100 translations = map(slurp_po,sys.argv[2:])
101 trans = map(lambda (l,i,t,f),ti=idents:
102 "%s:%3d%%(%d/%d/%d)%s"%(l,
103 100*float(t)/idents,
104 t,f,idents,
105 "*" * (idents != i)),
106 translations)
107 maxlanglen = len(reduce(maxlen,trans,""))
108 trans = map(lambda s,mll=maxlanglen: string.ljust(s,mll),trans)
110 collen = maxlanglen + len(" ")
112 numcols = int(79 / collen)
113 ltnc = (len(trans) / numcols) + (len(trans) % numcols)
115 cols = []
116 while trans:
117 c,trans = trans[:ltnc],trans[ltnc:]
118 cols.append(c)
120 lines = apply(map,tuple([None]+cols))
122 result = string.join(map(lambda l:string.join(map(NoneStr,list(l))," "),
123 lines),
124 "\n")
125 print result