| 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 | """ |
|---|
| 13 | from os import path, makedirs |
|---|
| 14 | from optparse import OptionParser |
|---|
| 15 | from babel.messages import Catalog |
|---|
| 16 | from babel.messages.extract import extract_from_dir |
|---|
| 17 | from babel.messages.pofile import write_po |
|---|
| 18 | |
|---|
| 19 | app_dir = 'zine' |
|---|
| 20 | i18n_dir = 'i18n' |
|---|
| 21 | app_path = path.realpath(path.join(path.dirname(__file__), path.pardir, app_dir)) |
|---|
| 22 | |
|---|
| 23 | KEYWORDS = { |
|---|
| 24 | '_': None, |
|---|
| 25 | 'gettext': None, |
|---|
| 26 | 'ngettext': (1, 2), |
|---|
| 27 | 'l_': None, |
|---|
| 28 | 'lazy_gettext': None, |
|---|
| 29 | 'lazy_ngettext': (1, 2) |
|---|
| 30 | } |
|---|
| 31 | BUGS_ADDRESS = 'zine-i18n@pocoo.org' |
|---|
| 32 | COPYRIGHT = 'The Pocoo Team' |
|---|
| 33 | METHODS = [ |
|---|
| 34 | ('**.py', 'python'), |
|---|
| 35 | ('**.html', 'jinja2'), |
|---|
| 36 | ('**.js', 'javascript'), |
|---|
| 37 | ] |
|---|
| 38 | COMMENT_TAGS = ['_'] |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | def 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 | |
|---|
| 47 | def 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 | |
|---|
| 90 | if __name__ == '__main__': |
|---|
| 91 | main() |
|---|