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
21 def relativePath(path, base):
22 '''return relative path from top source dir'''
23 # full pathname of path
24 path1 = os.path.normpath(os.path.realpath(path)).split(os.sep)
25 path2 = os.path.normpath(os.path.realpath(base)).split(os.sep)
26 if path1[:len(path2)] != path2:
27 print "Path %s is not under top source directory" % path
28 return os.path.join(*path1[len(path2):])
31 def ui_l10n(input_files, output, base):
32 '''Generate pot file from lib/ui/*'''
33 output = open(output, 'w')
34 Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"')
35 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
36 Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
37 for src in input_files:
39 for lineno, line in enumerate(input.readlines()):
40 if Submenu.match(line):
41 (string,) = Submenu.match(line).groups()
42 string = string.replace('_', ' ')
43 elif Toolbar.match(line):
44 (string,) = Toolbar.match(line).groups()
45 elif Item.match(line):
46 (string,) = Item.match(line).groups()
49 string = string.replace('\\', '\\\\').replace('"', '')
51 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
52 (relativePath(src, base), lineno+1, string)
57 def layouts_l10n(input_files, output, base):
58 '''Generate pot file from lib/layouts/*.layout and *.inc'''
59 output = open(output, 'w')
60 Style = re.compile(r'^Style\s+(.*)')
61 # include ???LabelString???, but exclude comment lines
62 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
63 GuiName = re.compile(r'\s*GuiName\s+(.*)')
64 ListName = re.compile(r'\s*ListName\s+(.*)')
65 for src in input_files:
67 for lineno, line in enumerate(input.readlines()):
69 (string,) = Style.match(line).groups()
70 string = string.replace('_', ' ')
71 elif LabelString.match(line):
72 (string,) = LabelString.match(line).groups()
73 elif GuiName.match(line):
74 (string,) = GuiName.match(line).groups()
75 elif ListName.match(line):
76 (string,) = ListName.match(line).groups()
79 string = string.replace('\\', '\\\\').replace('"', '')
81 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
82 (relativePath(src, base), lineno+1, string)
87 def qt4_l10n(input_files, output, base):
88 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
89 output = open(output, 'w')
90 pat = re.compile(r'\s*<string>(.*)</string>')
91 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
92 for src in input_files:
95 for lineno, line in enumerate(input.readlines()):
96 # skip the line after <property name=shortcut>
103 # get lines that match <string>...</string>
105 (string,) = pat.match(line).groups()
106 string = string.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', r'\"')
107 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
108 (relativePath(src, base), lineno+1, string)
113 def languages_l10n(input_files, output, base):
114 '''Generate pot file from lib/language'''
115 output = open(output, 'w')
116 # assuming only one language file
117 input = open(input_files[0])
118 for lineno, line in enumerate(input.readlines()):
121 items = line.split('"')
126 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
131 # I do not care extra "s like "af_ZA"
132 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % (relativePath(input_files[0], base), lineno+1, items[1])
138 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
142 path to the top source directory. default to '.'
144 output pot file, default to './lyx.pot'
147 layouts: lib/layouts/*
149 languages: file lib/languages
152 if __name__ == '__main__':
157 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
158 ['help', 'type=', 'output=', 'base='])
159 for (opt, value) in optlist:
160 if opt in ['-h', '--help']:
163 elif opt in ['-o', '--output']:
165 elif opt in ['-b', '--base']:
167 elif opt in ['-t', '--type']:
169 if input_type not in ['ui', 'layouts', 'qt4', 'languages'] or output is None:
170 print 'Wrong input type or output filename.'
172 if input_type == 'ui':
173 ui_l10n(args, output, base)
174 elif input_type == 'layouts':
175 layouts_l10n(args, output, base)
176 elif input_type == 'qt4':
177 qt4_l10n(args, output, base)
179 languages_l10n(args, output, base)