]> git.lyx.org Git - features.git/blob - po/lyx_pot.py
Tidy up po/lyx_pot.py, I did not 'svn add' the latest version of the script
[features.git] / po / lyx_pot.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file lyx_pot.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7 #
8 # \author Bo Peng
9 #
10 # Full author contact details are available in file CREDITS
11
12 # Usage: use
13 #     lyx_pot.py -h
14 # to get usage message
15
16 # This script will extract translatable strings from input files and write
17 # to output in gettext .pot format.
18 #
19 import sys, os, re, getopt
20
21 def relativePath(path, base):
22     '''return relative path from top source dir'''
23     # full pathname of path
24     path1 = os.path.normpath(os.path.realpath(path)).split(os.sep)
25     path2 = os.path.normpath(os.path.realpath(base)).split(os.sep)
26     if path1[:len(path2)] != path2:
27         print "Path %s is not under top source directory" % path
28     return os.path.join(*path1[len(path2):])
29
30
31 def ui_l10n(input_files, output, base):
32     '''Generate pot file from lib/ui/*'''
33     output = open(output, 'w')
34     Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"')
35     Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
36     Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
37     for src in input_files:
38         input = open(src)
39         for lineno, line in enumerate(input.readlines()):
40             if Submenu.match(line):
41                 (string,) = Submenu.match(line).groups()
42                 string = string.replace('_', ' ')
43             elif Toolbar.match(line):
44                 (string,) = Toolbar.match(line).groups()
45             elif Item.match(line):
46                 (string,) = Item.match(line).groups()
47             else:
48                 continue
49             string = string.replace('\\', '\\\\').replace('"', '')
50             if string != "":
51                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
52                     (relativePath(src, base), lineno+1, string)
53         input.close()
54     output.close()
55
56
57 def layouts_l10n(input_files, output, base):
58     '''Generate pot file from lib/layouts/*.layout and *.inc'''
59     output = open(output, 'w')
60     Style = re.compile(r'^Style\s+(.*)')
61     # include ???LabelString???, but exclude comment lines
62     LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
63     GuiName = re.compile(r'\s*GuiName\s+(.*)')
64     ListName = re.compile(r'\s*ListName\s+(.*)')
65     for src in input_files:
66         input = open(src)
67         for lineno, line in enumerate(input.readlines()):
68             if Style.match(line):
69                 (string,) = Style.match(line).groups()
70                 string = string.replace('_', ' ')
71             elif LabelString.match(line):
72                 (string,) = LabelString.match(line).groups()
73             elif GuiName.match(line):
74                 (string,) = GuiName.match(line).groups()
75             elif ListName.match(line):
76                 (string,) = ListName.match(line).groups()
77             else:
78                 continue
79             string = string.replace('\\', '\\\\').replace('"', '')
80             if string != "":
81                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
82                     (relativePath(src, base), lineno+1, string)
83         input.close()
84     output.close()
85
86
87 def qt4_l10n(input_files, output, base):
88     '''Generate pot file from src/frontends/qt4/ui/*.ui'''
89     output = open(output, 'w')
90     pat = re.compile(r'\s*<string>(.*)</string>')
91     prop = re.compile(r'\s*<property.*name.*=.*shortcut')
92     for src in input_files:
93         input = open(src)
94         skipNextLine = False
95         for lineno, line in enumerate(input.readlines()):
96             # skip the line after <property name=shortcut>
97             if skipNextLine:
98                 skipNextLine = False
99                 continue
100             if prop.match(line):
101                 skipNextLine = True
102                 continue
103             # get lines that match <string>...</string>
104             if pat.match(line):
105                 (string,) = pat.match(line).groups()
106                 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('"', r'\"')
107                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
108                     (relativePath(src, base), lineno+1, string) 
109         input.close()
110     output.close()
111
112
113 def languages_l10n(input_files, output, base):
114     '''Generate pot file from lib/language'''
115     output = open(output, 'w')
116     # assuming only one language file
117     input = open(input_files[0])
118     for lineno, line in enumerate(input.readlines()):
119         if line[0] == '#':
120             continue
121         items = line.split('"')
122         # empty lines?
123         if len(items) < 3:
124             continue
125         # From:
126         #   afrikaans   afrikaans       "Afrikaans"     false  iso8859-15 af_ZA  ""
127         # To:
128         #   #: lib/languages:2
129         #   msgid "Afrikaans"
130         #   msgstr ""
131         # I do not care extra "s like "af_ZA"
132         print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % (relativePath(input_files[0], base), lineno+1, items[1])
133     input.close()
134     output.close()
135
136
137 Usage = '''
138 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
139
140 where 
141     --base:
142         path to the top source directory. default to '.'
143     --output:
144         output pot file, default to './lyx.pot'
145     --input_type can be
146         ui: lib/ui/*
147         layouts: lib/layouts/*
148         qt4: qt4 ui files
149         languages: file lib/languages
150 '''
151
152 if __name__ == '__main__':
153     input_type = None
154     output = 'lyx.pot'
155     base = '.'
156     #
157     optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
158         ['help', 'type=', 'output=', 'base='])
159     for (opt, value) in optlist:
160         if opt in ['-h', '--help']:
161             print Usage
162             sys.exit(0)
163         elif opt in ['-o', '--output']:
164             output = value
165         elif opt in ['-b', '--base']:
166             base = value
167         elif opt in ['-t', '--type']:
168             input_type = value
169     if input_type not in ['ui', 'layouts', 'qt4', 'languages'] or output is None:
170         print 'Wrong input type or output filename.'
171         sys.exit(1)
172     if input_type == 'ui':
173         ui_l10n(args, output, base)
174     elif input_type == 'layouts':
175         layouts_l10n(args, output, base)
176     elif input_type == 'qt4':
177         qt4_l10n(args, output, base)
178     else:
179         languages_l10n(args, output, base)
180