~qcumber-some/widelands/gamekeeper_improvement

« back to all changes in this revision

Viewing changes to utils/confgettext.py

  • Committer: sirver
  • Date: 2005-11-07 20:42:37 UTC
  • Revision ID: git-v1:4389943402547e2a672839ffe0633c0bfb61492d
- added 2 scripts needed for localisation
- added 1 script to replace a string in a file ( which is need fairly often )


git-svn-id: https://widelands.svn.sourceforge.net/svnroot/widelands/trunk@949 37b2a8de-5219-0410-9f54-a31bc463ab9c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -tt
 
2
 
 
3
"""This proggy parses given files for occurences of translatable strings
 
4
if some are found they are output in a xgettext style file"""
 
5
 
 
6
import sys
 
7
import os
 
8
 
 
9
head = """# SOME DESCRIPTIVE TITLE.
 
10
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
 
11
# This file is distributed under the same license as the PACKAGE package.
 
12
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
 
13
#
 
14
msgid ""
 
15
msgstr ""
 
16
"Project-Id-Version: PACKAGE VERSION\\n"
 
17
"Report-Msgid-Bugs-To: \\n"
 
18
"POT-Creation-Date: 2005-11-05 22:37+0100\\n"
 
19
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
 
20
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
 
21
"Language-Team: LANGUAGE <LL@li.org>\\n"
 
22
"MIME-Version: 1.0\\n"
 
23
"Content-Type: text/plain; charset=UTF-8\\n"
 
24
"Content-Transfer-Encoding: 8bit\\n"
 
25
 
 
26
"""
 
27
 
 
28
class occurences: 
 
29
    def __init__( self, file, line ):
 
30
        self.line = line
 
31
        self.file = file
 
32
        
 
33
class trans_string:
 
34
    def __init__( self ):
 
35
        self.occurences = []
 
36
        self.str = ""
 
37
 
 
38
def is_multiline( str ):
 
39
    l = firstl( str, '"\'');
 
40
    if( l == -1 ):
 
41
        return False
 
42
    r =  firstr( str, '"\'') 
 
43
    if( r == l ):
 
44
        return True
 
45
 
 
46
def firstl( str, what ):
 
47
    for c in what:
 
48
        index = str.find( c )
 
49
        if( index != -1 ):
 
50
            return index
 
51
    return -1
 
52
 
 
53
def firstr( str, what ):
 
54
    for c in what:
 
55
        index = str.rfind( c )
 
56
        if( index != -1 ):
 
57
            return index
 
58
    return -1
 
59
 
 
60
def append_string( known_strings, array, string ):
 
61
    if known_strings.has_key( string.str ):
 
62
        i = known_strings[string.str]
 
63
        array[i].occurences += string.occurences 
 
64
    else:
 
65
        array.append( string )
 
66
        known_strings[string.str] = len( array ) - 1
 
67
    
 
68
def parse_conf( files ):
 
69
    translatable_strings = []
 
70
    known_strings = {}
 
71
    curstr = 0
 
72
    multiline = 0
 
73
    for file in files:
 
74
        lines = open( file, 'r' ).readlines();
 
75
        for i in range(0,len(lines)):
 
76
            line = lines[i].rstrip('\n')
 
77
            linenr = i+1
 
78
            
 
79
            if multiline:
 
80
                rindex = firstr(line, '"\'')
 
81
                if rindex == -1:
 
82
                    curstr.str += line
 
83
                    continue
 
84
                else:
 
85
                    curstr.str += line[:rindex]
 
86
                    append_string( known_strings, translatable_strings, curstr )
 
87
                    multiline = False
 
88
                    continue
 
89
 
 
90
            index = line.find( "=_" )
 
91
            if( index > 0 ):
 
92
                curstr = trans_string()
 
93
                curstr.occurences.append( occurences( file, linenr )) 
 
94
                restline = line[index+2:]
 
95
                multiline = is_multiline( restline );
 
96
                if multiline: # means exactly one "
 
97
                    index = firstl( restline, '"\'')
 
98
                    restline = restline[index+1:]
 
99
                    curstr.str += restline
 
100
                    continue
 
101
                # Is not multiline
 
102
                # If there are ' or " its easy
 
103
                l = firstl( restline, '"\'')
 
104
                r = firstr( restline, '"\'')
 
105
                if( l != -1 and r != -1 ):
 
106
                    restline = restline[l+1:]
 
107
                    r = firstr( restline, '"\'')
 
108
                    restline = restline[:r] 
 
109
                else:
 
110
                    # Remove comments
 
111
                    rindex = max( restline.rfind('#'), restline.rfind('//'))
 
112
                    if rindex != -1:
 
113
                        restline = restline[:rindex]
 
114
                    # And strip
 
115
                    restline = restline.strip()
 
116
                curstr.str = restline
 
117
                append_string( known_strings, translatable_strings, curstr )
 
118
 
 
119
    translatable_strings.sort( lambda str1,str2: cmp(str1.str,str2.str) ) 
 
120
 
 
121
    retval = head
 
122
    for i in translatable_strings:
 
123
        for occ in i.occurences:
 
124
            retval += "#: %s:%i\n" % ( occ.file, occ.line )
 
125
        retval += 'msgid "%s"\n' % i.str
 
126
        retval += 'msgstr ""\n\n'
 
127
    return retval 
 
128
 
 
129
 
 
130
#: ../src/ui/ui_fs_menus/fullscreen_menu_main.cc:41
 
131
#msgid "Single Player"
 
132
#msgstr ""
 
133
 
 
134
if __name__ == "__main__":
 
135
    if( sys.argv < 2 ):
 
136
        print "Usage %s <conf file> [ <conf file> ... ]" % sys.argv[0]
 
137
 
 
138
    print parse_conf( sys.argv[1:] )