Zine

open source content publishing system


source: scripts/extract-messages @ 1380:6af61c656d9d

Revision 1380:6af61c656d9d, 2.5 KB checked in by Thomas Waldmann <tw AT waldmann-edv DOT de>, 23 months ago (diff)

translation scripts: made more easily reusable, misc. cleanup (see below)

Made os.path usage more similar for all translation scripts (from os import path).

Made module docstrings more similar and more informative.

Replaced 'Zine' by 'main application'.

Replaced *zine* names by something more generic, e.g. 'app_dir' for the
application package directory (instead of 'zine').

Compute app_path (full path to application package dir) always in the
same way, at same place.

  • Property exe set to *
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4    Extract Messages
5    ~~~~~~~~~~~~~~~~
6
7    This script extracts messages from the main application or a plugin and
8    writes them into a PO-Template (POT).
9
10    :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details.
11    :license: BSD, see LICENSE for more details.
12"""
13from os import path, makedirs
14from optparse import OptionParser
15from babel.messages import Catalog
16from babel.messages.extract import extract_from_dir
17from babel.messages.pofile import write_po
18
19app_dir = 'zine'
20i18n_dir = 'i18n'
21app_path = path.realpath(path.join(path.dirname(__file__), path.pardir, app_dir))
22
23KEYWORDS = {
24    '_': None,
25    'gettext': None,
26    'ngettext': (1, 2),
27    'l_': None,
28    'lazy_gettext': None,
29    'lazy_ngettext': (1, 2)
30}
31BUGS_ADDRESS = 'zine-i18n@pocoo.org'
32COPYRIGHT = 'The Pocoo Team'
33METHODS = [
34    ('**.py', 'python'),
35    ('**.html', 'jinja2'),
36    ('**.js', 'javascript'),
37]
38COMMENT_TAGS = ['_']
39
40
41def strip_path(filename, base):
42    filename = path.normpath(path.join(base, filename))
43    return filename[len(path.commonprefix([
44        filename, path.dirname(base)])):].lstrip(path.sep)
45
46
47def main():
48    global parser
49    parser = OptionParser(usage='%prog [path]')
50    options, args = parser.parse_args()
51    if not args:
52        print 'Extracting core strings'
53        root = app_path
54    elif len(args) == 1:
55        root = path.join(path.abspath(args[0]))
56        if not path.isdir(root):
57            parser.error('source folder missing')
58        print 'Extracting from', root
59    else:
60        parser.error('incorrect number of arguments')
61
62    catalog = Catalog(msgid_bugs_address=BUGS_ADDRESS,
63                      copyright_holder=COPYRIGHT, charset='utf-8')
64
65    def callback(filename, method, options):
66        if method != 'ignore':
67            print strip_path(filename, root)
68
69    extracted = extract_from_dir(root, METHODS, {}, KEYWORDS,
70                                 COMMENT_TAGS, callback=callback,
71                                 strip_comment_tags=True)
72
73    for filename, lineno, message, comments in extracted:
74        catalog.add(message, None, [(strip_path(filename, root), lineno)],
75                    auto_comments=comments)
76
77    output_path = path.join(root, i18n_dir)
78    if not path.isdir(output_path):
79        makedirs(output_path)
80
81    f = file(path.join(output_path, 'messages.pot'), 'w')
82    try:
83        write_po(f, catalog, width=79)
84    finally:
85        f.close()
86
87    print 'All done.'
88
89
90if __name__ == '__main__':
91    main()
Note: See TracBrowser for help on using the repository browser.