]> git.lyx.org Git - lyx.git/blob - po/lyx_pot.py
Fix bug 2466 by Stefan Schimanski:
[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 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:
41         input = open(src)
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()
50             else:
51                 continue
52             string = string.replace('\\', '\\\\').replace('"', '')
53             if string != "":
54                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
55                     (relativePath(src, base), lineno+1, string)
56         input.close()
57     output.close()
58
59
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:
69         input = open(src)
70         for lineno, line in enumerate(input.readlines()):
71             if Style.match(line):
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()
80             else:
81                 continue
82             string = string.replace('\\', '\\\\').replace('"', '')
83             if string != "":
84                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
85                     (relativePath(src, base), lineno+1, string)
86         input.close()
87     output.close()
88
89
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:
96         input = open(src)
97         skipNextLine = False
98         for lineno, line in enumerate(input.readlines()):
99             # skip the line after <property name=shortcut>
100             if skipNextLine:
101                 skipNextLine = False
102                 continue
103             if prop.match(line):
104                 skipNextLine = True
105                 continue
106             # get lines that match <string>...</string>
107             if pat.match(line):
108                 (string,) = pat.match(line).groups()
109                 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('"', r'\"')
110                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
111                     (relativePath(src, base), lineno+1, string) 
112         input.close()
113     output.close()
114
115
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()):
122         if line[0] == '#':
123             continue
124         items = line.split('"')
125         # empty lines?
126         if len(items) < 3:
127             continue
128         # From:
129         #   afrikaans   afrikaans       "Afrikaans"     false  iso8859-15 af_ZA  ""
130         # To:
131         #   #: lib/languages:2
132         #   msgid "Afrikaans"
133         #   msgstr ""
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])
136     input.close()
137     output.close()
138
139
140 Usage = '''
141 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
142
143 where 
144     --base:
145         path to the top source directory. default to '.'
146     --output:
147         output pot file, default to './lyx.pot'
148     --input_type can be
149         ui: lib/ui/*
150         layouts: lib/layouts/*
151         qt4: qt4 ui files
152         languages: file lib/languages
153 '''
154
155 if __name__ == '__main__':
156     input_type = None
157     output = 'lyx.pot'
158     base = '.'
159     #
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']:
164             print Usage
165             sys.exit(0)
166         elif opt in ['-o', '--output']:
167             output = value
168         elif opt in ['-b', '--base']:
169             base = value
170         elif opt in ['-t', '--type']:
171             input_type = value
172     if input_type not in ['ui', 'layouts', 'qt4', 'languages'] or output is None:
173         print 'Wrong input type or output filename.'
174         sys.exit(1)
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)
181     else:
182         languages_l10n(args, output, base)
183