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 from __future__ import print_function
21 import glob, sys, os, re, getopt
24 def relativePath(path, base):
25 '''return relative path from top source dir'''
26 # full pathname of path
27 path1 = os.path.normpath(os.path.realpath(path)).split(os.sep)
28 path2 = os.path.normpath(os.path.realpath(base)).split(os.sep)
29 if path1[:len(path2)] != path2:
30 print("Path %s is not under top source directory" % path)
31 path3 = os.path.join(*path1[len(path2):]);
32 # replace all \ by / such that we get the same comments on Windows and *nix
33 path3 = path3.replace('\\', '/')
37 def writeString(outfile, infile, basefile, lineno, string):
38 string = string.replace('\\', '\\\\').replace('"', '')
41 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
42 (relativePath(infile, basefile), lineno, string), file=outfile)
45 def ui_l10n(input_files, output, base):
46 '''Generate pot file from lib/ui/*'''
47 output = io.open(output, 'w', encoding='utf_8', newline='\n')
48 Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"', re.IGNORECASE)
49 Popupmenu = re.compile(r'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
50 Dynamicmenu = re.compile(r'^[^#]*DynamicMenu\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
51 IconPalette = re.compile(r'^[^#]*IconPalette\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
52 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"', re.IGNORECASE)
53 Item = re.compile(r'[^#]*Item\s+"([^"]*)"', re.IGNORECASE)
54 TableInsert = re.compile(r'[^#]*TableInsert\s+"([^"]*)"', re.IGNORECASE)
55 for src in input_files:
56 input = io.open(src, encoding='utf_8')
57 for lineno, line in enumerate(input.readlines()):
58 if Submenu.match(line):
59 (string,) = Submenu.match(line).groups()
60 string = string.replace('_', ' ')
61 elif Popupmenu.match(line):
62 (string,) = Popupmenu.match(line).groups()
63 elif Dynamicmenu.match(line):
64 (string,) = Dynamicmenu.match(line).groups()
65 elif IconPalette.match(line):
66 (string,) = IconPalette.match(line).groups()
67 elif Toolbar.match(line):
68 (string,) = Toolbar.match(line).groups()
69 elif Item.match(line):
70 (string,) = Item.match(line).groups()
71 elif TableInsert.match(line):
72 (string,) = TableInsert.match(line).groups()
75 string = string.replace('"', '')
77 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
78 (relativePath(src, base), lineno+1, string), file=output)
83 def layouts_l10n(input_files, output, base, layouttranslations):
84 '''Generate pot file from lib/layouts/*.{layout,inc,module} and lib/citeengines/*.citeengine'''
85 ClassDescription = re.compile(r'^\s*#\s*\\Declare(LaTeX|DocBook)Class.*\{(.*)\}$', re.IGNORECASE)
86 ClassCategory = re.compile(r'^\s*#\s*\\DeclareCategory\{(.*)\}$', re.IGNORECASE)
87 Style = re.compile(r'^\s*Style\s+(.*\S)\s*$', re.IGNORECASE)
88 # match LabelString, EndLabelString, LabelStringAppendix and maybe others but no comments
89 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*\S)\s*$', re.IGNORECASE)
90 MenuString = re.compile(r'^[^#]*MenuString\S*\s+(.*\S)\s*$', re.IGNORECASE)
91 OutlinerName = re.compile(r'^[^#]*OutlinerName\s+(\S+|\"[^\"]*\")\s+\"([^\"]*)\"', re.IGNORECASE)
92 Tooltip = re.compile(r'^\s*Tooltip\S*\s+(.*\S)\s*$', re.IGNORECASE)
93 GuiName = re.compile(r'^\s*GuiName\s+(.*\S)\s*$', re.IGNORECASE)
94 ListName = re.compile(r'^\s*ListName\s+(.*\S)\s*$', re.IGNORECASE)
95 CategoryName = re.compile(r'^\s*Category\s+(.*\S)\s*$', re.IGNORECASE)
96 NameRE = re.compile(r'^\s*#\s*\\DeclareLyXModule.*{(.*)}$', re.IGNORECASE)
97 CiteNameRE = re.compile(r'^\s*#\s*\\DeclareLyXCiteEngine.*\{(.*)\}$', re.IGNORECASE)
98 InsetLayout = re.compile(r'^InsetLayout\s+\"?(.*)\"?\s*$', re.IGNORECASE)
99 FlexCheck = re.compile(r'^Flex:(.*)', re.IGNORECASE)
100 CaptionCheck = re.compile(r'^Caption:(.*)', re.IGNORECASE)
101 DescBegin = re.compile(r'^\s*#\s*DescriptionBegin\s*$', re.IGNORECASE)
102 DescEnd = re.compile(r'^\s*#\s*DescriptionEnd\s*$', re.IGNORECASE)
103 Category = re.compile(r'^\s*#\s*Category:\s+(.*\S)\s*$', re.IGNORECASE)
104 I18nPreamble = re.compile(r'^\s*((Lang)|(Babel))Preamble\s*$', re.IGNORECASE)
105 EndI18nPreamble = re.compile(r'^\s*End((Lang)|(Babel))Preamble\s*$', re.IGNORECASE)
106 I18nString = re.compile(r'_\(([^\)]+)\)')
107 CounterFormat = re.compile(r'^\s*PrettyFormat\s+"?(.*)"?\s*$', re.IGNORECASE)
108 CiteFormat = re.compile(r'^\s*CiteFormat', re.IGNORECASE)
109 # Note: preceding and trailing space in the val below matters
110 KeyVal = re.compile(r'^\s*_\w+\s(.*\S)*$')
111 Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE)
112 UsesFloatPkg = re.compile(r'^\s*UsesFloatPkg\s+(.*\S)\s*$', re.IGNORECASE)
113 IsPredefined = re.compile(r'^\s*IsPredefined\s+(.*\S)\s*$', re.IGNORECASE)
114 End = re.compile(r'^\s*End', re.IGNORECASE)
115 Comment = re.compile(r'^(.*)#')
116 Translation = re.compile(r'^\s*Translation\s+(.*\S)\s*$', re.IGNORECASE)
117 KeyValPair = re.compile(r'\s*"(.*)"\s+"(.*)"')
123 if layouttranslations:
124 linguas_file = os.path.join(base, 'po/LINGUAS')
125 for line in open(linguas_file).readlines():
126 res = Comment.search(line)
129 if line.strip() != '':
130 languages.extend(line.split())
132 # read old translations if available
134 input = io.open(output, encoding='utf_8')
136 for line in input.readlines():
137 res = Comment.search(line)
140 if line.strip() == '':
142 res = Translation.search(line)
145 if lang not in languages:
146 oldlanguages.append(lang)
147 languages.append(lang)
148 oldtrans[lang] = dict()
150 res = End.search(line)
154 res = KeyValPair.search(line)
155 if res and lang != '':
158 key = key.replace('\\"', '"').replace('\\\\', '\\')
159 val = val.replace('\\"', '"').replace('\\\\', '\\')
160 oldtrans[lang][key] = val
163 print("Error: Unable to handle line:")
166 print("Warning: Unable to open %s for reading." % output)
167 print(" Old translations will be lost.")
169 # walon is not a known document language
170 # FIXME: Do not hardcode, read from lib/languages!
171 if 'wa' in languages:
172 languages.remove('wa')
174 if layouttranslations:
175 out = io.open(output, 'w', encoding='utf_8')
177 out = io.open(output, 'w', encoding='utf_8', newline='\n')
178 for src in input_files:
179 readingDescription = False
180 readingI18nPreamble = False
182 readingCiteFormats = False
190 for line in io.open(src, encoding='utf_8').readlines():
192 res = ClassDescription.search(line)
194 string = res.group(2)
195 if not layouttranslations:
196 writeString(out, src, base, lineno + 1, string)
198 res = ClassCategory.search(line)
200 string = res.group(1)
201 if not layouttranslations:
202 writeString(out, src, base, lineno + 1, string)
204 if readingDescription:
205 res = DescEnd.search(line)
207 readingDescription = False
208 desc = " ".join(descLines)
209 if not layouttranslations:
210 writeString(out, src, base, lineno + 1, desc)
212 descLines.append(line[1:].strip())
214 res = DescBegin.search(line)
216 readingDescription = True
217 descStartLine = lineno
219 if readingI18nPreamble:
220 res = EndI18nPreamble.search(line)
222 readingI18nPreamble = False
224 res = I18nString.search(line)
226 string = res.group(1)
227 if layouttranslations:
230 writeString(out, src, base, lineno, string)
232 res = I18nPreamble.search(line)
234 readingI18nPreamble = True
236 res = NameRE.search(line)
238 string = res.group(1)
239 if not layouttranslations:
240 writeString(out, src, base, lineno + 1, string)
242 res = CiteNameRE.search(line)
244 string = res.group(1)
245 if not layouttranslations:
246 writeString(out, src, base, lineno + 1, string)
248 res = Style.search(line)
250 string = res.group(1)
251 string = string.replace('_', ' ')
252 # Style means something else inside a float definition
254 if not layouttranslations:
255 writeString(out, src, base, lineno, string)
257 res = LabelString.search(line)
259 string = res.group(1)
260 if not layouttranslations:
261 writeString(out, src, base, lineno, string)
263 res = MenuString.search(line)
265 string = res.group(1)
266 if not layouttranslations:
267 writeString(out, src, base, lineno, string)
269 res = OutlinerName.search(line)
271 string = res.group(2)
272 if not layouttranslations:
273 writeString(out, src, base, lineno, string)
275 res = Tooltip.search(line)
277 string = res.group(1)
278 if not layouttranslations:
279 writeString(out, src, base, lineno, string)
281 res = GuiName.search(line)
283 string = res.group(1)
284 if layouttranslations:
285 # gui name must only be added for floats
289 writeString(out, src, base, lineno, string)
291 res = CategoryName.search(line)
293 string = res.group(1)
294 if not layouttranslations:
295 writeString(out, src, base, lineno, string)
297 res = ListName.search(line)
299 string = res.group(1)
300 if layouttranslations:
301 listname = string.strip('"')
303 writeString(out, src, base, lineno, string)
305 res = InsetLayout.search(line)
307 string = res.group(1)
308 string = string.replace('_', ' ')
309 #Flex:xxx is not used in translation
310 #if not layouttranslations:
311 # writeString(out, src, base, lineno, string)
312 m = FlexCheck.search(string)
314 if not layouttranslations:
315 writeString(out, src, base, lineno, m.group(1))
316 m = CaptionCheck.search(string)
318 if not layouttranslations:
319 writeString(out, src, base, lineno, m.group(1))
321 res = Category.search(line)
323 string = res.group(1)
324 if not layouttranslations:
325 writeString(out, src, base, lineno, string)
327 res = CounterFormat.search(line)
329 string = res.group(1)
330 if not layouttranslations:
331 writeString(out, src, base, lineno, string)
333 res = Float.search(line)
337 res = IsPredefined.search(line)
339 string = res.group(1).lower()
345 res = UsesFloatPkg.search(line)
347 string = res.group(1).lower()
353 res = CiteFormat.search(line)
355 readingCiteFormats = True
357 res = End.search(line)
359 # We have four combinations of the flags usesFloatPkg and isPredefined:
360 # usesFloatPkg and isPredefined: might use standard babel translations
361 # usesFloatPkg and not isPredefined: does not use standard babel translations
362 # not usesFloatPkg and isPredefined: uses standard babel translations
363 # not usesFloatPkg and not isPredefined: not supported by LyX
364 # The third combination is even true for MarginFigure, MarginTable (both from
365 # tufte-book.layout) and Planotable, Plate (both from aguplus.inc).
366 if layouttranslations and readingFloat and usesFloatPkg:
368 keyset.add(floatname)
375 readingCiteFormats = False
378 if readingCiteFormats:
379 res = KeyVal.search(line)
382 if not layouttranslations:
383 writeString(out, src, base, lineno, val)
385 if layouttranslations:
386 # Extract translations of layout files
389 # Sort languages and key to minimize the diff between different runs
390 # with changed translations
397 ContextRe = re.compile(r'(.*)(\[\[.*\]\])')
399 print(u'''# This file has been automatically generated by po/lyx_pot.py.
400 # PLEASE MODIFY ONLY THE LAGUAGES HAVING NO .po FILE! If you want to regenerate
401 # this file from the translations, run `make ../lib/layouttranslations' in po.
402 # Python polib library is needed for building the output file.
404 # This file should remain fixed during minor LyX releases.
405 # For more comments see README.localization file.''', file=out)
406 for lang in languages:
407 print(u'\nTranslation %s' % lang, file=out)
408 if lang in list(oldtrans.keys()):
409 trans = oldtrans[lang]
412 if not lang in oldlanguages:
413 poname = os.path.join(base, 'po/' + lang + '.po')
414 po = polib.pofile(poname)
415 # Iterate through po entries and not keys for speed reasons.
416 # FIXME: The code is still too slow
418 if not entry.translated():
420 if entry.msgid in keys:
423 # some translators keep untranslated entries
427 if key in list(trans.keys()):
428 val = trans[key].replace('\\', '\\\\').replace('"', '\\"')
429 res = ContextRe.search(val)
432 key = key.replace('\\', '\\\\').replace('"', '\\"')
433 print(u'\t"%s" "%s"' % (key, val), file=out)
434 # also print untranslated entries to help translators
435 elif not lang in oldlanguages:
436 key = key.replace('\\', '\\\\').replace('"', '\\"')
437 res = ContextRe.search(key)
442 print(u'\t"%s" "%s"' % (key, val), file=out)
443 print(u'End', file=out)
448 def qt4_l10n(input_files, output, base):
449 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
450 output = io.open(output, 'w', encoding='utf_8', newline='\n')
451 pat = re.compile(r'\s*<string>(.*)</string>')
452 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
453 for src in input_files:
454 input = io.open(src, encoding='utf_8')
456 for lineno, line in enumerate(input.readlines()):
457 # skip the line after <property name=shortcut>
464 # get lines that match <string>...</string>
466 (string,) = pat.match(line).groups()
467 string = string.replace('&', '&').replace('"', '"')
468 string = string.replace('<', '<').replace('>', '>')
469 string = string.replace('\\', '\\\\').replace('"', r'\"')
470 string = string.replace('
', r'\n')
471 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
472 (relativePath(src, base), lineno+1, string), file=output)
477 def languages_l10n(input_files, output, base):
478 '''Generate pot file from lib/languages'''
479 out = io.open(output, 'w', encoding='utf_8', newline='\n')
480 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)', re.IGNORECASE)
482 for src in input_files:
486 for line in io.open(src, encoding='utf_8').readlines():
488 res = GuiName.search(line)
490 string = res.group(1)
491 writeString(out, src, base, lineno, string)
497 def latexfonts_l10n(input_files, output, base):
498 '''Generate pot file from lib/latexfonts'''
499 out = io.open(output, 'w', encoding='utf_8', newline='\n')
500 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)', re.IGNORECASE)
502 for src in input_files:
506 for line in io.open(src, encoding='utf_8').readlines():
508 res = GuiName.search(line)
510 string = res.group(1)
511 writeString(out, src, base, lineno, string)
517 def external_l10n(input_files, output, base):
518 '''Generate pot file from lib/xtemplates'''
519 output = io.open(output, 'w', encoding='utf_8', newline='\n')
520 Template = re.compile(r'^Template\s+(.*)', re.IGNORECASE)
521 GuiName = re.compile(r'\s*GuiName\s+(.*)', re.IGNORECASE)
522 HelpTextStart = re.compile(r'\s*HelpText\s', re.IGNORECASE)
523 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
524 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s', re.IGNORECASE)
526 for src in input_files:
527 input = io.open(src, encoding='utf_8')
530 prev_help_string = ''
531 for lineno, line in enumerate(input.readlines()):
532 if Template.match(line):
533 (string,) = Template.match(line).groups()
534 elif GuiName.match(line):
535 (string,) = GuiName.match(line).groups()
537 if HelpTextEnd.match(line):
539 print(u'\nmsgstr ""\n', file=output)
542 prev_help_string = ''
543 elif HelpTextSection.match(line):
544 (help_string,) = HelpTextSection.match(line).groups()
545 help_string = help_string.replace('"', '')
546 help_string = help_string.replace('\\', '_backsl_')
547 help_string = help_string.replace('_backsl_', '\\\\')
548 if help_string != "" and prev_help_string == '':
549 print(u'#: %s:%d\nmsgid ""\n"%s\\n"' % \
550 (relativePath(src, base), lineno+1, help_string), file=output)
552 elif help_string != "":
553 print(u'"%s\\n"' % help_string, file=output)
554 prev_help_string = help_string
557 print(u'"\\n"', file=output)
558 prev_help_string = 'xxxx'
559 elif HelpTextStart.match(line):
561 prev_help_string = ''
564 string = string.replace('"', '')
565 if string != "" and not inHelp:
566 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
567 (relativePath(src, base), lineno+1, string), file=output)
572 def formats_l10n(input_files, output, base):
573 '''Generate pot file from configure.py'''
574 output = io.open(output, 'w', encoding='utf_8', newline='\n')
575 GuiName = re.compile(r'.*\\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*', re.IGNORECASE)
576 GuiName2 = re.compile(r'.*\\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*', re.IGNORECASE)
577 input = io.open(input_files[0], encoding='utf_8')
578 for lineno, line in enumerate(input.readlines()):
581 if GuiName.match(line):
582 label = GuiName.match(line).group(1)
583 shortcut = GuiName.match(line).group(2).replace('"', '')
584 elif GuiName2.match(line):
585 label = GuiName2.match(line).group(1)
586 shortcut = GuiName2.match(line).group(2).replace('"', '')
589 label = label.replace('\\', '\\\\').replace('"', '')
591 labelsc = label + "|" + shortcut
593 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
594 (relativePath(input_files[0], base), lineno+1, label), file=output)
596 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
597 (relativePath(input_files[0], base), lineno+1, labelsc), file=output)
602 def encodings_l10n(input_files, output, base):
603 '''Generate pot file from lib/encodings'''
604 output = io.open(output, 'w', encoding='utf_8', newline='\n')
605 # assuming only one encodings file
606 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
607 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)^"]*)"\s+["\w-]+\s+(fixed|variable|variableunsafe)\s+\w+.*')
608 input = io.open(input_files[0], encoding='utf_8')
609 for lineno, line in enumerate(input.readlines()):
610 if not line.startswith('Encoding'):
613 guiname = reg.match(line).groups()[0]
615 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
616 (relativePath(input_files[0], base), lineno+1, guiname), file=output)
618 print("Error: Unable to handle line:")
620 # No need to abort if the parsing fails
626 def examples_templates_l10n(input_files, output, base):
627 '''Generate pot file from lib/templates and lib/examples'''
628 output = io.open(output, 'w', encoding='utf_8', newline='\n')
629 # only record each item once
631 inputfs = input_files[0].split()
633 parseExamplesTemplates(src, seen, output)
637 def parseExamplesTemplates(file, seen, output):
638 # Recursively iterate over subdirectories
639 if os.path.isdir(file):
640 for sfile in glob.glob( os.path.join(file, '*') ):
641 parseExamplesTemplates(sfile, seen, output)
643 filename = file.split(os.sep)[-1]
644 if os.path.isfile(file):
645 if filename[-4:] != ".lyx":
647 filename = filename[:-4]
648 if seen.count(filename) or filename[0].islower():
651 seen.append(filename)
653 print(u'#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
654 (relativePath(input_files[0], base), 0, filename.replace('_', ' ')), file=output)
659 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
663 path to the top source directory. default to '.'
665 output pot file, default to './lyx.pot'
667 filename that contains a list of input files in each line
670 layouts: lib/layouts/*
671 layouttranslations: create lib/layouttranslations from po/*.po and lib/layouts/*
673 languages: file lib/languages
674 latexfonts: file lib/latexfonts
675 encodings: file lib/encodings
676 external: external templates files
677 formats: formats predefined in lib/configure.py
678 examples_templates: example and template files
681 if __name__ == '__main__':
687 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
688 ['help', 'type=', 'output=', 'base=', 'src_file='])
689 for (opt, value) in optlist:
690 if opt in ['-h', '--help']:
693 elif opt in ['-o', '--output']:
695 elif opt in ['-b', '--base']:
697 elif opt in ['-t', '--type']:
699 elif opt in ['-s', '--src_file']:
700 input_files = [f.strip() for f in io.open(value, encoding='utf_8')]
702 if input_type not in ['ui', 'layouts', 'layouttranslations', 'qt4', 'languages', 'latexfonts', 'encodings', 'external', 'formats', 'examples_templates'] or output is None:
703 print('Wrong input type or output filename.')
708 # Ensure a unique sorting of input files and ignore the order in which they
709 # are given on the command line. This is important to avoid huge
710 # pseudo-diffs in the generated .pot file which would then end up in the
711 # .po files as well. We had this situation for years with people using
712 # different build systems to remerge .po files.
715 if input_type == 'ui':
716 ui_l10n(input_files, output, base)
717 elif input_type == 'latexfonts':
718 latexfonts_l10n(input_files, output, base)
719 elif input_type == 'layouts':
720 layouts_l10n(input_files, output, base, False)
721 elif input_type == 'layouttranslations':
722 layouts_l10n(input_files, output, base, True)
723 elif input_type == 'qt4':
724 qt4_l10n(input_files, output, base)
725 elif input_type == 'external':
726 external_l10n(input_files, output, base)
727 elif input_type == 'formats':
728 formats_l10n(input_files, output, base)
729 elif input_type == 'encodings':
730 encodings_l10n(input_files, output, base)
731 elif input_type == 'examples_templates':
732 examples_templates_l10n(input_files, output, base)
734 languages_l10n(input_files, output, base)