Example #1
0
def main():
    p = argparse.ArgumentParser(description='convert .po to .json')
    p.add_argument('po_file')
    p.add_argument('-e', '--encoding', help='encoding of po file')
    p.add_argument('-p', '--pretty-print', help='pretty-print JSON output',
                   action="store_true")
    args = p.parse_args()

    if not os.path.isfile(args.po_file):
        p.exit(u"not a file: %s" % args.po_file)
    if not args.po_file.endswith('.po'):
        p.exit(u"not a PO file: %s" % args.po_file)

    print pojson.convert(
        args.po_file, encoding=args.encoding,
        pretty_print=args.pretty_print).encode('utf-8')
Example #2
0
def test_convert_javascript(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(msgid=u'One', msgstr=u'Een')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)

    result = convert('foo', path, encoding='utf-8', js=True)
    assert result == u'var json_locale_data = {"foo": {"": {}, "One": [null, "Een"]}};'
def test_convert_explicit_encoding(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(msgid=u'One', msgstr=u'Eén')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)

    result = convert(path, encoding='utf-8')
    # XXX dependent on default key sorting of simplejson
    assert result == u'{"": {}, "One": [null, "Eén"]}'
def test_convert(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(msgid=u'Hello world', msgstr=u'Hallo wereld')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)

    result = convert(path)
    # XXX dependent on default key sorting of simplejson
    assert result == '{"": {}, "Hello world": [null, "Hallo wereld"]}'
Example #5
0
def main():
    p = argparse.ArgumentParser(description='convert .po to .json')
    p.add_argument('po_file')
    p.add_argument('-e', '--encoding', help='encoding of po file')
    p.add_argument('-p', '--pretty-print', help='pretty-print JSON output',
                   action="store_true")
    args = p.parse_args()

    if not os.path.isfile(args.po_file):
        p.exit(u"not a file: %s" % args.po_file)
    if not args.po_file.endswith('.po'):
        p.exit(u"not a PO file: %s" % args.po_file)

    if PY3K:
        print(pojson.convert(
            args.po_file, encoding=args.encoding,
            pretty_print=args.pretty_print))
    else:
        print(pojson.convert(
            args.po_file, encoding=args.encoding,
            pretty_print=args.pretty_print).encode('utf-8'))
Example #6
0
def test_convert_explicit_encoding(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(
        msgid=u'One',
        msgstr=u'Eén')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)

    result = convert(path, encoding='utf-8')
    # XXX dependent on default key sorting of simplejson
    assert result == u'{"": {}, "One": [null, "Eén"]}'
Example #7
0
def test_convert(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(
        msgid=u'Hello world',
        msgstr=u'Hallo wereld')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)
    
    result = convert(path)
    # XXX dependent on default key sorting of simplejson
    assert result == '{"": {}, "Hello world": [null, "Hallo wereld"]}'
Example #8
0
def test_convert_javascript(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(
        msgid=u'One',
        msgstr=u'Een')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)

    result = convert('foo', path, encoding='utf-8',
                     js=True)
    assert result == u'var json_locale_data = {"foo": {"": {}, "One": [null, "Een"]}};'
Example #9
0
def test_convert_pretty_print(tmpdir):
    po = polib.POFile()
    po.metadata = {}
    entry = polib.POEntry(
        msgid=u'One',
        msgstr=u'Een')
    po.append(entry)

    path = tmpdir.join('test.po').strpath
    po.save(path)

    result = convert(path, pretty_print=True)

    # XXX apparently different versions of simplejson use different
    # pretty printing algorithms, so this may break
    assert result == u'''\
Example #10
0
File: i18n.py Project: jt1/indico
    def run(self):
        try:
            pkg_resources.require("pojson")
        except pkg_resources.DistributionNotFound:
            print """
            pojson not found! pojson is needed for JS i18n file generation, if you're building Indico from source. Please install it.
            i.e. try 'easy_install pojson'"""
            sys.exit(-1)

        from pojson import convert

        localeDirs = [name for name in os.listdir(self.input_dir) if os.path.isdir(os.path.join(self.input_dir, name))]

        for locale in localeDirs:
            result = convert(os.path.join(self.input_dir, locale, "LC_MESSAGES", "messages-js.po"), pretty_print=True)
            fname = os.path.join(self.output_dir, "%s.js" % locale)
            with open(fname, "w") as f:
                f.write((u"var json_locale_data = {0};".format(result)).encode("utf-8"))
            print "wrote %s" % fname
Example #11
0
    def run(self):
        po_files = self.input_files.split(",")
        for po_file in po_files:
            if not os.path.isfile(po_file):
                log.error('not a file: {0!r}'.format(po_file))
                continue
            if not po_file.endswith('.po'):
                log.error('not a PO file: {0!r}'.format(po_file))
                continue

            json_file = os.path.normpath(
                os.path.join(self.output_dir,
                             os.path.basename(po_file).replace('.po', '.json'))
            )
            log.info('compiling catalog {0!r} to {1!r}'.format(po_file,
                                                               json_file))

            with open(json_file, "wb") as f:
                f.write(convert(po_file, pretty_print=self.pretty_print)
                        .encode("utf-8"))
Example #12
0
    def run(self):
        po_files = self.input_files.split(",")
        for po_file in po_files:
            if not os.path.isfile(po_file):
                log.error('not a file: {0!r}'.format(po_file))
                continue
            if not po_file.endswith('.po'):
                log.error('not a PO file: {0!r}'.format(po_file))
                continue

            json_file = os.path.normpath(
                os.path.join(self.output_dir,
                             os.path.basename(po_file).replace('.po',
                                                               '.json')))
            log.info('compiling catalog {0!r} to {1!r}'.format(
                po_file, json_file))

            with open(json_file, "wb") as f:
                f.write(
                    convert(po_file,
                            pretty_print=self.pretty_print).encode("utf-8"))
Example #13
0
    def run(self):
        po_files = []
        json_files = []

        if self.locale:
            po_files.append(
                os.path.join(self.directory, self.locale, 'LC_MESSAGES',
                             self.domain + '.po'))
            json_files.append(
                os.path.join(self.output_dir, self.locale, 'LC_MESSAGES',
                             self.domain + '.json'))
        else:
            for locale in os.listdir(self.directory):
                po_file = os.path.join(self.directory, locale, 'LC_MESSAGES',
                                       self.domain + '.po')

                if os.path.exists(po_file):
                    po_files.append(po_file)
                    json_files.append(
                        os.path.join(self.output_dir, locale, 'LC_MESSAGES',
                                     self.domain + '.json'))

        if not po_files:
            raise DistutilsOptionError('no message catalogs found')

        for po_file, json_file in izip(po_files, json_files):
            log.info('compiling catalog {0!r} to {1!r}'.format(
                po_file, json_file))

            output_dir = os.path.dirname(json_file)
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)

            with open(json_file, "wb") as f:
                f.write(
                    convert(po_file,
                            pretty_print=self.pretty_print).encode("utf-8"))
Example #14
0
    def run(self):
        po_files = []
        json_files = []

        if self.locale:
            po_files.append(os.path.join(self.directory, self.locale,
                                         'LC_MESSAGES',
                                         self.domain + '.po'))
            json_files.append(os.path.join(self.output_dir, self.locale,
                                           'LC_MESSAGES',
                                           self.domain + '.json'))
        else:
            for locale in os.listdir(self.directory):
                po_file = os.path.join(self.directory, locale,
                                       'LC_MESSAGES', self.domain + '.po')

                if os.path.exists(po_file):
                    po_files.append(po_file)
                    json_files.append(os.path.join(self.output_dir, locale,
                                                   'LC_MESSAGES',
                                                   self.domain + '.json'))

        if not po_files:
            raise DistutilsOptionError('no message catalogs found')

        for po_file, json_file in izip(po_files, json_files):
            log.info('compiling catalog {0!r} to {1!r}'.format(po_file,
                                                               json_file))

            output_dir = os.path.dirname(json_file)
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)

            with open(json_file, "wb") as f:
                f.write(convert(po_file, pretty_print=self.pretty_print)
                        .encode("utf-8"))
Example #15
0
def convert(args):
    print pojson.convert(args.domain,
                         args.po_file,
                         js=args.javascript,
                         encoding=args.encoding,
                         pretty_print=args.pretty_print).encode('utf-8')
Example #16
0
    # copy all other files to their destination
    print "Copying the other files to " + web_temp_dir
    shutil.copytree(os.path.join(web_dev_dir, 'images'),
                    os.path.join(web_temp_dir, 'images'))
    shutil.copytree(os.path.join(web_dev_dir, 'css'),
                    os.path.join(web_temp_dir, 'css'))

    # prepare translations
    print "Converting .po to .json and copying them to " + web_temp_dir
    translations_folder = os.path.join('..', 'prosoar', 'translations')
    for translation in os.listdir(translations_folder):
        if len(translation) != 2: continue

        po_file = os.path.join(translations_folder, translation,
                               'LC_MESSAGES', 'messages.po')
        result = u'{"%s":%s}' % (translation, pojson.convert(po_file))

        path = os.path.join(web_temp_dir, 'LC_MESSAGES', translation + '.json')
        file(path, 'w').write(result.encode('utf-8'))

    # move temp directory to web dir
    if os.path.exists(web_dir):
        shutil.move(web_dir, web_dir + ".old")

    shutil.move(web_temp_dir, web_dir)

    if os.path.exists(web_dir + ".old"):
        shutil.rmtree(web_dir + ".old")

    print "Done."