Zine

open source content publishing system


source: scripts/add-translation @ 1380:6af61c656d9d

Revision 1380:6af61c656d9d, 2.8 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    Add new Translation
5    ~~~~~~~~~~~~~~~~~~~
6
7    This script adds a new translation to the main application or a plugin.
8
9    :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details.
10    :license: BSD, see LICENSE for more details.
11"""
12from os import path, makedirs
13from optparse import OptionParser
14from datetime import datetime
15from babel import Locale, UnknownLocaleError
16from babel.messages import Catalog
17from babel.messages.pofile import read_po, write_po
18from babel.util import LOCALTZ
19
20app_dir = 'zine'
21i18n_dir = 'i18n'
22app_path = path.realpath(path.join(path.dirname(__file__), path.pardir, app_dir))
23app_i18n_path = path.join(app_path, i18n_dir)
24
25
26def main():
27    global parser
28    parser = OptionParser(usage='%prog [options] language')
29    parser.add_option('--plugin', dest='plugin', help='Create the '
30                      'translation for this plugin.  This '
31                      'has to be the full path to the plugin package.')
32    options, args = parser.parse_args()
33    if len(args) != 1:
34        parser.error('incorrect number of arguments')
35
36    try:
37        locale = Locale.parse(args[0])
38    except UnknownLocaleError, e:
39        parser.error(str(e))
40
41    if options.plugin is None:
42        create_application_lang(locale)
43    else:
44        create_plugin_lang(locale, options.plugin)
45
46
47def create_from_pot(locale, path):
48    try:
49        f = file(path)
50    except IOError, e:
51        parser.error(str(e))
52    try:
53        catalog = read_po(f, locale=locale)
54    finally:
55        f.close()
56    catalog.locale = locale
57    catalog.revision_date = datetime.now(LOCALTZ)
58    return catalog
59
60
61def write_catalog(catalog, folder):
62    target = path.join(folder, str(catalog.locale))
63    if not path.isdir(target):
64        makedirs(target)
65    f = file(path.join(target, 'messages.po'), 'w')
66    try:
67        write_po(f, catalog, width=79)
68    finally:
69        f.close()
70
71
72def create_application_lang(locale):
73    catalog = create_from_pot(locale, path.join(app_i18n_path, 'messages.pot'))
74    write_catalog(catalog, app_i18n_path)
75    print 'Created catalog for %s' % locale
76
77
78def create_plugin_lang(locale, path):
79    catalog = create_from_pot(locale, path.join(path, i18n_dir, 'messages.pot'))
80
81    # incorporate existing translations from the application
82    app_messages = path.join(app_i18n_path, str(locale), 'messages.po')
83    if path.isfile(app_messages):
84        f = file(app_messages)
85        try:
86            translated = read_po(f)
87        finally:
88            f.close()
89
90        for message in translated:
91            if message.id and message.id in catalog:
92                catalog[message.id].string = message.string
93
94    write_catalog(catalog, path.join(path, i18n_dir))
95    print 'Created catalog for %s' % locale
96
97
98if __name__ == '__main__':
99    main()
Note: See TracBrowser for help on using the repository browser.