2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
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('\\', '/')
34 def writeString(outfile, infile, basefile, lineno, string):
35 string = string.replace('\\', '\\\\').replace('"', '')
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 IconPalette = re.compile(r'^[^#]*IconPalette\s+"[^"]+"\s+"([^"]*)"')
48 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
49 Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
50 TableInsert = re.compile(r'[^#]*TableInsert\s+"([^"]*)"')
51 for src in input_files:
53 for lineno, line in enumerate(input.readlines()):
54 if Submenu.match(line):
55 (string,) = Submenu.match(line).groups()
56 string = string.replace('_', ' ')
57 elif Popupmenu.match(line):
58 (string,) = Popupmenu.match(line).groups()
59 elif IconPalette.match(line):
60 (string,) = IconPalette.match(line).groups()
61 elif Toolbar.match(line):
62 (string,) = Toolbar.match(line).groups()
63 elif Item.match(line):
64 (string,) = Item.match(line).groups()
65 elif TableInsert.match(line):
66 (string,) = TableInsert.match(line).groups()
69 string = string.replace('"', '')
71 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
72 (relativePath(src, base), lineno+1, string)
77 def layouts_l10n(input_files, output, base):
78 '''Generate pot file from lib/layouts/*.{layout,inc,module}'''
79 out = open(output, 'w')
80 Style = re.compile(r'^Style\s+(.*)', re.IGNORECASE)
81 # include ???LabelString???, but exclude comment lines
82 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
83 GuiName = re.compile(r'\s*GuiName\s+(.*)')
84 ListName = re.compile(r'\s*ListName\s+(.*)')
85 CategoryName = re.compile(r'\s*Category\s+(.*)')
86 NameRE = re.compile(r'DeclareLyXModule.*{(.*)}')
87 InsetLayout = re.compile(r'^InsetLayout\s+(.*)')
88 DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
89 DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
90 Category = re.compile(r'#Category: (.*)$')
91 I18nPreamble = re.compile(r'\s*(Lang)|(Babel)Preamble\s*$')
92 EndI18nPreamble = re.compile(r'\s*End(Lang)|(Babel)Preamble\s*$')
93 I18nString = re.compile(r'_\(([^\)]+)\)')
94 CounterFormat = re.compile(r'\s*PrettyFormat\s+"?(.*)"?')
96 for src in input_files:
97 readingDescription = False
98 readingI18nPreamble = False
102 for line in open(src).readlines():
104 if readingDescription:
105 res = DescEnd.search(line)
107 readingDescription = False
108 desc = " ".join(descLines)
109 writeString(out, src, base, lineno + 1, desc)
111 descLines.append(line[1:].strip())
113 res = DescBegin.search(line)
115 readingDescription = True
116 descStartLine = lineno
118 if readingI18nPreamble:
119 res = EndI18nPreamble.search(line)
121 readingI18nPreamble = False
123 res = I18nString.search(line)
125 string = res.group(1)
126 writeString(out, src, base, lineno, string)
128 res = I18nPreamble.search(line)
130 readingI18nPreamble = True
132 res = NameRE.search(line)
134 string = res.group(1)
135 string = string.replace('\\', '\\\\').replace('"', '')
137 print >> out, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
138 (relativePath(src, base), lineno + 1, string)
140 res = Style.search(line)
142 string = res.group(1)
143 string = string.replace('_', ' ')
144 writeString(out, src, base, lineno, string)
146 res = LabelString.search(line)
148 string = res.group(1)
149 writeString(out, src, base, lineno, string)
151 res = GuiName.search(line)
153 string = res.group(1)
154 writeString(out, src, base, lineno, string)
156 res = CategoryName.search(line)
158 string = res.group(1)
159 writeString(out, src, base, lineno, string)
161 res = ListName.search(line)
163 string = res.group(1)
164 writeString(out, src, base, lineno, string)
166 res = InsetLayout.search(line)
168 string = res.group(1)
169 string = string.replace('_', ' ')
170 writeString(out, src, base, lineno, string)
172 res = Category.search(line)
174 string = res.group(1)
175 writeString(out, src, base, lineno, string)
177 res = CounterFormat.search(line)
179 string = res.group(1)
180 writeString(out, src, base, lineno, string)
185 def qt4_l10n(input_files, output, base):
186 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
187 output = open(output, 'w')
188 pat = re.compile(r'\s*<string>(.*)</string>')
189 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
190 for src in input_files:
193 for lineno, line in enumerate(input.readlines()):
194 # skip the line after <property name=shortcut>
201 # get lines that match <string>...</string>
203 (string,) = pat.match(line).groups()
204 string = string.replace('&', '&').replace('<', '<').replace('>', '>')
205 string = string.replace('\\', '\\\\').replace('"', r'\"')
206 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
207 (relativePath(src, base), lineno+1, string)
212 def languages_l10n(input_files, output, base):
213 '''Generate pot file from lib/language'''
214 output = open(output, 'w')
215 # assuming only one language file
216 reg = re.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\),]+)"\s+(true|false)\s+[\w-]+\s+[\w\-]+\s+"[^"]*"')
217 input = open(input_files[0])
218 for lineno, line in enumerate(input.readlines()):
222 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
228 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
229 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
231 print "Error: Unable to handle line:"
233 # No need to abort if the parsing fails (e.g. "ignore" language has no encoding)
239 def external_l10n(input_files, output, base):
240 '''Generate pot file from lib/external_templates'''
241 output = open(output, 'w')
242 Template = re.compile(r'^Template\s+(.*)')
243 GuiName = re.compile(r'\s*GuiName\s+(.*)')
244 HelpTextStart = re.compile(r'\s*HelpText\s')
245 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
246 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
248 for src in input_files:
252 prev_help_string = ''
253 for lineno, line in enumerate(input.readlines()):
254 if Template.match(line):
255 (string,) = Template.match(line).groups()
256 elif GuiName.match(line):
257 (string,) = GuiName.match(line).groups()
259 if HelpTextEnd.match(line):
261 print >> output, '\nmsgstr ""\n'
264 prev_help_string = ''
265 elif HelpTextSection.match(line):
266 (help_string,) = HelpTextSection.match(line).groups()
267 help_string = help_string.replace('"', '')
268 if help_string != "" and prev_help_string == '':
269 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
270 (relativePath(src, base), lineno+1, help_string)
272 elif help_string != "":
273 print >> output, '"%s\\n"' % help_string
274 prev_help_string = help_string
275 elif HelpTextStart.match(line):
277 prev_help_string = ''
280 string = string.replace('"', '')
281 if string != "" and not inHelp:
282 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
283 (relativePath(src, base), lineno+1, string)
288 def formats_l10n(input_files, output, base):
289 '''Generate pot file from configure.py'''
290 output = open(output, 'w')
291 GuiName = re.compile(r'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
292 GuiName2 = re.compile(r'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
293 input = open(input_files[0])
294 for lineno, line in enumerate(input.readlines()):
297 if GuiName.match(line):
298 label = GuiName.match(line).group(1)
299 shortcut = GuiName.match(line).group(2).replace('"', '')
300 elif GuiName2.match(line):
301 label = GuiName2.match(line).group(1)
302 shortcut = GuiName2.match(line).group(2).replace('"', '')
305 label = label.replace('\\', '\\\\').replace('"', '')
307 labelsc = label + "|" + shortcut
309 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
310 (relativePath(input_files[0], base), lineno+1, label)
312 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
313 (relativePath(input_files[0], base), lineno+1, labelsc)
318 def encodings_l10n(input_files, output, base):
319 '''Generate pot file from lib/encodings'''
320 output = open(output, 'w')
321 # assuming only one encodings file
322 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
323 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable)\s+\w+.*')
324 input = open(input_files[0])
325 for lineno, line in enumerate(input.readlines()):
326 if not line.startswith('Encoding'):
329 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
330 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
332 print "Error: Unable to handle line:"
334 # No need to abort if the parsing fails
342 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
346 path to the top source directory. default to '.'
348 output pot file, default to './lyx.pot'
350 filename that contains a list of input files in each line
353 layouts: lib/layouts/*
355 languages: file lib/languages
356 encodings: file lib/encodings
357 external: external templates file
358 formats: formats predefined in lib/configure.py
361 if __name__ == '__main__':
367 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
368 ['help', 'type=', 'output=', 'base=', 'src_file='])
369 for (opt, value) in optlist:
370 if opt in ['-h', '--help']:
373 elif opt in ['-o', '--output']:
375 elif opt in ['-b', '--base']:
377 elif opt in ['-t', '--type']:
379 elif opt in ['-s', '--src_file']:
380 input_files = [f.strip() for f in open(value)]
382 if input_type not in ['ui', 'layouts', 'modules', 'qt4', 'languages', 'encodings', 'external', 'formats'] or output is None:
383 print 'Wrong input type or output filename.'
388 if input_type == 'ui':
389 ui_l10n(input_files, output, base)
390 elif input_type == 'layouts':
391 layouts_l10n(input_files, output, base)
392 elif input_type == 'qt4':
393 qt4_l10n(input_files, output, base)
394 elif input_type == 'external':
395 external_l10n(input_files, output, base)
396 elif input_type == 'formats':
397 formats_l10n(input_files, output, base)
398 elif input_type == 'encodings':
399 encodings_l10n(input_files, output, base)
401 languages_l10n(input_files, output, base)