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 path3 = os.path.join(*path1[len(path2):]);
29 # replace all \ by / such that we get the same comments on Windows and *nix
30 path3 = path3.replace('\\', '/')
34 def ui_l10n(input_files, output, base):
35 '''Generate pot file from lib/ui/*'''
36 output = open(output, 'w')
37 Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"')
38 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
39 Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
40 for src in input_files:
42 for lineno, line in enumerate(input.readlines()):
43 if Submenu.match(line):
44 (string,) = Submenu.match(line).groups()
45 string = string.replace('_', ' ')
46 elif Toolbar.match(line):
47 (string,) = Toolbar.match(line).groups()
48 elif Item.match(line):
49 (string,) = Item.match(line).groups()
52 string = string.replace('\\', '\\\\').replace('"', '')
54 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
55 (relativePath(src, base), lineno+1, string)
60 def layouts_l10n(input_files, output, base):
61 '''Generate pot file from lib/layouts/*.layout and *.inc'''
62 output = open(output, 'w')
63 Style = re.compile(r'^Style\s+(.*)')
64 # include ???LabelString???, but exclude comment lines
65 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
66 GuiName = re.compile(r'\s*GuiName\s+(.*)')
67 ListName = re.compile(r'\s*ListName\s+(.*)')
68 for src in input_files:
70 for lineno, line in enumerate(input.readlines()):
72 (string,) = Style.match(line).groups()
73 string = string.replace('_', ' ')
74 elif LabelString.match(line):
75 (string,) = LabelString.match(line).groups()
76 elif GuiName.match(line):
77 (string,) = GuiName.match(line).groups()
78 elif ListName.match(line):
79 (string,) = ListName.match(line).groups()
82 string = string.replace('\\', '\\\\').replace('"', '')
84 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
85 (relativePath(src, base), lineno+1, string)
90 def qt4_l10n(input_files, output, base):
91 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
92 output = open(output, 'w')
93 pat = re.compile(r'\s*<string>(.*)</string>')
94 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
95 for src in input_files:
98 for lineno, line in enumerate(input.readlines()):
99 # skip the line after <property name=shortcut>
106 # get lines that match <string>...</string>
108 (string,) = pat.match(line).groups()
109 string = string.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', r'\"')
110 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
111 (relativePath(src, base), lineno+1, string)
116 def languages_l10n(input_files, output, base):
117 '''Generate pot file from lib/language'''
118 output = open(output, 'w')
119 # assuming only one language file
120 input = open(input_files[0])
121 for lineno, line in enumerate(input.readlines()):
124 items = line.split('"')
129 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
134 # I do not care extra "s like "af_ZA"
135 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % (relativePath(input_files[0], base), lineno+1, items[1])
141 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
145 path to the top source directory. default to '.'
147 output pot file, default to './lyx.pot'
150 layouts: lib/layouts/*
152 languages: file lib/languages
155 if __name__ == '__main__':
160 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
161 ['help', 'type=', 'output=', 'base='])
162 for (opt, value) in optlist:
163 if opt in ['-h', '--help']:
166 elif opt in ['-o', '--output']:
168 elif opt in ['-b', '--base']:
170 elif opt in ['-t', '--type']:
172 if input_type not in ['ui', 'layouts', 'qt4', 'languages'] or output is None:
173 print 'Wrong input type or output filename.'
175 if input_type == 'ui':
176 ui_l10n(args, output, base)
177 elif input_type == 'layouts':
178 layouts_l10n(args, output, base)
179 elif input_type == 'qt4':
180 qt4_l10n(args, output, base)
182 languages_l10n(args, output, base)