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 writeString(outfile
, infile
, basefile
, lineno
, string
):
35 string
= string
.replace('\\', '\\\\').replace('"', '')
38 print >> outfile
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
39 (relativePath(infile
, basefile
), lineno
, string
)
42 def ui_l10n(input_files
, output
, base
):
43 '''Generate pot file from lib/ui/*'''
44 output
= open(output
, 'w')
45 Submenu
= re
.compile(r
'^[^#]*Submenu\s+"([^"]*)"')
46 Popupmenu
= re
.compile(r
'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"')
47 Toolbar
= re
.compile(r
'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
48 Item
= re
.compile(r
'[^#]*Item\s+"([^"]*)"')
49 TableInsert
= re
.compile(r
'[^#]*TableInsert\s+"([^"]*)"')
50 for src
in input_files
:
52 for lineno
, line
in enumerate(input.readlines()):
53 if Submenu
.match(line
):
54 (string
,) = Submenu
.match(line
).groups()
55 string
= string
.replace('_', ' ')
56 elif Popupmenu
.match(line
):
57 (string
,) = Popupmenu
.match(line
).groups()
58 elif Toolbar
.match(line
):
59 (string
,) = Toolbar
.match(line
).groups()
60 elif Item
.match(line
):
61 (string
,) = Item
.match(line
).groups()
62 elif TableInsert
.match(line
):
63 (string
,) = TableInsert
.match(line
).groups()
66 string
= string
.replace('"', '')
68 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
69 (relativePath(src
, base
), lineno
+1, string
)
74 def layouts_l10n(input_files
, output
, base
):
75 '''Generate pot file from lib/layouts/*.{layout,inc,module}'''
76 out
= open(output
, 'w')
77 Style
= re
.compile(r
'^Style\s+(.*)')
78 # include ???LabelString???, but exclude comment lines
79 LabelString
= re
.compile(r
'^[^#]*LabelString\S*\s+(.*)')
80 GuiName
= re
.compile(r
'\s*GuiName\s+(.*)')
81 ListName
= re
.compile(r
'\s*ListName\s+(.*)')
82 CategoryName
= re
.compile(r
'\s*Category\s+(.*)')
83 NameRE
= re
.compile(r
'DeclareLyXModule.*{(.*)}')
84 InsetLayout
= re
.compile(r
'^InsetLayout\s+(.*)')
85 DescBegin
= re
.compile(r
'#+\s*DescriptionBegin\s*$')
86 DescEnd
= re
.compile(r
'#+\s*DescriptionEnd\s*$')
87 Category
= re
.compile(r
'#Category: (.*)$')
88 I18nPreamble
= re
.compile(r
'\s*(Lang)|(Babel)Preamble\s*$')
89 EndI18nPreamble
= re
.compile(r
'\s*End(Lang)|(Babel)Preamble\s*$')
90 I18nString
= re
.compile(r
'_\(([^\)]+)\)')
92 for src
in input_files
:
93 readingDescription
= False
94 readingI18nPreamble
= False
98 for line
in open(src
).readlines():
100 if readingDescription
:
101 res
= DescEnd
.search(line
)
103 readingDescription
= False
104 desc
= " ".join(descLines
)
105 writeString(out
, src
, base
, lineno
+ 1, desc
)
107 descLines
.append(line
[1:].strip())
109 res
= DescBegin
.search(line
)
111 readingDescription
= True
112 descStartLine
= lineno
114 if readingI18nPreamble
:
115 res
= EndI18nPreamble
.search(line
)
117 readingI18nPreamble
= False
119 res
= I18nString
.search(line
)
121 string
= res
.group(1)
122 writeString(out
, src
, base
, lineno
, string
)
124 res
= I18nPreamble
.search(line
)
126 readingI18nPreamble
= True
128 res
= NameRE
.search(line
)
130 string
= res
.group(1)
131 string
= string
.replace('\\', '\\\\').replace('"', '')
133 print >> out
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
134 (relativePath(src
, base
), lineno
+ 1, string
)
136 res
= Style
.search(line
)
138 string
= res
.group(1)
139 string
= string
.replace('_', ' ')
140 writeString(out
, src
, base
, lineno
, string
)
142 res
= LabelString
.search(line
)
144 string
= res
.group(1)
145 writeString(out
, src
, base
, lineno
, string
)
147 res
= GuiName
.search(line
)
149 string
= res
.group(1)
150 writeString(out
, src
, base
, lineno
, string
)
152 res
= CategoryName
.search(line
)
154 string
= res
.group(1)
155 writeString(out
, src
, base
, lineno
, string
)
157 res
= ListName
.search(line
)
159 string
= res
.group(1)
160 writeString(out
, src
, base
, lineno
, string
)
162 res
= InsetLayout
.search(line
)
164 string
= res
.group(1)
165 string
= string
.replace('_', ' ')
166 writeString(out
, src
, base
, lineno
, string
)
168 res
= Category
.search(line
)
170 string
= res
.group(1)
171 writeString(out
, src
, base
, lineno
, string
)
176 def qt4_l10n(input_files
, output
, base
):
177 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
178 output
= open(output
, 'w')
179 pat
= re
.compile(r
'\s*<string>(.*)</string>')
180 prop
= re
.compile(r
'\s*<property.*name.*=.*shortcut')
181 for src
in input_files
:
184 for lineno
, line
in enumerate(input.readlines()):
185 # skip the line after <property name=shortcut>
192 # get lines that match <string>...</string>
194 (string
,) = pat
.match(line
).groups()
195 string
= string
.replace('&', '&').replace('<', '<').replace('>', '>')
196 string
= string
.replace('\\', '\\\\').replace('"', r
'\"')
197 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
198 (relativePath(src
, base
), lineno
+1, string
)
203 def languages_l10n(input_files
, output
, base
):
204 '''Generate pot file from lib/language'''
205 output
= open(output
, 'w')
206 # assuming only one language file
207 reg
= re
.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\),]+)"\s+(true|false)\s+[\w-]+\s+\w+\s+"[^"]*"')
208 input = open(input_files
[0])
209 for lineno
, line
in enumerate(input.readlines()):
213 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
219 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
220 (relativePath(input_files
[0], base
), lineno
+1, reg
.match(line
).groups()[0])
222 print "Error: Unable to handle line:"
224 # No need to abort if the parsing fails (e.g. "ignore" language has no encoding)
230 def external_l10n(input_files
, output
, base
):
231 '''Generate pot file from lib/external_templates'''
232 output
= open(output
, 'w')
233 Template
= re
.compile(r
'^Template\s+(.*)')
234 GuiName
= re
.compile(r
'\s*GuiName\s+(.*)')
235 HelpTextStart
= re
.compile(r
'\s*HelpText\s')
236 HelpTextSection
= re
.compile(r
'\s*(\S.*)\s*$')
237 HelpTextEnd
= re
.compile(r
'\s*HelpTextEnd\s')
239 for src
in input_files
:
243 prev_help_string
= ''
244 for lineno
, line
in enumerate(input.readlines()):
245 if Template
.match(line
):
246 (string
,) = Template
.match(line
).groups()
247 elif GuiName
.match(line
):
248 (string
,) = GuiName
.match(line
).groups()
250 if HelpTextEnd
.match(line
):
252 print >> output
, '\nmsgstr ""\n'
255 prev_help_string
= ''
256 elif HelpTextSection
.match(line
):
257 (help_string
,) = HelpTextSection
.match(line
).groups()
258 help_string
= help_string
.replace('"', '')
259 if help_string
!= "" and prev_help_string
== '':
260 print >> output
, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
261 (relativePath(src
, base
), lineno
+1, help_string
)
263 elif help_string
!= "":
264 print >> output
, '"%s\\n"' % help_string
265 prev_help_string
= help_string
266 elif HelpTextStart
.match(line
):
268 prev_help_string
= ''
271 string
= string
.replace('"', '')
272 if string
!= "" and not inHelp
:
273 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
274 (relativePath(src
, base
), lineno
+1, string
)
279 def formats_l10n(input_files
, output
, base
):
280 '''Generate pot file from configure.py'''
281 output
= open(output
, 'w')
282 GuiName
= re
.compile(r
'.*\Format\s+\S+\s+\S+\s+"([^"]*)"\s+(\S*)\s+.*')
283 GuiName2
= re
.compile(r
'.*\Format\s+\S+\s+\S+\s+([^"]\S+)\s+(\S*)\s+.*')
284 input = open(input_files
[0])
285 for lineno
, line
in enumerate(input.readlines()):
288 if GuiName
.match(line
):
289 label
= GuiName
.match(line
).group(1)
290 shortcut
= GuiName
.match(line
).group(2).replace('"', '')
291 elif GuiName2
.match(line
):
292 label
= GuiName2
.match(line
).group(1)
293 shortcut
= GuiName2
.match(line
).group(2).replace('"', '')
296 label
= label
.replace('\\', '\\\\').replace('"', '')
298 labelsc
= label
+ "|" + shortcut
300 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
301 (relativePath(input_files
[0], base
), lineno
+1, label
)
303 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
304 (relativePath(input_files
[0], base
), lineno
+1, labelsc
)
309 def encodings_l10n(input_files
, output
, base
):
310 '''Generate pot file from lib/encodings'''
311 output
= open(output
, 'w')
312 # assuming only one encodings file
313 # Encoding utf8 utf8 "Unicode (utf8)" UTF-8 variable inputenc
314 reg
= re
.compile('Encoding [\w-]+\s+[\w-]+\s+"([\w \-\(\)]+)"\s+[\w-]+\s+(fixed|variable)\s+\w+.*')
315 input = open(input_files
[0])
316 for lineno
, line
in enumerate(input.readlines()):
317 if not line
.startswith('Encoding'):
320 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
321 (relativePath(input_files
[0], base
), lineno
+1, reg
.match(line
).groups()[0])
323 print "Error: Unable to handle line:"
325 # No need to abort if the parsing fails
333 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] [-s|src_file filename] -t|--type input_type input_files
337 path to the top source directory. default to '.'
339 output pot file, default to './lyx.pot'
341 filename that contains a list of input files in each line
344 layouts: lib/layouts/*
346 languages: file lib/languages
347 encodings: file lib/encodings
348 external: external templates file
349 formats: formats predefined in lib/configure.py
352 if __name__
== '__main__':
358 optlist
, args
= getopt
.getopt(sys
.argv
[1:], 'ht:o:b:s:',
359 ['help', 'type=', 'output=', 'base=', 'src_file='])
360 for (opt
, value
) in optlist
:
361 if opt
in ['-h', '--help']:
364 elif opt
in ['-o', '--output']:
366 elif opt
in ['-b', '--base']:
368 elif opt
in ['-t', '--type']:
370 elif opt
in ['-s', '--src_file']:
371 input_files
= [f
.strip() for f
in open(value
)]
373 if input_type
not in ['ui', 'layouts', 'modules', 'qt4', 'languages', 'encodings', 'external', 'formats'] or output
is None:
374 print 'Wrong input type or output filename.'
379 if input_type
== 'ui':
380 ui_l10n(input_files
, output
, base
)
381 elif input_type
== 'layouts':
382 layouts_l10n(input_files
, output
, base
)
383 elif input_type
== 'qt4':
384 qt4_l10n(input_files
, output
, base
)
385 elif input_type
== 'external':
386 external_l10n(input_files
, output
, base
)
387 elif input_type
== 'formats':
388 formats_l10n(input_files
, output
, base
)
389 elif input_type
== 'encodings':
390 encodings_l10n(input_files
, output
, base
)
392 languages_l10n(input_files
, output
, base
)