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 Style = re.compile(r'^Style\s+(.*)', re.IGNORECASE)
82 # include ???LabelString???, but exclude comment lines
83 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
84 GuiName = re.compile(r'\s*GuiName\s+(.*)')
85 ListName = re.compile(r'\s*ListName\s+(.*)')
86 CategoryName = re.compile(r'\s*Category\s+(.*)')
87 NameRE = re.compile(r'DeclareLyXModule.*{(.*)}')
88 InsetLayout = re.compile(r'^InsetLayout\s+\"?(.*)\"?')
89 FlexCheck = re.compile(r'^Flex:(.*)')
90 DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
91 DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
92 Category = re.compile(r'#Category: (.*)$')
93 I18nPreamble = re.compile(r'\s*(Lang)|(Babel)Preamble\s*$')
94 EndI18nPreamble = re.compile(r'\s*End(Lang)|(Babel)Preamble\s*$')
95 I18nString = re.compile(r'_\(([^\)]+)\)')
96 CounterFormat = re.compile(r'\s*PrettyFormat\s+"?(.*)"?')
97 CiteFormat = re.compile(r'\s*CiteFormat')
98 KeyVal = re.compile(r'^\s*_\w+\s+(.*)$')
99 Float = re.compile(r'\s*Float\s*$')
100 UsesFloatPkg = re.compile(r'\s*UsesFloatPkg\s+(.*)')
101 IsPredefined = re.compile(r'\s*IsPredefined\s+(.*)')
102 End = re.compile(r'\s*End')
103 Comment = re.compile(r'\s*#')
104 Translation = re.compile(r'\s*Translation\s+(.*)\s*$')
105 KeyValPair = re.compile(r'\s*"(.*)"\s+"(.*)"')
111 if layouttranslations:
112 linguas_file = os.path.join(base, 'po/LINGUAS')
113 for line in open(linguas_file).readlines():
114 if Comment.search(line) == None:
115 languages.extend(line.split())
117 # read old translations if available
121 for line in input.readlines():
122 res = Comment.search(line)
125 if line.strip() == '':
127 res = Translation.search(line)
130 if lang not in languages:
131 oldlanguages.append(lang)
132 languages.append(lang)
133 oldtrans[lang] = dict()
135 res = End.search(line)
139 res = KeyValPair.search(line)
140 if res and lang != '':
141 key = res.group(1).decode('utf-8')
142 val = res.group(2).decode('utf-8')
143 key = key.replace('\\"', '"').replace('\\\\', '\\')
144 val = val.replace('\\"', '"').replace('\\\\', '\\')
145 oldtrans[lang][key] = val
148 print "Error: Unable to handle line:"
151 print "Warning: Unable to open %s for reading." % output
152 print " Old translations will be lost."
154 # walon is not a known document language
155 # FIXME: Do not hardcode, read from lib/languages!
156 if 'wa' in languages:
157 languages.remove('wa')
159 out = open(output, 'w')
160 for src in input_files:
161 readingDescription = False
162 readingI18nPreamble = False
164 readingCiteFormats = False
172 for line in open(src).readlines():
174 if readingDescription:
175 res = DescEnd.search(line)
177 readingDescription = False
178 desc = " ".join(descLines)
179 if not layouttranslations:
180 writeString(out, src, base, lineno + 1, desc)
182 descLines.append(line[1:].strip())
184 res = DescBegin.search(line)
186 readingDescription = True
187 descStartLine = lineno
189 if readingI18nPreamble:
190 res = EndI18nPreamble.search(line)
192 readingI18nPreamble = False
194 res = I18nString.search(line)
196 string = res.group(1)
197 if layouttranslations:
200 writeString(out, src, base, lineno, string)
202 res = I18nPreamble.search(line)
204 readingI18nPreamble = True
206 res = NameRE.search(line)
208 string = res.group(1)
209 if not layouttranslations:
210 writeString(out, src, base, lineno + 1, string)
212 res = Style.search(line)
214 string = res.group(1)
215 string = string.replace('_', ' ')
216 if not layouttranslations:
217 writeString(out, src, base, lineno, string)
219 res = LabelString.search(line)
221 string = res.group(1)
222 if not layouttranslations:
223 writeString(out, src, base, lineno, string)
225 res = GuiName.search(line)
227 string = res.group(1)
228 if layouttranslations:
229 # gui name must only be added for floats
233 writeString(out, src, base, lineno, string)
235 res = CategoryName.search(line)
237 string = res.group(1)
238 if not layouttranslations:
239 writeString(out, src, base, lineno, string)
241 res = ListName.search(line)
243 string = res.group(1)
244 if layouttranslations:
245 listname = string.strip('"')
247 writeString(out, src, base, lineno, string)
249 res = InsetLayout.search(line)
251 string = res.group(1)
252 string = string.replace('_', ' ')
253 #Flex:xxx is not used in translation
254 #if not layouttranslations:
255 # writeString(out, src, base, lineno, string)
256 m = FlexCheck.search(string)
258 if not layouttranslations:
259 writeString(out, src, base, lineno, m.group(1))
261 res = Category.search(line)
263 string = res.group(1)
264 if not layouttranslations:
265 writeString(out, src, base, lineno, string)
267 res = CounterFormat.search(line)
269 string = res.group(1)
270 if not layouttranslations:
271 writeString(out, src, base, lineno, string)
273 res = Float.search(line)
277 res = IsPredefined.search(line)
279 string = res.group(1).lower()
285 res = UsesFloatPkg.search(line)
287 string = res.group(1).lower()
293 res = CiteFormat.search(line)
295 readingCiteFormats = True
297 res = End.search(line)
299 # If a float is predefined by the package and it does not need
300 # the float package then it uses the standard babel translations.
301 # This is even true for MarginFigure, MarginTable (both from
302 # tufte-book.layout) and Planotable, Plate (both from aguplus.inc).
303 if layouttranslations and readingFloat and usesFloatPkg and not isPredefined:
305 keyset.add(floatname)
312 readingCiteFormats = False
315 if readingCiteFormats:
316 res = KeyVal.search(line)
319 if not layouttranslations:
320 writeString(out, src, base, lineno, val)
322 if layouttranslations:
323 # Extract translations of layout files
326 # Sort languages and key to minimize the diff between different runs
327 # with changed translations
334 print >> out, '''# This file has been automatically generated by po/lyx_pot.py.
335 # PLEASE MODIFY ONLY THE LAGUAGES HAVING NO .po FILE! If you want to regenerate
336 # this file from the translations, run `make ../lib/layouttranslations' in po.
337 # Python polib library is needed for building the output file.
339 # This file should remain fixed during minor LyX releases.
340 # For more comments see README.localization file.'''
341 for lang in languages:
342 print >> out, '\nTranslation %s' % lang
343 if lang in oldtrans.keys():
344 trans = oldtrans[lang]
347 if not lang in oldlanguages:
348 poname = os.path.join(base, 'po/' + lang + '.po')
349 po = polib.pofile(poname)
350 # Iterate through po entries and not keys for speed reasons.
351 # FIXME: The code is still too slow
353 if not entry.translated():
355 if entry.msgid in keys:
358 # some translators keep untranslated entries
362 if key in trans.keys():
363 val = trans[key].replace('\\', '\\\\').replace('"', '\\"')
364 key = key.replace('\\', '\\\\').replace('"', '\\"')
365 print >> out, '\t"%s" "%s"' % \
366 (key.encode('utf-8'), val.encode('utf-8'))
367 # also print untranslated entries to help translators
368 elif not lang in oldlanguages:
369 key = key.replace('\\', '\\\\').replace('"', '\\"')
370 print >> out, '\t"%s" "%s"' % \
371 (key.encode('utf-8'), key.encode('utf-8'))
377 def qt4_l10n(input_files, output, base):
378 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
379 output = open(output, 'w')
380 pat = re.compile(r'\s*<string>(.*)</string>')
381 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
382 for src in input_files:
385 for lineno, line in enumerate(input.readlines()):
386 # skip the line after <property name=shortcut>
393 # get lines that match <string>...</string>
395 (string,) = pat.match(line).groups()
396 string = string.replace('&', '&').replace('"', '"')
397 string = string.replace('<', '<').replace('>', '>')
398 string = string.replace('\\', '\\\\').replace('"', r'\"')
399 string = string.replace('
', r'\n')
400 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
401 (relativePath(src, base), lineno+1, string)
406 def languages_l10n(input_files, output, base):
407 '''Generate pot file from lib/languages'''
408 out = open(output, 'w')
409 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)')
411 for src in input_files:
415 for line in open(src).readlines():
417 res = GuiName.search(line)
419 string = res.group(1)
420 writeString(out, src, base, lineno, string)
426 def external_l10n(input_files, output, base):
427 '''Generate pot file from lib/external_templates'''
428 output = open(output, 'w')
429 Template = re.compile(r'^Template\s+(.*)')
430 GuiName = re.compile(r'\s*GuiName\s+(.*)')
431 HelpTextStart = re.compile(r'\s*HelpText\s')
432 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
433 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
435 for src in input_files:
439 prev_help_string = ''
440 for lineno, line in enumerate(input.readlines()):
441 if Template.match(line):
442 (string,) = Template.match(line).groups()
443 elif GuiName.match(line):
444 (string,) = GuiName.match(line).groups()
446 if HelpTextEnd.match(line):
448 print >> output, '\nmsgstr ""\n'
451 prev_help_string = ''
452 elif HelpTextSection.match(line):
453 (help_string,) = HelpTextSection.match(line).groups()
454 help_string = help_string.replace('"', '')
455 if help_string != "" and prev_help_string == '':
456 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
457 (relativePath(src, base), lineno+1, help_string)
459 elif help_string != "":
460 print >> output, '"%s\\n"' % help_string
461 prev_help_string = help_string
462 elif HelpTextStart.match(line):
464 prev_help_string = ''
467 string = string.replace('"', '')
468 if string != "" and not inHelp:
469 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
470 (relativePath(src, base), lineno+1, string)
475 def formats_l10n(input_files, output, base):
476 '''Generate pot file from configure.py'''
477 output = open(output, 'w')
478 GuiName = re.compile(r'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
479 GuiName2 = re.compile(r'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
480 input = open(input_files[0])
481 for lineno, line in enumerate(input.readlines()):
484 if GuiName.match(line):
485 label = GuiName.match(line).group(1)
486 shortcut = GuiName.match(line).group(2).replace('"', '')
487 elif GuiName2.match(line):
488 label = GuiName2.match(line).group(1)
489 shortcut = GuiName2.match(line).group(2).replace('"', '')
492 label = label.replace('\\', '\\\\').replace('"', '')
494 labelsc = label + "|" + shortcut
496 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
497 (relativePath(input_files[0], base), lineno+1, label)
499 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
500 (relativePath(input_files[0], base), lineno+1, labelsc)
505 def encodings_l10n(input_files, output, base):
506 '''Generate pot file from lib/encodings'''
507 output = open(output, 'w')
508 # assuming only one encodings file
509 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
510 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable)\s+\w+.*')
511 input = open(input_files[0])
512 for lineno, line in enumerate(input.readlines()):
513 if not line.startswith('Encoding'):
516 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
517 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
519 print "Error: Unable to handle line:"
521 # No need to abort if the parsing fails
529 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
533 path to the top source directory. default to '.'
535 output pot file, default to './lyx.pot'
537 filename that contains a list of input files in each line
540 layouts: lib/layouts/*
541 layouttranslations: create lib/layouttranslations from po/*.po and lib/layouts/*
543 languages: file lib/languages
544 encodings: file lib/encodings
545 external: external templates file
546 formats: formats predefined in lib/configure.py
549 if __name__ == '__main__':
555 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
556 ['help', 'type=', 'output=', 'base=', 'src_file='])
557 for (opt, value) in optlist:
558 if opt in ['-h', '--help']:
561 elif opt in ['-o', '--output']:
563 elif opt in ['-b', '--base']:
565 elif opt in ['-t', '--type']:
567 elif opt in ['-s', '--src_file']:
568 input_files = [f.strip() for f in open(value)]
570 if input_type not in ['ui', 'layouts', 'layouttranslations', 'qt4', 'languages', 'encodings', 'external', 'formats'] or output is None:
571 print 'Wrong input type or output filename.'
576 if input_type == 'ui':
577 ui_l10n(input_files, output, base)
578 elif input_type == 'layouts':
579 layouts_l10n(input_files, output, base, False)
580 elif input_type == 'layouttranslations':
581 layouts_l10n(input_files, output, base, True)
582 elif input_type == 'qt4':
583 qt4_l10n(input_files, output, base)
584 elif input_type == 'external':
585 external_l10n(input_files, output, base)
586 elif input_type == 'formats':
587 formats_l10n(input_files, output, base)
588 elif input_type == 'encodings':
589 encodings_l10n(input_files, output, base)
591 languages_l10n(input_files, output, base)