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