]> git.lyx.org Git - lyx.git/blob - po/lyx_pot.py
Renamed Changelog.txt to ChangeLog.txt
[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     Popupmenu = re.compile(r'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"')
39     Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
40     Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
41     for src in input_files:
42         input = open(src)
43         for lineno, line in enumerate(input.readlines()):
44             if Submenu.match(line):
45                 (string,) = Submenu.match(line).groups()
46                 string = string.replace('_', ' ')
47             elif Popupmenu.match(line):
48                 (string,) = Popupmenu.match(line).groups()
49             elif Toolbar.match(line):
50                 (string,) = Toolbar.match(line).groups()
51             elif Item.match(line):
52                 (string,) = Item.match(line).groups()
53             else:
54                 continue
55             string = string.replace('"', '')
56             if string != "":
57                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
58                     (relativePath(src, base), lineno+1, string)
59         input.close()
60     output.close()
61
62
63 def layouts_l10n(input_files, output, base):
64     '''Generate pot file from lib/layouts/*.layout and *.inc'''
65     output = open(output, 'w')
66     Style = re.compile(r'^Style\s+(.*)')
67     # include ???LabelString???, but exclude comment lines
68     LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
69     GuiName = re.compile(r'\s*GuiName\s+(.*)')
70     ListName = re.compile(r'\s*ListName\s+(.*)')
71     for src in input_files:
72         input = open(src)
73         for lineno, line in enumerate(input.readlines()):
74             if Style.match(line):
75                 (string,) = Style.match(line).groups()
76                 string = string.replace('_', ' ')
77             elif LabelString.match(line):
78                 (string,) = LabelString.match(line).groups()
79             elif GuiName.match(line):
80                 (string,) = GuiName.match(line).groups()
81             elif ListName.match(line):
82                 (string,) = ListName.match(line).groups()
83             else:
84                 continue
85             string = string.replace('\\', '\\\\').replace('"', '')
86             if string != "":
87                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
88                     (relativePath(src, base), lineno+1, string)
89         input.close()
90     output.close()
91
92
93 def qt4_l10n(input_files, output, base):
94     '''Generate pot file from src/frontends/qt4/ui/*.ui'''
95     output = open(output, 'w')
96     pat = re.compile(r'\s*<string>(.*)</string>')
97     prop = re.compile(r'\s*<property.*name.*=.*shortcut')
98     for src in input_files:
99         input = open(src)
100         skipNextLine = False
101         for lineno, line in enumerate(input.readlines()):
102             # skip the line after <property name=shortcut>
103             if skipNextLine:
104                 skipNextLine = False
105                 continue
106             if prop.match(line):
107                 skipNextLine = True
108                 continue
109             # get lines that match <string>...</string>
110             if pat.match(line):
111                 (string,) = pat.match(line).groups()
112                 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('"', r'\"')
113                 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
114                     (relativePath(src, base), lineno+1, string) 
115         input.close()
116     output.close()
117
118
119 def languages_l10n(input_files, output, base):
120     '''Generate pot file from lib/language'''
121     output = open(output, 'w')
122     # assuming only one language file
123     input = open(input_files[0])
124     for lineno, line in enumerate(input.readlines()):
125         if line[0] == '#':
126             continue
127         items = line.split('"')
128         # empty lines?
129         if len(items) < 3:
130             continue
131         # From:
132         #   afrikaans   afrikaans       "Afrikaans"     false  iso8859-15 af_ZA  ""
133         # To:
134         #   #: lib/languages:2
135         #   msgid "Afrikaans"
136         #   msgstr ""
137         # I do not care extra "s like "af_ZA"
138         print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % (relativePath(input_files[0], base), lineno+1, items[1])
139     input.close()
140     output.close()
141
142
143 Usage = '''
144 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
145
146 where 
147     --base:
148         path to the top source directory. default to '.'
149     --output:
150         output pot file, default to './lyx.pot'
151     --input_type can be
152         ui: lib/ui/*
153         layouts: lib/layouts/*
154         qt4: qt4 ui files
155         languages: file lib/languages
156 '''
157
158 if __name__ == '__main__':
159     input_type = None
160     output = 'lyx.pot'
161     base = '.'
162     #
163     optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
164         ['help', 'type=', 'output=', 'base='])
165     for (opt, value) in optlist:
166         if opt in ['-h', '--help']:
167             print Usage
168             sys.exit(0)
169         elif opt in ['-o', '--output']:
170             output = value
171         elif opt in ['-b', '--base']:
172             base = value
173         elif opt in ['-t', '--type']:
174             input_type = value
175     if input_type not in ['ui', 'layouts', 'qt4', 'languages'] or output is None:
176         print 'Wrong input type or output filename.'
177         sys.exit(1)
178     if input_type == 'ui':
179         ui_l10n(args, output, base)
180     elif input_type == 'layouts':
181         layouts_l10n(args, output, base)
182     elif input_type == 'qt4':
183         qt4_l10n(args, output, base)
184     else:
185         languages_l10n(args, output, base)
186