]> git.lyx.org Git - lyx.git/blob - po/lyx_pot.py
* cs.po
[lyx.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     path3 = os.path.join(*path1[len(path2):]);
29     # replace all \ by / such that we get the same comments on Windows and *nix
30     path3 = path3.replace('\\', '/')
31     return path3
32
33
34 def writeString(outfile, infile, basefile, lineno, string):
35     string = string.replace('\\', '\\\\').replace('"', '')
36     if string == "":
37         return
38     print >> outfile, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
39         (relativePath(infile, basefile), lineno, string)
40
41
42 def ui_l10n(input_files, output, base):
43     '''Generate pot file from lib/ui/*'''
44     output = open(output, 'w')
45     Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"')
46     Popupmenu = re.compile(r'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"')
47     Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
48     Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
49     TableInsert = re.compile(r'[^#]*TableInsert\s+"([^"]*)"')
50     for src in input_files:
51         input = open(src)
52         for lineno, line in enumerate(input.readlines()):
53             if Submenu.match(line):
54                 (string,) = Submenu.match(line).groups()
55                 string = string.replace('_', ' ')
56             elif Popupmenu.match(line):
57                 (string,) = Popupmenu.match(line).groups()
58             elif Toolbar.match(line):
59                 (string,) = Toolbar.match(line).groups()
60             elif Item.match(line):
61                 (string,) = Item.match(line).groups()
62             elif TableInsert.match(line):
63                 (string,) = TableInsert.match(line).groups()
64             else:
65                 continue
66             string = string.replace('"', '')
67             if string != "":
68                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
69                     (relativePath(src, base), lineno+1, string)
70         input.close()
71     output.close()
72
73
74 def layouts_l10n(input_files, output, base):
75     '''Generate pot file from lib/layouts/*.{layout,inc,module}'''
76     out = open(output, 'w')
77     Style = re.compile(r'^Style\s+(.*)')
78     # include ???LabelString???, but exclude comment lines
79     LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
80     GuiName = re.compile(r'\s*GuiName\s+(.*)')
81     ListName = re.compile(r'\s*ListName\s+(.*)')
82     CategoryName = re.compile(r'\s*Category\s+(.*)')
83     NameRE = re.compile(r'DeclareLyXModule.*{(.*)}')
84     DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
85     DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
86
87     for src in input_files:
88         readingDescription = False
89         descStartLine = -1
90         descLines = []
91         lineno = 0
92         for line in open(src).readlines():
93             lineno += 1
94             if readingDescription:
95                 res = DescEnd.search(line)
96                 if res != None:
97                     readingDescription = False
98                     desc = " ".join(descLines)
99                     writeString(out, src, base, lineno + 1, desc)
100                     continue
101                 descLines.append(line[1:].strip())
102                 continue
103             res = DescBegin.search(line)
104             if res != None:
105                 readingDescription = True
106                 descStartLine = lineno
107                 continue
108             res = NameRE.search(line)
109             if res != None:
110                 string = res.group(1)
111                 string = string.replace('\\', '\\\\').replace('"', '')
112                 if string != "":
113                     print >> out, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
114                         (relativePath(src, base), lineno + 1, string)
115                 continue
116             res = Style.search(line)
117             if res != None:
118                 string = res.group(1)
119                 string = string.replace('_', ' ')
120                 writeString(out, src, base, lineno, string)
121                 continue
122             res = LabelString.search(line)
123             if res != None:
124                 string = res.group(1)
125                 writeString(out, src, base, lineno, string)
126                 continue
127             res = GuiName.search(line)
128             if res != None:
129                 string = res.group(1)
130                 writeString(out, src, base, lineno, string)
131                 continue
132             res = CategoryName.search(line)
133             if res != None:
134                 string = res.group(1)
135                 writeString(out, src, base, lineno, string)
136                 continue
137             res = ListName.search(line)
138             if res != None:
139                 string = res.group(1)
140                 writeString(out, src, base, lineno, string)
141                 continue
142     out.close()
143
144
145 def qt4_l10n(input_files, output, base):
146     '''Generate pot file from src/frontends/qt4/ui/*.ui'''
147     output = open(output, 'w')
148     pat = re.compile(r'\s*<string>(.*)</string>')
149     prop = re.compile(r'\s*<property.*name.*=.*shortcut')
150     for src in input_files:
151         input = open(src)
152         skipNextLine = False
153         for lineno, line in enumerate(input.readlines()):
154             # skip the line after <property name=shortcut>
155             if skipNextLine:
156                 skipNextLine = False
157                 continue
158             if prop.match(line):
159                 skipNextLine = True
160                 continue
161             # get lines that match <string>...</string>
162             if pat.match(line):
163                 (string,) = pat.match(line).groups()
164                 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>')
165                 string = string.replace('\\', '\\\\').replace('"', r'\"')
166                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
167                     (relativePath(src, base), lineno+1, string) 
168         input.close()
169     output.close()
170
171
172 def languages_l10n(input_files, output, base):
173     '''Generate pot file from lib/language'''
174     output = open(output, 'w')
175     # assuming only one language file
176     reg = re.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\)]+)"\s+(true|false)\s+[\w-]+\s+\w+\s+"[^"]*"')
177     input = open(input_files[0])
178     for lineno, line in enumerate(input.readlines()):
179         if line[0] == '#':
180             continue
181         # From:
182         #   afrikaans   afrikaans       "Afrikaans"     false  iso8859-15 af_ZA  ""
183         # To:
184         #   #: lib/languages:2
185         #   msgid "Afrikaans"
186         #   msgstr ""
187         if reg.match(line):
188             print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
189                 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
190         else:
191             print "Error: Unable to handle line:"
192             print line
193             # No need to abort if the parsing fails (e.g. "ignore" language has no encoding)
194             # sys.exit(1)
195     input.close()
196     output.close()
197
198
199 def external_l10n(input_files, output, base):
200     '''Generate pot file from lib/external_templates'''
201     output = open(output, 'w')
202     Template = re.compile(r'^Template\s+(.*)')
203     GuiName = re.compile(r'\s*GuiName\s+(.*)')
204     HelpTextStart = re.compile(r'\s*HelpText\s')
205     HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
206     HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
207     i = -1
208     for src in input_files:
209         input = open(src)
210         inHelp = False
211         hadHelp = False
212         prev_help_string = ''
213         for lineno, line in enumerate(input.readlines()):
214             if Template.match(line):
215                 (string,) = Template.match(line).groups()
216             elif GuiName.match(line):
217                 (string,) = GuiName.match(line).groups()
218             elif inHelp:
219                 if HelpTextEnd.match(line):
220                     if hadHelp:
221                         print >> output, '\nmsgstr ""\n'
222                     inHelp = False
223                     hadHelp = False
224                     prev_help_string = ''
225                 elif HelpTextSection.match(line):
226                     (help_string,) = HelpTextSection.match(line).groups()
227                     help_string = help_string.replace('"', '')
228                     if help_string != "" and prev_help_string == '':
229                         print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
230                             (relativePath(src, base), lineno+1, help_string)
231                         hadHelp = True
232                     elif help_string != "":
233                         print >> output, '"%s\\n"' % help_string
234                     prev_help_string = help_string
235             elif HelpTextStart.match(line):
236                 inHelp = True
237                 prev_help_string = ''
238             else:
239                 continue
240             string = string.replace('"', '')
241             if string != "" and not inHelp:
242                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
243                     (relativePath(src, base), lineno+1, string)
244         input.close()
245     output.close()
246
247
248 def formats_l10n(input_files, output, base):
249     '''Generate pot file from configure.py'''
250     output = open(output, 'w')
251     GuiName = re.compile(r'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
252     GuiName2 = re.compile(r'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
253     input = open(input_files[0])
254     for lineno, line in enumerate(input.readlines()):
255         label = ""
256         labelsc = ""
257         if GuiName.match(line):
258             label = GuiName.match(line).group(1)
259             shortcut = GuiName.match(line).group(2).replace('"', '')
260         elif GuiName2.match(line):
261             label = GuiName2.match(line).group(1)
262             shortcut = GuiName2.match(line).group(2).replace('"', '')
263         else:
264             continue
265         label = label.replace('\\', '\\\\').replace('"', '')
266         if shortcut != "":
267             labelsc = label + "|" + shortcut
268         if label != "":
269             print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
270                 (relativePath(input_files[0], base), lineno+1, label)
271         if labelsc != "":
272             print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
273                 (relativePath(input_files[0], base), lineno+1, labelsc)
274     input.close()
275     output.close()
276
277
278 Usage = '''
279 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
280
281 where 
282     --base:
283         path to the top source directory. default to '.'
284     --output:
285         output pot file, default to './lyx.pot'
286     --input_type can be
287         ui: lib/ui/*
288         layouts: lib/layouts/*
289         qt4: qt4 ui files
290         languages: file lib/languages
291         external: external templates file
292         formats: formats predefined in lib/configure.py
293 '''
294
295 if __name__ == '__main__':
296     input_type = None
297     output = 'lyx.pot'
298     base = '.'
299     #
300     optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
301         ['help', 'type=', 'output=', 'base='])
302     for (opt, value) in optlist:
303         if opt in ['-h', '--help']:
304             print Usage
305             sys.exit(0)
306         elif opt in ['-o', '--output']:
307             output = value
308         elif opt in ['-b', '--base']:
309             base = value
310         elif opt in ['-t', '--type']:
311             input_type = value
312     if input_type not in ['ui', 'layouts', 'modules', 'qt4', 'languages', 'external', 'formats'] or output is None:
313         print 'Wrong input type or output filename.'
314         sys.exit(1)
315     if input_type == 'ui':
316         ui_l10n(args, output, base)
317     elif input_type == 'layouts':
318         layouts_l10n(args, output, base)
319     elif input_type == 'qt4':
320         qt4_l10n(args, output, base)
321     elif input_type == 'external':
322         external_l10n(args, output, base)
323     elif input_type == 'formats':
324         formats_l10n(args, output, base)
325     else:
326         languages_l10n(args, output, base)
327