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 FlexCheck = re.compile(r'^Flex:(.*)')
89 DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
90 DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
91 Category = re.compile(r'#Category: (.*)$')
92 I18nPreamble = re.compile(r'\s*(Lang)|(Babel)Preamble\s*$')
93 EndI18nPreamble = re.compile(r'\s*End(Lang)|(Babel)Preamble\s*$')
94 I18nString = re.compile(r'_\(([^\)]+)\)')
95 CounterFormat = re.compile(r'\s*PrettyFormat\s+"?(.*)"?')
96 CiteFormat = re.compile(r'\s*CiteFormat')
97 KeyVal = re.compile(r'^\s*_\w+\s+(.*)$')
98 End = re.compile(r'\s*End')
100 for src in input_files:
101 readingDescription = False
102 readingI18nPreamble = False
103 readingCiteFormats = False
107 for line in open(src).readlines():
109 if readingDescription:
110 res = DescEnd.search(line)
112 readingDescription = False
113 desc = " ".join(descLines)
114 writeString(out, src, base, lineno + 1, desc)
116 descLines.append(line[1:].strip())
118 res = DescBegin.search(line)
120 readingDescription = True
121 descStartLine = lineno
123 if readingI18nPreamble:
124 res = EndI18nPreamble.search(line)
126 readingI18nPreamble = False
128 res = I18nString.search(line)
130 string = res.group(1)
131 writeString(out, src, base, lineno, string)
133 res = I18nPreamble.search(line)
135 readingI18nPreamble = True
137 res = NameRE.search(line)
139 string = res.group(1)
140 string = string.replace('\\', '\\\\').replace('"', '')
142 print >> out, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
143 (relativePath(src, base), lineno + 1, string)
145 res = Style.search(line)
147 string = res.group(1)
148 string = string.replace('_', ' ')
149 writeString(out, src, base, lineno, string)
151 res = LabelString.search(line)
153 string = res.group(1)
154 writeString(out, src, base, lineno, string)
156 res = GuiName.search(line)
158 string = res.group(1)
159 writeString(out, src, base, lineno, string)
161 res = CategoryName.search(line)
163 string = res.group(1)
164 writeString(out, src, base, lineno, string)
166 res = ListName.search(line)
168 string = res.group(1)
169 writeString(out, src, base, lineno, string)
171 res = InsetLayout.search(line)
173 string = res.group(1)
174 string = string.replace('_', ' ')
175 #Flex:xxx is not used in translation
176 #writeString(out, src, base, lineno, string)
177 m = FlexCheck.search(string)
179 writeString(out, src, base, lineno, m.group(1))
181 res = Category.search(line)
183 string = res.group(1)
184 writeString(out, src, base, lineno, string)
186 res = CounterFormat.search(line)
188 string = res.group(1)
189 writeString(out, src, base, lineno, string)
191 res = CiteFormat.search(line)
193 readingCiteFormats = True
194 res = End.search(line)
195 if res != None and readingCiteFormats:
196 readingCiteFormats = False
197 if readingCiteFormats:
198 res = KeyVal.search(line)
201 writeString(out, src, base, lineno, val)
206 def qt4_l10n(input_files, output, base):
207 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
208 output = open(output, 'w')
209 pat = re.compile(r'\s*<string>(.*)</string>')
210 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
211 for src in input_files:
214 for lineno, line in enumerate(input.readlines()):
215 # skip the line after <property name=shortcut>
222 # get lines that match <string>...</string>
224 (string,) = pat.match(line).groups()
225 string = string.replace('&', '&').replace('<', '<').replace('>', '>')
226 string = string.replace('\\', '\\\\').replace('"', r'\"')
227 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
228 (relativePath(src, base), lineno+1, string)
233 def languages_l10n(input_files, output, base):
234 '''Generate pot file from lib/languages'''
235 out = open(output, 'w')
236 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)')
238 for src in input_files:
242 for line in open(src).readlines():
244 res = GuiName.search(line)
246 string = res.group(1)
247 writeString(out, src, base, lineno, string)
253 def external_l10n(input_files, output, base):
254 '''Generate pot file from lib/external_templates'''
255 output = open(output, 'w')
256 Template = re.compile(r'^Template\s+(.*)')
257 GuiName = re.compile(r'\s*GuiName\s+(.*)')
258 HelpTextStart = re.compile(r'\s*HelpText\s')
259 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
260 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
262 for src in input_files:
266 prev_help_string = ''
267 for lineno, line in enumerate(input.readlines()):
268 if Template.match(line):
269 (string,) = Template.match(line).groups()
270 elif GuiName.match(line):
271 (string,) = GuiName.match(line).groups()
273 if HelpTextEnd.match(line):
275 print >> output, '\nmsgstr ""\n'
278 prev_help_string = ''
279 elif HelpTextSection.match(line):
280 (help_string,) = HelpTextSection.match(line).groups()
281 help_string = help_string.replace('"', '')
282 if help_string != "" and prev_help_string == '':
283 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
284 (relativePath(src, base), lineno+1, help_string)
286 elif help_string != "":
287 print >> output, '"%s\\n"' % help_string
288 prev_help_string = help_string
289 elif HelpTextStart.match(line):
291 prev_help_string = ''
294 string = string.replace('"', '')
295 if string != "" and not inHelp:
296 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
297 (relativePath(src, base), lineno+1, string)
302 def formats_l10n(input_files, output, base):
303 '''Generate pot file from configure.py'''
304 output = open(output, 'w')
305 GuiName = re.compile(r'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
306 GuiName2 = re.compile(r'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
307 input = open(input_files[0])
308 for lineno, line in enumerate(input.readlines()):
311 if GuiName.match(line):
312 label = GuiName.match(line).group(1)
313 shortcut = GuiName.match(line).group(2).replace('"', '')
314 elif GuiName2.match(line):
315 label = GuiName2.match(line).group(1)
316 shortcut = GuiName2.match(line).group(2).replace('"', '')
319 label = label.replace('\\', '\\\\').replace('"', '')
321 labelsc = label + "|" + shortcut
323 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
324 (relativePath(input_files[0], base), lineno+1, label)
326 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
327 (relativePath(input_files[0], base), lineno+1, labelsc)
332 def encodings_l10n(input_files, output, base):
333 '''Generate pot file from lib/encodings'''
334 output = open(output, 'w')
335 # assuming only one encodings file
336 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
337 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable)\s+\w+.*')
338 input = open(input_files[0])
339 for lineno, line in enumerate(input.readlines()):
340 if not line.startswith('Encoding'):
343 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
344 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
346 print "Error: Unable to handle line:"
348 # No need to abort if the parsing fails
356 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
360 path to the top source directory. default to '.'
362 output pot file, default to './lyx.pot'
364 filename that contains a list of input files in each line
367 layouts: lib/layouts/*
369 languages: file lib/languages
370 encodings: file lib/encodings
371 external: external templates file
372 formats: formats predefined in lib/configure.py
375 if __name__ == '__main__':
381 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
382 ['help', 'type=', 'output=', 'base=', 'src_file='])
383 for (opt, value) in optlist:
384 if opt in ['-h', '--help']:
387 elif opt in ['-o', '--output']:
389 elif opt in ['-b', '--base']:
391 elif opt in ['-t', '--type']:
393 elif opt in ['-s', '--src_file']:
394 input_files = [f.strip() for f in open(value)]
396 if input_type not in ['ui', 'layouts', 'modules', 'qt4', 'languages', 'encodings', 'external', 'formats'] or output is None:
397 print 'Wrong input type or output filename.'
402 if input_type == 'ui':
403 ui_l10n(input_files, output, base)
404 elif input_type == 'layouts':
405 layouts_l10n(input_files, output, base)
406 elif input_type == 'qt4':
407 qt4_l10n(input_files, output, base)
408 elif input_type == 'external':
409 external_l10n(input_files, output, base)
410 elif input_type == 'formats':
411 formats_l10n(input_files, output, base)
412 elif input_type == 'encodings':
413 encodings_l10n(input_files, output, base)
415 languages_l10n(input_files, output, base)