Source

zine-main / scripts / update-translations

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Update the translations
    ~~~~~~~~~~~~~~~~~~~~~~~

    Update the translations from the POT.

    :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""
from os import path, listdir, rename
from optparse import OptionParser
from babel import Locale
from babel.messages import Catalog
from babel.messages.pofile import write_po, read_po


domains = ['messages']


def main():
    global parser
    parser = OptionParser(usage='%prog [path]')
    parser.add_option('--locale', '-l', dest='locale',
                      help="update the specified locale")
    parser.add_option('--statistics', '-s', default=False,
                      action='store_true', help="show statistics")
    options, args = parser.parse_args()
    if not args:
        print 'Updating core strings'
        root = path.abspath(path.join(path.dirname(__file__),
                                      path.pardir, 'zine', 'i18n'))
    elif len(args) == 1:
        root = path.join(path.abspath(args[0]), 'i18n')
        if not path.isdir(root):
            parser.error('source folder missing')
        print 'Updating', root
    else:
        parser.error('incorrect number of arguments')

    if options.locale:
        for domain in domains:
            filepath = path.join(root, options.locale, domain + '.po')
            if not path.exists(filepath):
                parser.error("unknown locale. %s not found." % filepath)


    f = file(path.join(root, 'messages.pot'))
    try:
        template = read_po(f)
    finally:
        f.close()

    po_files = []
    for lang in listdir(root):
        for domain in domains:
            filename = path.join(root, lang, domain + '.po')
            if options.locale and filename != \
                               path.join(root, options.locale, domain + '.po'):
                continue
            if path.exists(filename):
                print 'Updating %r' % lang,
                locale = Locale.parse(lang)
                f = file(filename)
                try:
                    catalog = read_po(f, locale=locale, domain=domain)
                finally:
                    f.close()
                catalog.update(template)

                # XXX: this is kinda dangerous, but as we are using a
                # revision control system anyways that shouldn't make
                # too many problems
                f = file(filename, 'w')
                try:
                    write_po(f, catalog, ignore_obsolete=True,
                             include_previous=False, width=79)
                finally:
                    if options.statistics:
                        translated = fuzzy = percentage = 0
                        for message in list(catalog)[1:]:
                            if message.string:
                                translated +=1
                            if 'fuzzy' in message.flags:
                                fuzzy += 1
                        if len(catalog):
                            percentage = translated * 100 // len(catalog)
                            print "-> %d of %d messages (%d%%) translated" % (
                                translated, len(catalog), percentage),
                            if fuzzy:
                                if fuzzy == 1:
                                    print "%d of which is fuzzy" % fuzzy,
                                else:
                                    print "%d of which are fuzzy" % fuzzy,
                            print
                    else:
                        print
                    f.close()


    print 'All done.'


if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.