| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | """ |
|---|
| 4 | Update the translations |
|---|
| 5 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | This script updates the translations of the main application or a plugin |
|---|
| 8 | from a POT file. |
|---|
| 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, listdir, rename |
|---|
| 14 | from optparse import OptionParser |
|---|
| 15 | from babel import Locale |
|---|
| 16 | from babel.messages import Catalog |
|---|
| 17 | from babel.messages.pofile import write_po, read_po |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | domains = ['messages'] |
|---|
| 21 | |
|---|
| 22 | app_dir = 'zine' |
|---|
| 23 | i18n_dir = 'i18n' |
|---|
| 24 | app_path = path.realpath(path.join(path.dirname(__file__), path.pardir, app_dir)) |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | def main(): |
|---|
| 28 | global parser |
|---|
| 29 | parser = OptionParser(usage='%prog [path]') |
|---|
| 30 | parser.add_option('--locale', '-l', dest='locale', |
|---|
| 31 | help="update the specified locale") |
|---|
| 32 | parser.add_option('--statistics', '-s', default=False, |
|---|
| 33 | action='store_true', help="show statistics") |
|---|
| 34 | options, args = parser.parse_args() |
|---|
| 35 | if not args: |
|---|
| 36 | print 'Updating core strings' |
|---|
| 37 | root = path.join(app_path, i18n_dir) |
|---|
| 38 | elif len(args) == 1: |
|---|
| 39 | root = path.join(path.abspath(args[0]), i18n_dir) |
|---|
| 40 | if not path.isdir(root): |
|---|
| 41 | parser.error('source folder missing') |
|---|
| 42 | print 'Updating', root |
|---|
| 43 | else: |
|---|
| 44 | parser.error('incorrect number of arguments') |
|---|
| 45 | |
|---|
| 46 | if options.locale: |
|---|
| 47 | for domain in domains: |
|---|
| 48 | filepath = path.join(root, options.locale, domain + '.po') |
|---|
| 49 | if not path.exists(filepath): |
|---|
| 50 | parser.error("unknown locale. %s not found." % filepath) |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | f = file(path.join(root, 'messages.pot')) |
|---|
| 54 | try: |
|---|
| 55 | template = read_po(f) |
|---|
| 56 | finally: |
|---|
| 57 | f.close() |
|---|
| 58 | |
|---|
| 59 | po_files = [] |
|---|
| 60 | for lang in listdir(root): |
|---|
| 61 | for domain in domains: |
|---|
| 62 | filename = path.join(root, lang, domain + '.po') |
|---|
| 63 | if options.locale and filename != \ |
|---|
| 64 | path.join(root, options.locale, domain + '.po'): |
|---|
| 65 | continue |
|---|
| 66 | if path.exists(filename): |
|---|
| 67 | print 'Updating %r' % lang, |
|---|
| 68 | locale = Locale.parse(lang) |
|---|
| 69 | f = file(filename) |
|---|
| 70 | try: |
|---|
| 71 | catalog = read_po(f, locale=locale, domain=domain) |
|---|
| 72 | finally: |
|---|
| 73 | f.close() |
|---|
| 74 | catalog.update(template) |
|---|
| 75 | |
|---|
| 76 | # XXX: this is kinda dangerous, but as we are using a |
|---|
| 77 | # revision control system anyways that shouldn't make |
|---|
| 78 | # too many problems |
|---|
| 79 | f = file(filename, 'w') |
|---|
| 80 | try: |
|---|
| 81 | write_po(f, catalog, ignore_obsolete=True, |
|---|
| 82 | include_previous=False, width=79) |
|---|
| 83 | finally: |
|---|
| 84 | if options.statistics: |
|---|
| 85 | translated = fuzzy = percentage = 0 |
|---|
| 86 | for message in list(catalog)[1:]: |
|---|
| 87 | if message.string: |
|---|
| 88 | translated +=1 |
|---|
| 89 | if 'fuzzy' in message.flags: |
|---|
| 90 | fuzzy += 1 |
|---|
| 91 | if len(catalog): |
|---|
| 92 | percentage = translated * 100 // len(catalog) |
|---|
| 93 | print "-> %d of %d messages (%d%%) translated" % ( |
|---|
| 94 | translated, len(catalog), percentage), |
|---|
| 95 | if fuzzy: |
|---|
| 96 | if fuzzy == 1: |
|---|
| 97 | print "%d of which is fuzzy" % fuzzy, |
|---|
| 98 | else: |
|---|
| 99 | print "%d of which are fuzzy" % fuzzy, |
|---|
| 100 | print |
|---|
| 101 | else: |
|---|
| 102 | print |
|---|
| 103 | f.close() |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | print 'All done.' |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | if __name__ == '__main__': |
|---|
| 110 | main() |
|---|