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
20 if sys.version_info < (2, 4, 0):
21 from sets import Set as set
23 def relativePath(path, base):
24 '''return relative path from top source dir'''
25 # full pathname of path
26 path1 = os.path.normpath(os.path.realpath(path)).split(os.sep)
27 path2 = os.path.normpath(os.path.realpath(base)).split(os.sep)
28 if path1[:len(path2)] != path2:
29 print "Path %s is not under top source directory" % path
30 path3 = os.path.join(*path1[len(path2):]);
31 # replace all \ by / such that we get the same comments on Windows and *nix
32 path3 = path3.replace('\\', '/')
36 def writeString(outfile, infile, basefile, lineno, string):
37 string = string.replace('\\', '\\\\').replace('"', '')
40 print >> outfile, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
41 (relativePath(infile, basefile), lineno, string)
44 def ui_l10n(input_files, output, base):
45 '''Generate pot file from lib/ui/*'''
46 output = open(output, 'w')
47 Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"')
48 Popupmenu = re.compile(r'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"')
49 IconPalette = re.compile(r'^[^#]*IconPalette\s+"[^"]+"\s+"([^"]*)"')
50 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
51 Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
52 TableInsert = re.compile(r'[^#]*TableInsert\s+"([^"]*)"')
53 for src in input_files:
55 for lineno, line in enumerate(input.readlines()):
56 if Submenu.match(line):
57 (string,) = Submenu.match(line).groups()
58 string = string.replace('_', ' ')
59 elif Popupmenu.match(line):
60 (string,) = Popupmenu.match(line).groups()
61 elif IconPalette.match(line):
62 (string,) = IconPalette.match(line).groups()
63 elif Toolbar.match(line):
64 (string,) = Toolbar.match(line).groups()
65 elif Item.match(line):
66 (string,) = Item.match(line).groups()
67 elif TableInsert.match(line):
68 (string,) = TableInsert.match(line).groups()
71 string = string.replace('"', '')
73 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
74 (relativePath(src, base), lineno+1, string)
79 def layouts_l10n(input_files, output, base, layouttranslations):
80 '''Generate pot file from lib/layouts/*.{layout,inc,module}'''
81 out = open(output, 'w')
82 Style = re.compile(r'^Style\s+(.*)', re.IGNORECASE)
83 # include ???LabelString???, but exclude comment lines
84 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
85 GuiName = re.compile(r'\s*GuiName\s+(.*)')
86 ListName = re.compile(r'\s*ListName\s+(.*)')
87 CategoryName = re.compile(r'\s*Category\s+(.*)')
88 NameRE = re.compile(r'DeclareLyXModule.*{(.*)}')
89 InsetLayout = re.compile(r'^InsetLayout\s+\"?(.*)\"?')
90 FlexCheck = re.compile(r'^Flex:(.*)')
91 DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
92 DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
93 Category = re.compile(r'#Category: (.*)$')
94 I18nPreamble = re.compile(r'\s*(Lang)|(Babel)Preamble\s*$')
95 EndI18nPreamble = re.compile(r'\s*End(Lang)|(Babel)Preamble\s*$')
96 I18nString = re.compile(r'_\(([^\)]+)\)')
97 CounterFormat = re.compile(r'\s*PrettyFormat\s+"?(.*)"?')
98 CiteFormat = re.compile(r'\s*CiteFormat')
99 KeyVal = re.compile(r'^\s*_\w+\s+(.*)$')
100 Float = re.compile(r'\s*Float')
101 End = re.compile(r'\s*End')
102 Comment = re.compile(r'\s*#')
106 if layouttranslations:
107 linguas_file = os.path.join(base, 'po/LINGUAS')
108 for line in open(linguas_file).readlines():
109 if Comment.search(line) == None:
110 languages.extend(line.split())
111 # walon is not a known document language
112 # FIXME: Do not hardcode, read from lib/languages!
113 if 'wa' in languages:
114 languages.remove('wa')
116 for src in input_files:
117 readingDescription = False
118 readingI18nPreamble = False
120 readingCiteFormats = False
124 for line in open(src).readlines():
126 if readingDescription:
127 res = DescEnd.search(line)
129 readingDescription = False
130 desc = " ".join(descLines)
131 if not layouttranslations:
132 writeString(out, src, base, lineno + 1, desc)
134 descLines.append(line[1:].strip())
136 res = DescBegin.search(line)
138 readingDescription = True
139 descStartLine = lineno
141 if readingI18nPreamble:
142 res = EndI18nPreamble.search(line)
144 readingI18nPreamble = False
146 res = I18nString.search(line)
148 string = res.group(1)
149 if layouttranslations:
152 writeString(out, src, base, lineno, string)
154 res = I18nPreamble.search(line)
156 readingI18nPreamble = True
158 res = NameRE.search(line)
160 string = res.group(1)
161 if not layouttranslations:
162 writeString(out, src, base, lineno + 1, string)
164 res = Style.search(line)
166 string = res.group(1)
167 string = string.replace('_', ' ')
168 if not layouttranslations:
169 writeString(out, src, base, lineno, string)
171 res = LabelString.search(line)
173 string = res.group(1)
174 if not layouttranslations:
175 writeString(out, src, base, lineno, string)
177 res = GuiName.search(line)
179 string = res.group(1)
180 if layouttranslations:
181 # gui name must only be added for floats
185 writeString(out, src, base, lineno, string)
187 res = CategoryName.search(line)
189 string = res.group(1)
190 if not layouttranslations:
191 writeString(out, src, base, lineno, string)
193 res = ListName.search(line)
195 string = res.group(1)
196 if layouttranslations:
197 keyset.add(string.strip('"'))
199 writeString(out, src, base, lineno, string)
201 res = InsetLayout.search(line)
203 string = res.group(1)
204 string = string.replace('_', ' ')
205 #Flex:xxx is not used in translation
206 #if not layouttranslations:
207 # writeString(out, src, base, lineno, string)
208 m = FlexCheck.search(string)
210 if not layouttranslations:
211 writeString(out, src, base, lineno, m.group(1))
213 res = Category.search(line)
215 string = res.group(1)
216 if not layouttranslations:
217 writeString(out, src, base, lineno, string)
219 res = CounterFormat.search(line)
221 string = res.group(1)
222 if not layouttranslations:
223 writeString(out, src, base, lineno, string)
225 res = Float.search(line)
229 res = CiteFormat.search(line)
231 readingCiteFormats = True
232 res = End.search(line)
234 readingCiteFormats = False
236 if readingCiteFormats:
237 res = KeyVal.search(line)
240 if not layouttranslations:
241 writeString(out, src, base, lineno, val)
243 if layouttranslations:
244 # Extract translations of layout files
247 # Sort languages and key to minimize the diff between different runs
248 # with changed translations
255 print >> out, '''# This file has been automatically generated by po/lyx_pot.py.
256 # PLEASE DO NOT MODIFY ANYTHING HERE! If you want to regenerate this file
257 # from the translations, run `make ../lib/layouttranslations' in po.'''
258 for lang in languages:
259 print >> out, '\nTranslation %s' % lang
260 poname = os.path.join(base, 'po/' + lang + '.po')
261 po = polib.pofile(poname)
262 # Iterate through po entries and not keys for speed reasons.
263 # FIXME: The code is still too slow
266 if not entry.translated():
268 if entry.msgid in keys:
269 key = entry.msgid.replace('\\', '\\\\').replace('"', '\\"')
270 val = entry.msgstr.replace('\\', '\\\\').replace('"', '\\"')
271 # some translators keep untranslated entries
275 if key in trans.keys():
277 print >> out, '\t"%s" "%s"' % \
278 (key.encode('utf-8'), val.encode('utf-8'))
284 def qt4_l10n(input_files, output, base):
285 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
286 output = open(output, 'w')
287 pat = re.compile(r'\s*<string>(.*)</string>')
288 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
289 for src in input_files:
292 for lineno, line in enumerate(input.readlines()):
293 # skip the line after <property name=shortcut>
300 # get lines that match <string>...</string>
302 (string,) = pat.match(line).groups()
303 string = string.replace('&', '&').replace('"', '"')
304 string = string.replace('<', '<').replace('>', '>')
305 string = string.replace('\\', '\\\\').replace('"', r'\"')
306 string = string.replace('
', r'\n')
307 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
308 (relativePath(src, base), lineno+1, string)
313 def languages_l10n(input_files, output, base):
314 '''Generate pot file from lib/languages'''
315 out = open(output, 'w')
316 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)')
318 for src in input_files:
322 for line in open(src).readlines():
324 res = GuiName.search(line)
326 string = res.group(1)
327 writeString(out, src, base, lineno, string)
333 def external_l10n(input_files, output, base):
334 '''Generate pot file from lib/external_templates'''
335 output = open(output, 'w')
336 Template = re.compile(r'^Template\s+(.*)')
337 GuiName = re.compile(r'\s*GuiName\s+(.*)')
338 HelpTextStart = re.compile(r'\s*HelpText\s')
339 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
340 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
342 for src in input_files:
346 prev_help_string = ''
347 for lineno, line in enumerate(input.readlines()):
348 if Template.match(line):
349 (string,) = Template.match(line).groups()
350 elif GuiName.match(line):
351 (string,) = GuiName.match(line).groups()
353 if HelpTextEnd.match(line):
355 print >> output, '\nmsgstr ""\n'
358 prev_help_string = ''
359 elif HelpTextSection.match(line):
360 (help_string,) = HelpTextSection.match(line).groups()
361 help_string = help_string.replace('"', '')
362 if help_string != "" and prev_help_string == '':
363 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
364 (relativePath(src, base), lineno+1, help_string)
366 elif help_string != "":
367 print >> output, '"%s\\n"' % help_string
368 prev_help_string = help_string
369 elif HelpTextStart.match(line):
371 prev_help_string = ''
374 string = string.replace('"', '')
375 if string != "" and not inHelp:
376 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
377 (relativePath(src, base), lineno+1, string)
382 def formats_l10n(input_files, output, base):
383 '''Generate pot file from configure.py'''
384 output = open(output, 'w')
385 GuiName = re.compile(r'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
386 GuiName2 = re.compile(r'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
387 input = open(input_files[0])
388 for lineno, line in enumerate(input.readlines()):
391 if GuiName.match(line):
392 label = GuiName.match(line).group(1)
393 shortcut = GuiName.match(line).group(2).replace('"', '')
394 elif GuiName2.match(line):
395 label = GuiName2.match(line).group(1)
396 shortcut = GuiName2.match(line).group(2).replace('"', '')
399 label = label.replace('\\', '\\\\').replace('"', '')
401 labelsc = label + "|" + shortcut
403 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
404 (relativePath(input_files[0], base), lineno+1, label)
406 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
407 (relativePath(input_files[0], base), lineno+1, labelsc)
412 def encodings_l10n(input_files, output, base):
413 '''Generate pot file from lib/encodings'''
414 output = open(output, 'w')
415 # assuming only one encodings file
416 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
417 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable)\s+\w+.*')
418 input = open(input_files[0])
419 for lineno, line in enumerate(input.readlines()):
420 if not line.startswith('Encoding'):
423 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
424 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
426 print "Error: Unable to handle line:"
428 # No need to abort if the parsing fails
436 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
440 path to the top source directory. default to '.'
442 output pot file, default to './lyx.pot'
444 filename that contains a list of input files in each line
447 layouts: lib/layouts/*
448 layouttranslations: create lib/layouttranslations from po/*.po and lib/layouts/*
450 languages: file lib/languages
451 encodings: file lib/encodings
452 external: external templates file
453 formats: formats predefined in lib/configure.py
456 if __name__ == '__main__':
462 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
463 ['help', 'type=', 'output=', 'base=', 'src_file='])
464 for (opt, value) in optlist:
465 if opt in ['-h', '--help']:
468 elif opt in ['-o', '--output']:
470 elif opt in ['-b', '--base']:
472 elif opt in ['-t', '--type']:
474 elif opt in ['-s', '--src_file']:
475 input_files = [f.strip() for f in open(value)]
477 if input_type not in ['ui', 'layouts', 'layouttranslations', 'qt4', 'languages', 'encodings', 'external', 'formats'] or output is None:
478 print 'Wrong input type or output filename.'
483 if input_type == 'ui':
484 ui_l10n(input_files, output, base)
485 elif input_type == 'layouts':
486 layouts_l10n(input_files, output, base, False)
487 elif input_type == 'layouttranslations':
488 layouts_l10n(input_files, output, base, True)
489 elif input_type == 'qt4':
490 qt4_l10n(input_files, output, base)
491 elif input_type == 'external':
492 external_l10n(input_files, output, base)
493 elif input_type == 'formats':
494 formats_l10n(input_files, output, base)
495 elif input_type == 'encodings':
496 encodings_l10n(input_files, output, base)
498 languages_l10n(input_files, output, base)