]> git.lyx.org Git - features.git/blob - po/lyx_pot.py
* po/lyx_pot.py:
[features.git] / po / lyx_pot.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file lyx_pot.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7 #
8 # \author Bo Peng
9 #
10 # Full author contact details are available in file CREDITS
11
12 # Usage: use
13 #     lyx_pot.py -h
14 # to get usage message
15
16 # This script will extract translatable strings from input files and write
17 # to output in gettext .pot format.
18 #
19 import sys, os, re, getopt
20
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('\\', '/')
31     return path3
32
33
34 def writeString(outfile, infile, basefile, lineno, string):
35     string = string.replace('\\', '\\\\').replace('"', '')
36     if string == "":
37         return
38     print >> outfile, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
39         (relativePath(infile, basefile), lineno, string)
40
41
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:
51         input = open(src)
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()
64             else:
65                 continue
66             string = string.replace('"', '')
67             if string != "":
68                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
69                     (relativePath(src, base), lineno+1, string)
70         input.close()
71     output.close()
72
73
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     DescBegin = re.compile(r'#+\s*DescriptionBegin\s*$')
85     DescEnd = re.compile(r'#+\s*DescriptionEnd\s*$')
86
87     for src in input_files:
88         readingDescription = False
89         descStartLine = -1
90         descLines = []
91         lineno = 0
92         for line in open(src).readlines():
93             lineno += 1
94             if readingDescription:
95                 res = DescEnd.search(line)
96                 if res != None:
97                     readingDescription = False
98                     desc = " ".join(descLines)
99                     print >> out, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
100                         (relativePath(src, base), lineno + 1, desc)
101                     continue
102                 descLines.append(line[1:].strip())
103                 continue
104             res = DescBegin.search(line)
105             if res != None:
106                 readingDescription = True
107                 descStartLine = lineno
108                 continue
109             res = NameRE.search(line)
110             if res != None:
111                 string = res.group(1)
112                 string = string.replace('\\', '\\\\').replace('"', '')
113                 if string != "":
114                     print >> out, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
115                         (relativePath(src, base), lineno + 1, string)
116                 continue
117             res = Style.search(line)
118             if res != None:
119                 string = res.group(1)
120                 string = string.replace('_', ' ')
121                 writeString(out, src, base, lineno, string)
122                 continue
123             res = LabelString.search(line)
124             if res != None:
125                 string = res.group(1)
126                 writeString(out, src, base, lineno, string)
127                 continue
128             res = GuiName.search(line)
129             if res != None:
130                 string = res.group(1)
131                 writeString(out, src, base, lineno, string)
132                 continue
133             res = CategoryName.search(line)
134             if res != None:
135                 string = res.group(1)
136                 writeString(out, src, base, lineno, string)
137                 continue
138             res = ListName.search(line)
139             if res != None:
140                 string = res.group(1)
141                 writeString(out, src, base, lineno, string)
142                 continue
143     out.close()
144
145
146 def qt4_l10n(input_files, output, base):
147     '''Generate pot file from src/frontends/qt4/ui/*.ui'''
148     output = open(output, 'w')
149     pat = re.compile(r'\s*<string>(.*)</string>')
150     prop = re.compile(r'\s*<property.*name.*=.*shortcut')
151     for src in input_files:
152         input = open(src)
153         skipNextLine = False
154         for lineno, line in enumerate(input.readlines()):
155             # skip the line after <property name=shortcut>
156             if skipNextLine:
157                 skipNextLine = False
158                 continue
159             if prop.match(line):
160                 skipNextLine = True
161                 continue
162             # get lines that match <string>...</string>
163             if pat.match(line):
164                 (string,) = pat.match(line).groups()
165                 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('"', r'\"')
166                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
167                     (relativePath(src, base), lineno+1, string) 
168         input.close()
169     output.close()
170
171
172 def languages_l10n(input_files, output, base):
173     '''Generate pot file from lib/language'''
174     output = open(output, 'w')
175     # assuming only one language file
176     reg = re.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\)]+)"\s+(true|false)\s+[\w-]+\s+\w+\s+"[^"]*"')
177     input = open(input_files[0])
178     for lineno, line in enumerate(input.readlines()):
179         if line[0] == '#':
180             continue
181         # From:
182         #   afrikaans   afrikaans       "Afrikaans"     false  iso8859-15 af_ZA  ""
183         # To:
184         #   #: lib/languages:2
185         #   msgid "Afrikaans"
186         #   msgstr ""
187         if reg.match(line):
188             print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
189                 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
190         else:
191             print "Error: Unable to handle line:"
192             print line
193             sys.exit(1)
194     input.close()
195     output.close()
196
197
198 def external_l10n(input_files, output, base):
199     '''Generate pot file from lib/external_templates'''
200     output = open(output, 'w')
201     Template = re.compile(r'^Template\s+(.*)')
202     GuiName = re.compile(r'\s*GuiName\s+(.*)')
203     HelpTextStart = re.compile(r'\s*HelpText\s')
204     HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
205     HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
206     i = -1
207     for src in input_files:
208         input = open(src)
209         inHelp = False
210         hadHelp = False
211         prev_help_string = ''
212         for lineno, line in enumerate(input.readlines()):
213             if Template.match(line):
214                 (string,) = Template.match(line).groups()
215             elif GuiName.match(line):
216                 (string,) = GuiName.match(line).groups()
217             elif inHelp:
218                 if HelpTextEnd.match(line):
219                     if hadHelp:
220                         print >> output, '\nmsgstr ""\n'
221                     inHelp = False
222                     hadHelp = False
223                     prev_help_string = ''
224                 elif HelpTextSection.match(line):
225                     (help_string,) = HelpTextSection.match(line).groups()
226                     help_string = help_string.replace('"', '')
227                     if help_string != "" and prev_help_string == '':
228                         print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
229                             (relativePath(src, base), lineno+1, help_string)
230                         hadHelp = True
231                     elif help_string != "":
232                         print >> output, '"%s\\n"' % help_string
233                     prev_help_string = help_string
234             elif HelpTextStart.match(line):
235                 inHelp = True
236                 prev_help_string = ''
237             else:
238                 continue
239             string = string.replace('"', '')
240             if string != "" and not inHelp:
241                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
242                     (relativePath(src, base), lineno+1, string)
243         input.close()
244     output.close()
245
246
247 Usage = '''
248 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
249
250 where 
251     --base:
252         path to the top source directory. default to '.'
253     --output:
254         output pot file, default to './lyx.pot'
255     --input_type can be
256         ui: lib/ui/*
257         layouts: lib/layouts/*
258         qt4: qt4 ui files
259         languages: file lib/languages
260         external: external templates file
261 '''
262
263 if __name__ == '__main__':
264     input_type = None
265     output = 'lyx.pot'
266     base = '.'
267     #
268     optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
269         ['help', 'type=', 'output=', 'base='])
270     for (opt, value) in optlist:
271         if opt in ['-h', '--help']:
272             print Usage
273             sys.exit(0)
274         elif opt in ['-o', '--output']:
275             output = value
276         elif opt in ['-b', '--base']:
277             base = value
278         elif opt in ['-t', '--type']:
279             input_type = value
280     if input_type not in ['ui', 'layouts', 'modules', 'qt4', 'languages', 'external'] or output is None:
281         print 'Wrong input type or output filename.'
282         sys.exit(1)
283     if input_type == 'ui':
284         ui_l10n(args, output, base)
285     elif input_type == 'layouts':
286         layouts_l10n(args, output, base)
287     elif input_type == 'qt4':
288         qt4_l10n(args, output, base)
289     elif input_type == 'external':
290         external_l10n(args, output, base)
291     else:
292         languages_l10n(args, output, base)
293