po-checktrans.py 3.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/python
#
# This quick hack gives translation statistics (from the core translation
# files).
#
# Copyright (C) 2001, Cyrille Chepelov <chepelov@calixo.net>
#
# This quick hack is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This quick hack is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
19 20
# along with this quick hack; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 22 23 24
#

import string,os,sys,math

25 26 27
collen = 1

def NoneStr(n):
28
    if n is None: return (" " * maxlanglen)
29 30
    return n

31
def slurp_po(fname):
32 33
    """returns a tuple (language_name,ids,translations) of what has
    been translated."""
34 35 36 37 38 39 40 41
    base,ext = os.path.splitext(os.path.basename(fname))
    language_name = base

    idents = 0
    translations = 0
    
    st = ""
    id = ""
42 43 44 45

    fuzzy = 0
    prev_fuzzy = 0
    fuzzies = 0
46 47 48 49 50 51
    
    fd = open(fname,"r")
    while 1:
        s = fd.readline()
        if not s: break

52 53 54 55
        if s[0] == '#':
            if s.find('fuzzy') >= 0:
                fuzzy = 1
                
56
        s = string.strip(string.split(s,'#')[0])
57
        if not s: continue # empty or comment line.
58 59 60 61 62
        
        sl = string.split(s)
        if sl[0] == "msgid":
            #print "id",id,"st",st
            if st:
63 64
                if prev_fuzzy:
                    fuzzies = fuzzies + 1
65 66 67 68 69 70 71 72 73 74 75
                translations = translations + 1
            id = sl[0]
            st = ""
            del sl[0]
        elif sl[0] == "msgstr":
            #print "id",id,"st",st
            if st:
                idents = idents + 1
            id = sl[0]
            st = ""
            del sl[0]
76 77
            prev_fuzzy = fuzzy
            fuzzy = 0
78 79 80 81 82
            
        for k in sl:
            if k != '""':
                st = st + k
    #print "translations:",translations,"idents:",idents
83
    return (language_name, idents, translations,fuzzies)
84

85 86
if len(sys.argv)<3:
    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 90 91 92 93 94 95 96
    print " <lang.po>: file name of the translation to check"
    sys.exit(1)

def maxlen(a,b):
    if len(a) > len(b):
        return a
    return b

97 98
(t,idents,n,f) = slurp_po(sys.argv[1])
del t,n,f
99 100

translations = map(slurp_po,sys.argv[2:])
101 102
trans = map(lambda (l,i,t,f),ti=idents:
            "%s:%3d%%(%d/%d/%d)%s"%(l,
103
                                 100*float(t)/idents,
104
                                 t,f,idents,
105
                                 "*" * (idents != i)),
106
            translations)
107 108
maxlanglen = len(reduce(maxlen,trans,""))
trans = map(lambda s,mll=maxlanglen: string.ljust(s,mll),trans)
109

110
collen = maxlanglen + len("  ")
111

112
numcols = int(79 / collen)
113 114
ltnc = (len(trans) / numcols) + (len(trans) % numcols)

115 116 117 118 119 120 121
cols = []
while trans:
    c,trans = trans[:ltnc],trans[ltnc:]
    cols.append(c)

lines = apply(map,tuple([None]+cols))

122 123
result = string.join(map(lambda l:string.join(map(NoneStr,list(l)),"  "),
                         lines),
124 125 126
                     "\n")
print result