Back to svn state
[lyx.git] / po / lyx_pot.py
blob551f00953328c78699c5297e97ac68eb2a20529e
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
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.
8 # \author Bo Peng
10 # Full author contact details are available in file CREDITS
12 # Usage: use
13 # lyx_pot.py -h
14 # to get usage message
16 # This script will extract translatable strings from input files and write
17 # to output in gettext .pot format.
19 import sys, os, re, getopt
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
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)
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()
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 InsetLayout = re.compile(r'^InsetLayout\s+(.*)')
85 DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
86 DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
88 for src in input_files:
89 readingDescription = False
90 descStartLine = -1
91 descLines = []
92 lineno = 0
93 for line in open(src).readlines():
94 lineno += 1
95 if readingDescription:
96 res = DescEnd.search(line)
97 if res != None:
98 readingDescription = False
99 desc = " ".join(descLines)
100 writeString(out, src, base, lineno + 1, desc)
101 continue
102 descLines.append(line[1:].strip())
103 continue
104 res = DescBegin.search(line)
105 if res != None:
106 readingDescription = True
107 descStartLine = lineno
108 continue
109 res = NameRE.search(line)
110 if res != None:
111 string = res.group(1)
112 string = string.replace('\\', '\\\\').replace('"', '')
113 if string != "":
114 print >> out, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
115 (relativePath(src, base), lineno + 1, string)
116 continue
117 res = Style.search(line)
118 if res != None:
119 string = res.group(1)
120 string = string.replace('_', ' ')
121 writeString(out, src, base, lineno, string)
122 continue
123 res = LabelString.search(line)
124 if res != None:
125 string = res.group(1)
126 writeString(out, src, base, lineno, string)
127 continue
128 res = GuiName.search(line)
129 if res != None:
130 string = res.group(1)
131 writeString(out, src, base, lineno, string)
132 continue
133 res = CategoryName.search(line)
134 if res != None:
135 string = res.group(1)
136 writeString(out, src, base, lineno, string)
137 continue
138 res = ListName.search(line)
139 if res != None:
140 string = res.group(1)
141 writeString(out, src, base, lineno, string)
142 continue
143 res = InsetLayout.search(line)
144 if res != None:
145 string = res.group(1)
146 string = string.replace('_', ' ')
147 writeString(out, src, base, lineno, string)
148 continue
149 out.close()
152 def qt4_l10n(input_files, output, base):
153 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
154 output = open(output, 'w')
155 pat = re.compile(r'\s*<string>(.*)</string>')
156 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
157 for src in input_files:
158 input = open(src)
159 skipNextLine = False
160 for lineno, line in enumerate(input.readlines()):
161 # skip the line after <property name=shortcut>
162 if skipNextLine:
163 skipNextLine = False
164 continue
165 if prop.match(line):
166 skipNextLine = True
167 continue
168 # get lines that match <string>...</string>
169 if pat.match(line):
170 (string,) = pat.match(line).groups()
171 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>')
172 string = string.replace('\\', '\\\\').replace('"', r'\"')
173 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
174 (relativePath(src, base), lineno+1, string)
175 input.close()
176 output.close()
179 def languages_l10n(input_files, output, base):
180 '''Generate pot file from lib/language'''
181 output = open(output, 'w')
182 # assuming only one language file
183 reg = re.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\)]+)"\s+(true|false)\s+[\w-]+\s+\w+\s+"[^"]*"')
184 input = open(input_files[0])
185 for lineno, line in enumerate(input.readlines()):
186 if line[0] == '#':
187 continue
188 # From:
189 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
190 # To:
191 # #: lib/languages:2
192 # msgid "Afrikaans"
193 # msgstr ""
194 if reg.match(line):
195 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
196 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
197 else:
198 print "Error: Unable to handle line:"
199 print line
200 # No need to abort if the parsing fails (e.g. "ignore" language has no encoding)
201 # sys.exit(1)
202 input.close()
203 output.close()
206 def external_l10n(input_files, output, base):
207 '''Generate pot file from lib/external_templates'''
208 output = open(output, 'w')
209 Template = re.compile(r'^Template\s+(.*)')
210 GuiName = re.compile(r'\s*GuiName\s+(.*)')
211 HelpTextStart = re.compile(r'\s*HelpText\s')
212 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
213 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
214 i = -1
215 for src in input_files:
216 input = open(src)
217 inHelp = False
218 hadHelp = False
219 prev_help_string = ''
220 for lineno, line in enumerate(input.readlines()):
221 if Template.match(line):
222 (string,) = Template.match(line).groups()
223 elif GuiName.match(line):
224 (string,) = GuiName.match(line).groups()
225 elif inHelp:
226 if HelpTextEnd.match(line):
227 if hadHelp:
228 print >> output, '\nmsgstr ""\n'
229 inHelp = False
230 hadHelp = False
231 prev_help_string = ''
232 elif HelpTextSection.match(line):
233 (help_string,) = HelpTextSection.match(line).groups()
234 help_string = help_string.replace('"', '')
235 if help_string != "" and prev_help_string == '':
236 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
237 (relativePath(src, base), lineno+1, help_string)
238 hadHelp = True
239 elif help_string != "":
240 print >> output, '"%s\\n"' % help_string
241 prev_help_string = help_string
242 elif HelpTextStart.match(line):
243 inHelp = True
244 prev_help_string = ''
245 else:
246 continue
247 string = string.replace('"', '')
248 if string != "" and not inHelp:
249 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
250 (relativePath(src, base), lineno+1, string)
251 input.close()
252 output.close()
255 def formats_l10n(input_files, output, base):
256 '''Generate pot file from configure.py'''
257 output = open(output, 'w')
258 GuiName = re.compile(r'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
259 GuiName2 = re.compile(r'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
260 input = open(input_files[0])
261 for lineno, line in enumerate(input.readlines()):
262 label = ""
263 labelsc = ""
264 if GuiName.match(line):
265 label = GuiName.match(line).group(1)
266 shortcut = GuiName.match(line).group(2).replace('"', '')
267 elif GuiName2.match(line):
268 label = GuiName2.match(line).group(1)
269 shortcut = GuiName2.match(line).group(2).replace('"', '')
270 else:
271 continue
272 label = label.replace('\\', '\\\\').replace('"', '')
273 if shortcut != "":
274 labelsc = label + "|" + shortcut
275 if label != "":
276 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
277 (relativePath(input_files[0], base), lineno+1, label)
278 if labelsc != "":
279 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
280 (relativePath(input_files[0], base), lineno+1, labelsc)
281 input.close()
282 output.close()
285 def encodings_l10n(input_files, output, base):
286 '''Generate pot file from lib/encodings'''
287 output = open(output, 'w')
288 # assuming only one encodings file
289 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
290 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable)\s+\w+.*')
291 input = open(input_files[0])
292 for lineno, line in enumerate(input.readlines()):
293 if not line.startswith('Encoding'):
294 continue
295 if reg.match(line):
296 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
297 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
298 else:
299 print "Error: Unable to handle line:"
300 print line
301 # No need to abort if the parsing fails
302 # sys.exit(1)
303 input.close()
304 output.close()
308 Usage = '''
309 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
311 where
312 --base:
313 path to the top source directory. default to '.'
314 --output:
315 output pot file, default to './lyx.pot'
316 --input_type can be
317 ui: lib/ui/*
318 layouts: lib/layouts/*
319 qt4: qt4 ui files
320 languages: file lib/languages
321 encodings: file lib/encodings
322 external: external templates file
323 formats: formats predefined in lib/configure.py
326 if __name__ == '__main__':
327 input_type = None
328 output = 'lyx.pot'
329 base = '.'
331 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
332 ['help', 'type=', 'output=', 'base='])
333 for (opt, value) in optlist:
334 if opt in ['-h', '--help']:
335 print Usage
336 sys.exit(0)
337 elif opt in ['-o', '--output']:
338 output = value
339 elif opt in ['-b', '--base']:
340 base = value
341 elif opt in ['-t', '--type']:
342 input_type = value
343 if input_type not in ['ui', 'layouts', 'modules', 'qt4', 'languages', 'encodings', 'external', 'formats'] or output is None:
344 print 'Wrong input type or output filename.'
345 sys.exit(1)
346 if input_type == 'ui':
347 ui_l10n(args, output, base)
348 elif input_type == 'layouts':
349 layouts_l10n(args, output, base)
350 elif input_type == 'qt4':
351 qt4_l10n(args, output, base)
352 elif input_type == 'external':
353 external_l10n(args, output, base)
354 elif input_type == 'formats':
355 formats_l10n(args, output, base)
356 elif input_type == 'encodings':
357 encodings_l10n(args, output, base)
358 else:
359 languages_l10n(args, output, base)