| 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 | """ |
|---|
| 12 | from os import path, makedirs |
|---|
| 13 | from optparse import OptionParser |
|---|
| 14 | from datetime import datetime |
|---|
| 15 | from babel import Locale, UnknownLocaleError |
|---|
| 16 | from babel.messages import Catalog |
|---|
| 17 | from babel.messages.pofile import read_po, write_po |
|---|
| 18 | from babel.util import LOCALTZ |
|---|
| 19 | |
|---|
| 20 | app_dir = 'zine' |
|---|
| 21 | i18n_dir = 'i18n' |
|---|
| 22 | app_path = path.realpath(path.join(path.dirname(__file__), path.pardir, app_dir)) |
|---|
| 23 | app_i18n_path = path.join(app_path, i18n_dir) |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | def 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 | |
|---|
| 47 | def 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 | |
|---|
| 61 | def 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 | |
|---|
| 72 | def 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 | |
|---|
| 78 | def 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 | |
|---|
| 98 | if __name__ == '__main__': |
|---|
| 99 | main() |
|---|