예제 #1
0
파일: pomtrans.py 프로젝트: KDE/pology
def translate_direct(paths, tsbuilder, options):

    transervs = {}

    catpaths = collect_catalogs(paths)
    for catpath in catpaths:

        # Collect messages and texts to translate.
        cat = Catalog(catpath)
        if options.accel is not None:  # force explicitly given accelerator
            cat.set_accelerator(options.accel)
        texts = []
        msgs = []
        for msg in cat:
            if to_translate(msg, options):
                msgf = MessageUnsafe(msg)
                remove_accel_msg(msgf, cat)
                texts.append(msgf.msgid)
                if msg.msgid_plural is not None:
                    texts.append(msgf.msgid_plural)
                msgs.append(msg)

        # Translate collected texts.
        slang = options.slang or "en"
        transerv = get_transerv(slang, options.tlang, cat, cat, tsbuilder)
        texts_tr = transerv.translate(texts) if texts else []
        if texts_tr is None:
            warning(
                _("@info",
                  "Translation service failure on '%(file)s'.",
                  file=catpath))
            continue
        for i, text in enumerate(texts_tr):
            text = reduce_for_encoding(text, cat.encoding())
            texts_tr[i] = text

        # Put translated texts into messages.
        singlepls = cat.plural_indices_single()
        for msg in msgs:
            msgid_tr = texts_tr.pop(0)
            if msg.msgid_plural is not None:
                msgid_plural_tr = texts_tr.pop(0)
            if msgid_tr:
                if msg.msgid_plural is not None:
                    for i in range(len(msg.msgstr)):
                        if i in singlepls:
                            msg.msgstr[i] = msgid_tr
                        else:
                            msg.msgstr[i] = msgid_plural_tr
                else:
                    msg.msgstr[0] = msgid_tr
                decorate(msg, options)

        sync_rep(cat, msgs)
예제 #2
0
파일: pomtrans.py 프로젝트: KDE/pology
def translate_parallel(paths, tsbuilder, options):

    pathrepl = options.parcats
    comppath = options.parcomp
    slang = options.slang
    tlang = options.tlang

    ccat = None
    if comppath is not None:
        if not os.path.isfile(comppath):
            error(
                _("@info",
                  "Compendium '%(file)s' does not exist.",
                  file=comppath))
        ccat = Catalog(comppath, monitored=False)

    if pathrepl is not None:
        lst = pathrepl.split(":")
        if len(lst) != 2:
            error(
                _("@info",
                  "Invalid search and replace specification '%(spec)s'.",
                  spec=pathrepl))
        pathsrch, pathrepl = lst

    catpaths = collect_catalogs(paths)
    for catpath in catpaths:

        # Open parallel catalog if it exists.
        pcat = None
        if pathrepl is not None:
            pcatpath = catpath.replace(pathsrch, pathrepl, 1)
            if catpath == pcatpath:
                error(
                    _("@info",
                      "Parallel catalog and target catalog are same files "
                      "for '%(file)s'.",
                      file=catpath))
            if os.path.isfile(pcatpath):
                pcat = Catalog(pcatpath, monitored=False)

        # If there is neither the parallel catalog nor the compendium,
        # skip processing current target catalog.
        if not pcat and not ccat:
            continue

        # Collect messages and texts to translate.
        cat = Catalog(catpath)
        pmsgs, psmsgs, ptexts = [], [], []
        cmsgs, csmsgs, ctexts = [], [], []
        for msg in cat:
            if to_translate(msg, options):
                # Priority: parallel catalog, then compendium.
                for scat, msgs, smsgs, texts in (
                    (pcat, pmsgs, psmsgs, ptexts),
                    (ccat, cmsgs, csmsgs, ctexts),
                ):
                    if scat and msg in scat:
                        smsg = scat[msg]
                        if smsg.translated:
                            msgs.append(msg)
                            smsgs.append(smsg)
                            texts.extend(smsg.msgstr)
                            break

        # Translate collected texts.
        texts_tr = []
        for texts, scat in ((ptexts, pcat), (ctexts, ccat)):
            transerv = get_transerv(slang, tlang, scat, cat, tsbuilder)
            texts_tr.append(transerv.translate(texts) if texts else [])
            if texts_tr[-1] is None:
                texts_tr = None
                break
        if texts_tr is None:
            warning(
                _("@info",
                  "Translation service failure on '%(file)s'.",
                  file=catpath))
            continue
        ptexts_tr, ctexts_tr = texts_tr

        # Put translated texts into messages.
        # For plural messages, assume 1-1 match to parallel language.
        for msgs, smsgs, texts in (
            (pmsgs, psmsgs, ptexts_tr),
            (cmsgs, csmsgs, ctexts_tr),
        ):
            for msg, smsg in zip(msgs, smsgs):
                ctexts = []
                for i in range(len(smsg.msgstr)):
                    text = texts.pop(0)
                    text = reduce_for_encoding(text, cat.encoding())
                    ctexts.append(text)
                for i in range(len(msg.msgstr)):
                    msg.msgstr[i] = i < len(ctexts) and ctexts[i] or ctexts[-1]
                    decorate(msg, options)

        sync_rep(cat, pmsgs + cmsgs)