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+"([^"]*)"', re.IGNORECASE)
48 Popupmenu = re.compile(r'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
49 IconPalette = re.compile(r'^[^#]*IconPalette\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
50 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
51 Item = re.compile(r'[^#]*Item\s+"([^"]*)"', re.IGNORECASE)
52 TableInsert = re.compile(r'[^#]*TableInsert\s+"([^"]*)"', re.IGNORECASE)
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 ClassDescription = re.compile(r'^\s*#\s*\\Declare(LaTeX|DocBook)Class.*\{(.*)\}$', re.IGNORECASE)
82 ClassCategory = re.compile(r'^\s*#\s*\\DeclareCategory\{(.*)\}$', re.IGNORECASE)
83 Style = re.compile(r'^\s*Style\s+(.*\S)\s*$', re.IGNORECASE)
84 # match LabelString, EndLabelString, LabelStringAppendix and maybe others but no comments
85 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*\S)\s*$', re.IGNORECASE)
86 MenuString = re.compile(r'^[^#]*MenuString\S*\s+(.*\S)\s*$', re.IGNORECASE)
87 Tooltip = re.compile(r'^[^#]*Tooltip\S*\s+(.*\S)\s*$', re.IGNORECASE)
88 GuiName = re.compile(r'^\s*GuiName\s+(.*\S)\s*$', re.IGNORECASE)
89 ListName = re.compile(r'^\s*ListName\s+(.*\S)\s*$', re.IGNORECASE)
90 CategoryName = re.compile(r'^\s*Category\s+(.*\S)\s*$', re.IGNORECASE)
91 NameRE = re.compile(r'^\s*#\s*\\DeclareLyXModule.*{(.*)}$', re.IGNORECASE)
92 InsetLayout = re.compile(r'^InsetLayout\s+\"?(.*)\"?\s*$', re.IGNORECASE)
93 FlexCheck = re.compile(r'^Flex:(.*)', re.IGNORECASE)
94 CaptionCheck = re.compile(r'^Caption:(.*)', re.IGNORECASE)
95 DescBegin = re.compile(r'^\s*#DescriptionBegin\s*$', re.IGNORECASE)
96 DescEnd = re.compile(r'^\s*#\s*DescriptionEnd\s*$', re.IGNORECASE)
97 Category = re.compile(r'^\s*#\s*Category:\s+(.*\S)\s*$', re.IGNORECASE)
98 I18nPreamble = re.compile(r'^\s*((Lang)|(Babel))Preamble\s*$', re.IGNORECASE)
99 EndI18nPreamble = re.compile(r'^\s*End((Lang)|(Babel))Preamble\s*$', re.IGNORECASE)
100 I18nString = re.compile(r'_\(([^\)]+)\)')
101 CounterFormat = re.compile(r'^\s*PrettyFormat\s+"?(.*)"?\s*$', re.IGNORECASE)
102 CiteFormat = re.compile(r'^\s*CiteFormat', re.IGNORECASE)
103 KeyVal = re.compile(r'^\s*_\w+\s+(.*\S)\s*$')
104 Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE)
105 UsesFloatPkg = re.compile(r'^\s*UsesFloatPkg\s+(.*\S)\s*$', re.IGNORECASE)
106 IsPredefined = re.compile(r'^\s*IsPredefined\s+(.*\S)\s*$', re.IGNORECASE)
107 End = re.compile(r'^\s*End', re.IGNORECASE)
108 Comment = re.compile(r'^(.*)#')
109 Translation = re.compile(r'^\s*Translation\s+(.*\S)\s*$', re.IGNORECASE)
110 KeyValPair = re.compile(r'\s*"(.*)"\s+"(.*)"')
116 if layouttranslations:
117 linguas_file = os.path.join(base, 'po/LINGUAS')
118 for line in open(linguas_file).readlines():
119 res = Comment.search(line)
122 if line.strip() != '':
123 languages.extend(line.split())
125 # read old translations if available
129 for line in input.readlines():
130 res = Comment.search(line)
133 if line.strip() == '':
135 res = Translation.search(line)
138 if lang not in languages:
139 oldlanguages.append(lang)
140 languages.append(lang)
141 oldtrans[lang] = dict()
143 res = End.search(line)
147 res = KeyValPair.search(line)
148 if res and lang != '':
149 key = res.group(1).decode('utf-8')
150 val = res.group(2).decode('utf-8')
151 key = key.replace('\\"', '"').replace('\\\\', '\\')
152 val = val.replace('\\"', '"').replace('\\\\', '\\')
153 oldtrans[lang][key] = val
156 print "Error: Unable to handle line:"
159 print "Warning: Unable to open %s for reading." % output
160 print " Old translations will be lost."
162 # walon is not a known document language
163 # FIXME: Do not hardcode, read from lib/languages!
164 if 'wa' in languages:
165 languages.remove('wa')
167 out = open(output, 'w')
168 for src in input_files:
169 readingDescription = False
170 readingI18nPreamble = False
172 readingCiteFormats = False
180 for line in open(src).readlines():
182 res = ClassDescription.search(line)
184 string = res.group(2)
185 if not layouttranslations:
186 writeString(out, src, base, lineno + 1, string)
188 res = ClassCategory.search(line)
190 string = res.group(1)
191 if not layouttranslations:
192 writeString(out, src, base, lineno + 1, string)
194 if readingDescription:
195 res = DescEnd.search(line)
197 readingDescription = False
198 desc = " ".join(descLines)
199 if not layouttranslations:
200 writeString(out, src, base, lineno + 1, desc)
202 descLines.append(line[1:].strip())
204 res = DescBegin.search(line)
206 readingDescription = True
207 descStartLine = lineno
209 if readingI18nPreamble:
210 res = EndI18nPreamble.search(line)
212 readingI18nPreamble = False
214 res = I18nString.search(line)
216 string = res.group(1)
217 if layouttranslations:
220 writeString(out, src, base, lineno, string)
222 res = I18nPreamble.search(line)
224 readingI18nPreamble = True
226 res = NameRE.search(line)
228 string = res.group(1)
229 if not layouttranslations:
230 writeString(out, src, base, lineno + 1, string)
232 res = Style.search(line)
234 string = res.group(1)
235 string = string.replace('_', ' ')
236 # Style means something else inside a float definition
238 if not layouttranslations:
239 writeString(out, src, base, lineno, string)
241 res = LabelString.search(line)
243 string = res.group(1)
244 if not layouttranslations:
245 writeString(out, src, base, lineno, string)
247 res = MenuString.search(line)
249 string = res.group(1)
250 if not layouttranslations:
251 writeString(out, src, base, lineno, string)
253 res = Tooltip.search(line)
255 string = res.group(1)
256 if not layouttranslations:
257 writeString(out, src, base, lineno, string)
259 res = GuiName.search(line)
261 string = res.group(1)
262 if layouttranslations:
263 # gui name must only be added for floats
267 writeString(out, src, base, lineno, string)
269 res = CategoryName.search(line)
271 string = res.group(1)
272 if not layouttranslations:
273 writeString(out, src, base, lineno, string)
275 res = ListName.search(line)
277 string = res.group(1)
278 if layouttranslations:
279 listname = string.strip('"')
281 writeString(out, src, base, lineno, string)
283 res = InsetLayout.search(line)
285 string = res.group(1)
286 string = string.replace('_', ' ')
287 #Flex:xxx is not used in translation
288 #if not layouttranslations:
289 # writeString(out, src, base, lineno, string)
290 m = FlexCheck.search(string)
292 if not layouttranslations:
293 writeString(out, src, base, lineno, m.group(1))
294 m = CaptionCheck.search(string)
296 if not layouttranslations:
297 writeString(out, src, base, lineno, m.group(1))
299 res = Category.search(line)
301 string = res.group(1)
302 if not layouttranslations:
303 writeString(out, src, base, lineno, string)
305 res = CounterFormat.search(line)
307 string = res.group(1)
308 if not layouttranslations:
309 writeString(out, src, base, lineno, string)
311 res = Float.search(line)
315 res = IsPredefined.search(line)
317 string = res.group(1).lower()
323 res = UsesFloatPkg.search(line)
325 string = res.group(1).lower()
331 res = CiteFormat.search(line)
333 readingCiteFormats = True
335 res = End.search(line)
337 # We have four combinations of the flags usesFloatPkg and isPredefined:
338 # usesFloatPkg and isPredefined: might use standard babel translations
339 # usesFloatPkg and not isPredefined: does not use standard babel translations
340 # not usesFloatPkg and isPredefined: uses standard babel translations
341 # not usesFloatPkg and not isPredefined: not supported by LyX
342 # The third combination is even true for MarginFigure, MarginTable (both from
343 # tufte-book.layout) and Planotable, Plate (both from aguplus.inc).
344 if layouttranslations and readingFloat and usesFloatPkg:
346 keyset.add(floatname)
353 readingCiteFormats = False
356 if readingCiteFormats:
357 res = KeyVal.search(line)
360 if not layouttranslations:
361 writeString(out, src, base, lineno, val)
363 if layouttranslations:
364 # Extract translations of layout files
367 # Sort languages and key to minimize the diff between different runs
368 # with changed translations
375 ContextRe = re.compile(r'(.*)(\[\[.*\]\])')
377 print >> out, '''# This file has been automatically generated by po/lyx_pot.py.
378 # PLEASE MODIFY ONLY THE LAGUAGES HAVING NO .po FILE! If you want to regenerate
379 # this file from the translations, run `make ../lib/layouttranslations' in po.
380 # Python polib library is needed for building the output file.
382 # This file should remain fixed during minor LyX releases.
383 # For more comments see README.localization file.'''
384 for lang in languages:
385 print >> out, '\nTranslation %s' % lang
386 if lang in oldtrans.keys():
387 trans = oldtrans[lang]
390 if not lang in oldlanguages:
391 poname = os.path.join(base, 'po/' + lang + '.po')
392 po = polib.pofile(poname)
393 # Iterate through po entries and not keys for speed reasons.
394 # FIXME: The code is still too slow
396 if not entry.translated():
398 if entry.msgid in keys:
401 # some translators keep untranslated entries
405 if key in trans.keys():
406 val = trans[key].replace('\\', '\\\\').replace('"', '\\"')
407 res = ContextRe.search(val)
410 key = key.replace('\\', '\\\\').replace('"', '\\"')
411 print >> out, '\t"%s" "%s"' % \
412 (key.encode('utf-8'), val.encode('utf-8'))
413 # also print untranslated entries to help translators
414 elif not lang in oldlanguages:
415 key = key.replace('\\', '\\\\').replace('"', '\\"')
416 res = ContextRe.search(key)
421 print >> out, '\t"%s" "%s"' % \
422 (key.encode('utf-8'), val.encode('utf-8'))
428 def qt4_l10n(input_files, output, base):
429 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
430 output = open(output, 'w')
431 pat = re.compile(r'\s*<string>(.*)</string>')
432 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
433 for src in input_files:
436 for lineno, line in enumerate(input.readlines()):
437 # skip the line after <property name=shortcut>
444 # get lines that match <string>...</string>
446 (string,) = pat.match(line).groups()
447 string = string.replace('&', '&').replace('"', '"')
448 string = string.replace('<', '<').replace('>', '>')
449 string = string.replace('\\', '\\\\').replace('"', r'\"')
450 string = string.replace('
', r'\n')
451 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
452 (relativePath(src, base), lineno+1, string)
457 def languages_l10n(input_files, output, base):
458 '''Generate pot file from lib/languages'''
459 out = open(output, 'w')
460 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)', re.IGNORECASE)
462 for src in input_files:
466 for line in open(src).readlines():
468 res = GuiName.search(line)
470 string = res.group(1)
471 writeString(out, src, base, lineno, string)
477 def latexfonts_l10n(input_files, output, base):
478 '''Generate pot file from lib/latexfonts'''
479 out = open(output, 'w')
480 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)', re.IGNORECASE)
482 for src in input_files:
486 for line in open(src).readlines():
488 res = GuiName.search(line)
490 string = res.group(1)
491 writeString(out, src, base, lineno, string)
497 def external_l10n(input_files, output, base):
498 '''Generate pot file from lib/external_templates'''
499 output = open(output, 'w')
500 Template = re.compile(r'^Template\s+(.*)', re.IGNORECASE)
501 GuiName = re.compile(r'\s*GuiName\s+(.*)', re.IGNORECASE)
502 HelpTextStart = re.compile(r'\s*HelpText\s', re.IGNORECASE)
503 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
504 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s', re.IGNORECASE)
506 for src in input_files:
510 prev_help_string = ''
511 for lineno, line in enumerate(input.readlines()):
512 if Template.match(line):
513 (string,) = Template.match(line).groups()
514 elif GuiName.match(line):
515 (string,) = GuiName.match(line).groups()
517 if HelpTextEnd.match(line):
519 print >> output, '\nmsgstr ""\n'
522 prev_help_string = ''
523 elif HelpTextSection.match(line):
524 (help_string,) = HelpTextSection.match(line).groups()
525 help_string = help_string.replace('"', '')
526 if help_string != "" and prev_help_string == '':
527 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
528 (relativePath(src, base), lineno+1, help_string)
530 elif help_string != "":
531 print >> output, '"%s\\n"' % help_string
532 prev_help_string = help_string
533 elif HelpTextStart.match(line):
535 prev_help_string = ''
538 string = string.replace('"', '')
539 if string != "" and not inHelp:
540 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
541 (relativePath(src, base), lineno+1, string)
546 def formats_l10n(input_files, output, base):
547 '''Generate pot file from configure.py'''
548 output = open(output, 'w')
549 GuiName = re.compile(r'.*\\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*', re.IGNORECASE)
550 GuiName2 = re.compile(r'.*\\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*', re.IGNORECASE)
551 input = open(input_files[0])
552 for lineno, line in enumerate(input.readlines()):
555 if GuiName.match(line):
556 label = GuiName.match(line).group(1)
557 shortcut = GuiName.match(line).group(2).replace('"', '')
558 elif GuiName2.match(line):
559 label = GuiName2.match(line).group(1)
560 shortcut = GuiName2.match(line).group(2).replace('"', '')
563 label = label.replace('\\', '\\\\').replace('"', '')
565 labelsc = label + "|" + shortcut
567 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
568 (relativePath(input_files[0], base), lineno+1, label)
570 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
571 (relativePath(input_files[0], base), lineno+1, labelsc)
576 def encodings_l10n(input_files, output, base):
577 '''Generate pot file from lib/encodings'''
578 output = open(output, 'w')
579 # assuming only one encodings file
580 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
581 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable|variableunsafe)\s+\w+.*')
582 input = open(input_files[0])
583 for lineno, line in enumerate(input.readlines()):
584 if not line.startswith('Encoding'):
587 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
588 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
590 print "Error: Unable to handle line:"
592 # No need to abort if the parsing fails
600 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
604 path to the top source directory. default to '.'
606 output pot file, default to './lyx.pot'
608 filename that contains a list of input files in each line
611 layouts: lib/layouts/*
612 layouttranslations: create lib/layouttranslations from po/*.po and lib/layouts/*
614 languages: file lib/languages
615 latexfonts: file lib/latexfonts
616 encodings: file lib/encodings
617 external: external templates file
618 formats: formats predefined in lib/configure.py
621 if __name__ == '__main__':
627 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
628 ['help', 'type=', 'output=', 'base=', 'src_file='])
629 for (opt, value) in optlist:
630 if opt in ['-h', '--help']:
633 elif opt in ['-o', '--output']:
635 elif opt in ['-b', '--base']:
637 elif opt in ['-t', '--type']:
639 elif opt in ['-s', '--src_file']:
640 input_files = [f.strip() for f in open(value)]
642 if input_type not in ['ui', 'layouts', 'layouttranslations', 'qt4', 'languages', 'latexfonts', 'encodings', 'external', 'formats'] or output is None:
643 print 'Wrong input type or output filename.'
648 if input_type == 'ui':
649 ui_l10n(input_files, output, base)
650 elif input_type == 'latexfonts':
651 latexfonts_l10n(input_files, output, base)
652 elif input_type == 'layouts':
653 layouts_l10n(input_files, output, base, False)
654 elif input_type == 'layouttranslations':
655 layouts_l10n(input_files, output, base, True)
656 elif input_type == 'qt4':
657 qt4_l10n(input_files, output, base)
658 elif input_type == 'external':
659 external_l10n(input_files, output, base)
660 elif input_type == 'formats':
661 formats_l10n(input_files, output, base)
662 elif input_type == 'encodings':
663 encodings_l10n(input_files, output, base)
665 languages_l10n(input_files, output, base)