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 Popupmenu
= re
.compile(r
'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"')
39 Toolbar
= re
.compile(r
'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
40 Item
= re
.compile(r
'[^#]*Item\s+"([^"]*)"')
41 TableInsert
= re
.compile(r
'[^#]*TableInsert\s+"([^"]*)"')
42 for src
in input_files
:
44 for lineno
, line
in enumerate(input.readlines()):
45 if Submenu
.match(line
):
46 (string
,) = Submenu
.match(line
).groups()
47 string
= string
.replace('_', ' ')
48 elif Popupmenu
.match(line
):
49 (string
,) = Popupmenu
.match(line
).groups()
50 elif Toolbar
.match(line
):
51 (string
,) = Toolbar
.match(line
).groups()
52 elif Item
.match(line
):
53 (string
,) = Item
.match(line
).groups()
54 elif TableInsert
.match(line
):
55 (string
,) = TableInsert
.match(line
).groups()
58 string
= string
.replace('"', '')
60 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
61 (relativePath(src
, base
), lineno
+1, string
)
66 def layouts_l10n(input_files
, output
, base
):
67 '''Generate pot file from lib/layouts/*.layout and *.inc'''
68 output
= open(output
, 'w')
69 Style
= re
.compile(r
'^Style\s+(.*)')
70 # include ???LabelString???, but exclude comment lines
71 LabelString
= re
.compile(r
'^[^#]*LabelString\S*\s+(.*)')
72 GuiName
= re
.compile(r
'\s*GuiName\s+(.*)')
73 ListName
= re
.compile(r
'\s*ListName\s+(.*)')
74 for src
in input_files
:
76 for lineno
, line
in enumerate(input.readlines()):
78 (string
,) = Style
.match(line
).groups()
79 string
= string
.replace('_', ' ')
80 elif LabelString
.match(line
):
81 (string
,) = LabelString
.match(line
).groups()
82 elif GuiName
.match(line
):
83 (string
,) = GuiName
.match(line
).groups()
84 elif ListName
.match(line
):
85 (string
,) = ListName
.match(line
).groups()
88 string
= string
.replace('\\', '\\\\').replace('"', '')
90 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
91 (relativePath(src
, base
), lineno
+1, string
)
96 def qt4_l10n(input_files
, output
, base
):
97 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
98 output
= open(output
, 'w')
99 pat
= re
.compile(r
'\s*<string>(.*)</string>')
100 prop
= re
.compile(r
'\s*<property.*name.*=.*shortcut')
101 for src
in input_files
:
104 for lineno
, line
in enumerate(input.readlines()):
105 # skip the line after <property name=shortcut>
112 # get lines that match <string>...</string>
114 (string
,) = pat
.match(line
).groups()
115 string
= string
.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', r
'\"')
116 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
117 (relativePath(src
, base
), lineno
+1, string
)
122 def languages_l10n(input_files
, output
, base
):
123 '''Generate pot file from lib/language'''
124 output
= open(output
, 'w')
125 # assuming only one language file
126 reg
= re
.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\)]+)"\s+(true|false)\s+[\w-]+\s+\w+\s+"[^"]*"')
127 input = open(input_files
[0])
128 for lineno
, line
in enumerate(input.readlines()):
132 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
138 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
139 (relativePath(input_files
[0], base
), lineno
+1, reg
.match(line
).groups()[0])
141 print "Error: Unable to handle line:"
148 def external_l10n(input_files
, output
, base
):
149 '''Generate pot file from lib/external_templates'''
150 output
= open(output
, 'w')
151 Template
= re
.compile(r
'^Template\s+(.*)')
152 GuiName
= re
.compile(r
'\s*GuiName\s+(.*)')
153 HelpTextStart
= re
.compile(r
'\s*HelpText\s')
154 HelpTextSection
= re
.compile(r
'\s*(\S.*)\s*$')
155 HelpTextEnd
= re
.compile(r
'\s*HelpTextEnd\s')
157 for src
in input_files
:
161 prev_help_string
= ''
162 for lineno
, line
in enumerate(input.readlines()):
163 if Template
.match(line
):
164 (string
,) = Template
.match(line
).groups()
165 elif GuiName
.match(line
):
166 (string
,) = GuiName
.match(line
).groups()
168 if HelpTextEnd
.match(line
):
170 print >> output
, '\nmsgstr ""\n'
173 prev_help_string
= ''
174 elif HelpTextSection
.match(line
):
175 (help_string
,) = HelpTextSection
.match(line
).groups()
176 help_string
= help_string
.replace('"', '')
177 if help_string
!= "" and prev_help_string
== '':
178 print >> output
, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
179 (relativePath(src
, base
), lineno
+1, help_string
)
181 elif help_string
!= "":
182 print >> output
, '"%s\\n"' % help_string
183 prev_help_string
= help_string
184 elif HelpTextStart
.match(line
):
186 prev_help_string
= ''
189 string
= string
.replace('"', '')
190 if string
!= "" and not inHelp
:
191 print >> output
, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
192 (relativePath(src
, base
), lineno
+1, string
)
198 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
202 path to the top source directory. default to '.'
204 output pot file, default to './lyx.pot'
207 layouts: lib/layouts/*
209 languages: file lib/languages
210 external: external templates file
213 if __name__
== '__main__':
218 optlist
, args
= getopt
.getopt(sys
.argv
[1:], 'ht:o:b:',
219 ['help', 'type=', 'output=', 'base='])
220 for (opt
, value
) in optlist
:
221 if opt
in ['-h', '--help']:
224 elif opt
in ['-o', '--output']:
226 elif opt
in ['-b', '--base']:
228 elif opt
in ['-t', '--type']:
230 if input_type
not in ['ui', 'layouts', 'qt4', 'languages', 'external'] or output
is None:
231 print 'Wrong input type or output filename.'
233 if input_type
== 'ui':
234 ui_l10n(args
, output
, base
)
235 elif input_type
== 'layouts':
236 layouts_l10n(args
, output
, base
)
237 elif input_type
== 'qt4':
238 qt4_l10n(args
, output
, base
)
239 elif input_type
== 'external':
240 external_l10n(args
, output
, base
)
242 languages_l10n(args
, output
, base
)