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 sys, os, re, getopt
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('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
41 (relativePath(infile, basefile), lineno, string), file=outfile)
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('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
74 (relativePath(src, base), lineno+1, string), file=output)
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 OutlinerName = re.compile(r'^[^#]*OutlinerName\s+(\S+|\"[^\"]*\")\s+(\S+|\"[^\"]*\")\s*$', re.IGNORECASE)
88 Tooltip = re.compile(r'^\s*Tooltip\S*\s+(.*\S)\s*$', re.IGNORECASE)
89 GuiName = re.compile(r'^\s*GuiName\s+(.*\S)\s*$', re.IGNORECASE)
90 ListName = re.compile(r'^\s*ListName\s+(.*\S)\s*$', re.IGNORECASE)
91 CategoryName = re.compile(r'^\s*Category\s+(.*\S)\s*$', re.IGNORECASE)
92 NameRE = re.compile(r'^\s*#\s*\\DeclareLyXModule.*{(.*)}$', re.IGNORECASE)
93 InsetLayout = re.compile(r'^InsetLayout\s+\"?(.*)\"?\s*$', re.IGNORECASE)
94 FlexCheck = re.compile(r'^Flex:(.*)', re.IGNORECASE)
95 CaptionCheck = re.compile(r'^Caption:(.*)', re.IGNORECASE)
96 DescBegin = re.compile(r'^\s*#DescriptionBegin\s*$', re.IGNORECASE)
97 DescEnd = re.compile(r'^\s*#\s*DescriptionEnd\s*$', re.IGNORECASE)
98 Category = re.compile(r'^\s*#\s*Category:\s+(.*\S)\s*$', re.IGNORECASE)
99 I18nPreamble = re.compile(r'^\s*((Lang)|(Babel))Preamble\s*$', re.IGNORECASE)
100 EndI18nPreamble = re.compile(r'^\s*End((Lang)|(Babel))Preamble\s*$', re.IGNORECASE)
101 I18nString = re.compile(r'_\(([^\)]+)\)')
102 CounterFormat = re.compile(r'^\s*PrettyFormat\s+"?(.*)"?\s*$', re.IGNORECASE)
103 CiteFormat = re.compile(r'^\s*CiteFormat', re.IGNORECASE)
104 KeyVal = re.compile(r'^\s*_\w+\s+(.*\S)\s*$')
105 Float = re.compile(r'^\s*Float\s*$', re.IGNORECASE)
106 UsesFloatPkg = re.compile(r'^\s*UsesFloatPkg\s+(.*\S)\s*$', re.IGNORECASE)
107 IsPredefined = re.compile(r'^\s*IsPredefined\s+(.*\S)\s*$', re.IGNORECASE)
108 End = re.compile(r'^\s*End', re.IGNORECASE)
109 Comment = re.compile(r'^(.*)#')
110 Translation = re.compile(r'^\s*Translation\s+(.*\S)\s*$', re.IGNORECASE)
111 KeyValPair = re.compile(r'\s*"(.*)"\s+"(.*)"')
117 if layouttranslations:
118 linguas_file = os.path.join(base, 'po/LINGUAS')
119 for line in open(linguas_file).readlines():
120 res = Comment.search(line)
123 if line.strip() != '':
124 languages.extend(line.split())
126 # read old translations if available
130 for line in input.readlines():
131 res = Comment.search(line)
134 if line.strip() == '':
136 res = Translation.search(line)
139 if lang not in languages:
140 oldlanguages.append(lang)
141 languages.append(lang)
142 oldtrans[lang] = dict()
144 res = End.search(line)
148 res = KeyValPair.search(line)
149 if res and lang != '':
150 key = res.group(1).decode('utf-8')
151 val = res.group(2).decode('utf-8')
152 key = key.replace('\\"', '"').replace('\\\\', '\\')
153 val = val.replace('\\"', '"').replace('\\\\', '\\')
154 oldtrans[lang][key] = val
157 print("Error: Unable to handle line:")
160 print("Warning: Unable to open %s for reading." % output)
161 print(" Old translations will be lost.")
163 # walon is not a known document language
164 # FIXME: Do not hardcode, read from lib/languages!
165 if 'wa' in languages:
166 languages.remove('wa')
168 out = open(output, 'w')
169 for src in input_files:
170 readingDescription = False
171 readingI18nPreamble = False
173 readingCiteFormats = False
181 for line in open(src).readlines():
183 res = ClassDescription.search(line)
185 string = res.group(2)
186 if not layouttranslations:
187 writeString(out, src, base, lineno + 1, string)
189 res = ClassCategory.search(line)
191 string = res.group(1)
192 if not layouttranslations:
193 writeString(out, src, base, lineno + 1, string)
195 if readingDescription:
196 res = DescEnd.search(line)
198 readingDescription = False
199 desc = " ".join(descLines)
200 if not layouttranslations:
201 writeString(out, src, base, lineno + 1, desc)
203 descLines.append(line[1:].strip())
205 res = DescBegin.search(line)
207 readingDescription = True
208 descStartLine = lineno
210 if readingI18nPreamble:
211 res = EndI18nPreamble.search(line)
213 readingI18nPreamble = False
215 res = I18nString.search(line)
217 string = res.group(1)
218 if layouttranslations:
221 writeString(out, src, base, lineno, string)
223 res = I18nPreamble.search(line)
225 readingI18nPreamble = True
227 res = NameRE.search(line)
229 string = res.group(1)
230 if not layouttranslations:
231 writeString(out, src, base, lineno + 1, string)
233 res = Style.search(line)
235 string = res.group(1)
236 string = string.replace('_', ' ')
237 # Style means something else inside a float definition
239 if not layouttranslations:
240 writeString(out, src, base, lineno, string)
242 res = LabelString.search(line)
244 string = res.group(1)
245 if not layouttranslations:
246 writeString(out, src, base, lineno, string)
248 res = MenuString.search(line)
250 string = res.group(1)
251 if not layouttranslations:
252 writeString(out, src, base, lineno, string)
254 res = OutlinerName.search(line)
256 string = res.group(2)
257 if not layouttranslations:
258 writeString(out, src, base, lineno, string)
260 res = Tooltip.search(line)
262 string = res.group(1)
263 if not layouttranslations:
264 writeString(out, src, base, lineno, string)
266 res = GuiName.search(line)
268 string = res.group(1)
269 if layouttranslations:
270 # gui name must only be added for floats
274 writeString(out, src, base, lineno, string)
276 res = CategoryName.search(line)
278 string = res.group(1)
279 if not layouttranslations:
280 writeString(out, src, base, lineno, string)
282 res = ListName.search(line)
284 string = res.group(1)
285 if layouttranslations:
286 listname = string.strip('"')
288 writeString(out, src, base, lineno, string)
290 res = InsetLayout.search(line)
292 string = res.group(1)
293 string = string.replace('_', ' ')
294 #Flex:xxx is not used in translation
295 #if not layouttranslations:
296 # writeString(out, src, base, lineno, string)
297 m = FlexCheck.search(string)
299 if not layouttranslations:
300 writeString(out, src, base, lineno, m.group(1))
301 m = CaptionCheck.search(string)
303 if not layouttranslations:
304 writeString(out, src, base, lineno, m.group(1))
306 res = Category.search(line)
308 string = res.group(1)
309 if not layouttranslations:
310 writeString(out, src, base, lineno, string)
312 res = CounterFormat.search(line)
314 string = res.group(1)
315 if not layouttranslations:
316 writeString(out, src, base, lineno, string)
318 res = Float.search(line)
322 res = IsPredefined.search(line)
324 string = res.group(1).lower()
330 res = UsesFloatPkg.search(line)
332 string = res.group(1).lower()
338 res = CiteFormat.search(line)
340 readingCiteFormats = True
342 res = End.search(line)
344 # We have four combinations of the flags usesFloatPkg and isPredefined:
345 # usesFloatPkg and isPredefined: might use standard babel translations
346 # usesFloatPkg and not isPredefined: does not use standard babel translations
347 # not usesFloatPkg and isPredefined: uses standard babel translations
348 # not usesFloatPkg and not isPredefined: not supported by LyX
349 # The third combination is even true for MarginFigure, MarginTable (both from
350 # tufte-book.layout) and Planotable, Plate (both from aguplus.inc).
351 if layouttranslations and readingFloat and usesFloatPkg:
353 keyset.add(floatname)
360 readingCiteFormats = False
363 if readingCiteFormats:
364 res = KeyVal.search(line)
367 if not layouttranslations:
368 writeString(out, src, base, lineno, val)
370 if layouttranslations:
371 # Extract translations of layout files
374 # Sort languages and key to minimize the diff between different runs
375 # with changed translations
382 ContextRe = re.compile(r'(.*)(\[\[.*\]\])')
384 print('''# This file has been automatically generated by po/lyx_pot.py.
385 # PLEASE MODIFY ONLY THE LAGUAGES HAVING NO .po FILE! If you want to regenerate
386 # this file from the translations, run `make ../lib/layouttranslations' in po.
387 # Python polib library is needed for building the output file.
389 # This file should remain fixed during minor LyX releases.
390 # For more comments see README.localization file.''', file=out)
391 for lang in languages:
392 print('\nTranslation %s' % lang, file=out)
393 if lang in list(oldtrans.keys()):
394 trans = oldtrans[lang]
397 if not lang in oldlanguages:
398 poname = os.path.join(base, 'po/' + lang + '.po')
399 po = polib.pofile(poname)
400 # Iterate through po entries and not keys for speed reasons.
401 # FIXME: The code is still too slow
403 if not entry.translated():
405 if entry.msgid in keys:
408 # some translators keep untranslated entries
412 if key in list(trans.keys()):
413 val = trans[key].replace('\\', '\\\\').replace('"', '\\"')
414 res = ContextRe.search(val)
417 key = key.replace('\\', '\\\\').replace('"', '\\"')
418 print('\t"%s" "%s"' % \
419 (key.encode('utf-8'), val.encode('utf-8')), file=out)
420 # also print untranslated entries to help translators
421 elif not lang in oldlanguages:
422 key = key.replace('\\', '\\\\').replace('"', '\\"')
423 res = ContextRe.search(key)
428 print('\t"%s" "%s"' % \
429 (key.encode('utf-8'), val.encode('utf-8')), file=out)
430 print('End', file=out)
435 def qt4_l10n(input_files, output, base):
436 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
437 output = open(output, 'w')
438 pat = re.compile(r'\s*<string>(.*)</string>')
439 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
440 for src in input_files:
443 for lineno, line in enumerate(input.readlines()):
444 # skip the line after <property name=shortcut>
451 # get lines that match <string>...</string>
453 (string,) = pat.match(line).groups()
454 string = string.replace('&', '&').replace('"', '"')
455 string = string.replace('<', '<').replace('>', '>')
456 string = string.replace('\\', '\\\\').replace('"', r'\"')
457 string = string.replace('
', r'\n')
458 print('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
459 (relativePath(src, base), lineno+1, string), file=output)
464 def languages_l10n(input_files, output, base):
465 '''Generate pot file from lib/languages'''
466 out = open(output, 'w')
467 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)', re.IGNORECASE)
469 for src in input_files:
473 for line in open(src).readlines():
475 res = GuiName.search(line)
477 string = res.group(1)
478 writeString(out, src, base, lineno, string)
484 def latexfonts_l10n(input_files, output, base):
485 '''Generate pot file from lib/latexfonts'''
486 out = open(output, 'w')
487 GuiName = re.compile(r'^[^#]*GuiName\s+(.*)', re.IGNORECASE)
489 for src in input_files:
493 for line in open(src).readlines():
495 res = GuiName.search(line)
497 string = res.group(1)
498 writeString(out, src, base, lineno, string)
504 def external_l10n(input_files, output, base):
505 '''Generate pot file from lib/external_templates'''
506 output = open(output, 'w')
507 Template = re.compile(r'^Template\s+(.*)', re.IGNORECASE)
508 GuiName = re.compile(r'\s*GuiName\s+(.*)', re.IGNORECASE)
509 HelpTextStart = re.compile(r'\s*HelpText\s', re.IGNORECASE)
510 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
511 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s', re.IGNORECASE)
513 for src in input_files:
517 prev_help_string = ''
518 for lineno, line in enumerate(input.readlines()):
519 if Template.match(line):
520 (string,) = Template.match(line).groups()
521 elif GuiName.match(line):
522 (string,) = GuiName.match(line).groups()
524 if HelpTextEnd.match(line):
526 print('\nmsgstr ""\n', file=output)
529 prev_help_string = ''
530 elif HelpTextSection.match(line):
531 (help_string,) = HelpTextSection.match(line).groups()
532 help_string = help_string.replace('"', '')
533 if help_string != "" and prev_help_string == '':
534 print('#: %s:%d\nmsgid ""\n"%s\\n"' % \
535 (relativePath(src, base), lineno+1, help_string), file=output)
537 elif help_string != "":
538 print('"%s\\n"' % help_string, file=output)
539 prev_help_string = help_string
540 elif HelpTextStart.match(line):
542 prev_help_string = ''
545 string = string.replace('"', '')
546 if string != "" and not inHelp:
547 print('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
548 (relativePath(src, base), lineno+1, string), file=output)
553 def formats_l10n(input_files, output, base):
554 '''Generate pot file from configure.py'''
555 output = open(output, 'w')
556 GuiName = re.compile(r'.*\\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*', re.IGNORECASE)
557 GuiName2 = re.compile(r'.*\\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*', re.IGNORECASE)
558 input = open(input_files[0])
559 for lineno, line in enumerate(input.readlines()):
562 if GuiName.match(line):
563 label = GuiName.match(line).group(1)
564 shortcut = GuiName.match(line).group(2).replace('"', '')
565 elif GuiName2.match(line):
566 label = GuiName2.match(line).group(1)
567 shortcut = GuiName2.match(line).group(2).replace('"', '')
570 label = label.replace('\\', '\\\\').replace('"', '')
572 labelsc = label + "|" + shortcut
574 print('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
575 (relativePath(input_files[0], base), lineno+1, label), file=output)
577 print('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
578 (relativePath(input_files[0], base), lineno+1, labelsc), file=output)
583 def encodings_l10n(input_files, output, base):
584 '''Generate pot file from lib/encodings'''
585 output = open(output, 'w')
586 # assuming only one encodings file
587 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
588 reg = re.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable|variableunsafe)\s+\w+.*')
589 input = open(input_files[0])
590 for lineno, line in enumerate(input.readlines()):
591 if not line.startswith('Encoding'):
594 print('#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
595 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0]), file=output)
597 print("Error: Unable to handle line:")
599 # No need to abort if the parsing fails
607 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
611 path to the top source directory. default to '.'
613 output pot file, default to './lyx.pot'
615 filename that contains a list of input files in each line
618 layouts: lib/layouts/*
619 layouttranslations: create lib/layouttranslations from po/*.po and lib/layouts/*
621 languages: file lib/languages
622 latexfonts: file lib/latexfonts
623 encodings: file lib/encodings
624 external: external templates file
625 formats: formats predefined in lib/configure.py
628 if __name__ == '__main__':
634 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:s:',
635 ['help', 'type=', 'output=', 'base=', 'src_file='])
636 for (opt, value) in optlist:
637 if opt in ['-h', '--help']:
640 elif opt in ['-o', '--output']:
642 elif opt in ['-b', '--base']:
644 elif opt in ['-t', '--type']:
646 elif opt in ['-s', '--src_file']:
647 input_files = [f.strip() for f in open(value)]
649 if input_type not in ['ui', 'layouts', 'layouttranslations', 'qt4', 'languages', 'latexfonts', 'encodings', 'external', 'formats'] or output is None:
650 print('Wrong input type or output filename.')
655 if input_type == 'ui':
656 ui_l10n(input_files, output, base)
657 elif input_type == 'latexfonts':
658 latexfonts_l10n(input_files, output, base)
659 elif input_type == 'layouts':
660 layouts_l10n(input_files, output, base, False)
661 elif input_type == 'layouttranslations':
662 layouts_l10n(input_files, output, base, True)
663 elif input_type == 'qt4':
664 qt4_l10n(input_files, output, base)
665 elif input_type == 'external':
666 external_l10n(input_files, output, base)
667 elif input_type == 'formats':
668 formats_l10n(input_files, output, base)
669 elif input_type == 'encodings':
670 encodings_l10n(input_files, output, base)
672 languages_l10n(input_files, output, base)