コード例 #1
0
ファイル: updateTemplates.py プロジェクト: righnatios/0ad
def generateTemplatesForMessagesFile(messagesFilePath):

    with open(messagesFilePath, 'r') as fileObject:
        settings = json.load(fileObject)

    rootPath = os.path.dirname(messagesFilePath)

    for templateSettings in settings:
        if "skip" in templateSettings and templateSettings["skip"] == "yes":
            continue

        inputRootPath = rootPath
        if "inputRoot" in templateSettings:
            inputRootPath = os.path.join(rootPath, templateSettings["inputRoot"])

        template = Catalog(os.path.join(rootPath, templateSettings["output"]), create=True, truncate=True)
        h = template.update_header(templateSettings["project"], "Translation template for %project.", "Copyright © "+"2014"+" "+templateSettings["copyrightHolder"], "This file is distributed under the same license as the %project project.", plforms="nplurals=2; plural=(n != 1);")
        h.remove_field("Report-Msgid-Bugs-To")
        h.remove_field("Last-Translator")
        h.remove_field("Language-Team")
        h.remove_field("Language")
        h.author = Monlist()

        for rule in templateSettings["rules"]:
            if "skip" in rule and rule["skip"] == "yes":
                continue

            options = rule.get("options", {})
            extractorClass = getattr(__import__("extractors.extractors", {}, {}, [rule["extractor"]]), rule["extractor"])
            extractor = extractorClass(inputRootPath, rule["filemasks"], options)
            formatFlag = None
            if "format" in options:
                formatFlag = options["format"]
            for message, plural, context, location, comments in extractor.run():
                msg = Message({"msgid": message, "msgid_plural": plural, "msgctxt": context, "auto_comment": comments, "flag": [formatFlag] if formatFlag and string.find(message, "%") != -1 else None, "source": [location]})
                if template.get(msg):
                    template.get(msg).source.append(Monpair(location))
                else:
                    template.add(msg)

        template.set_encoding("utf-8")
        template.sync(fitplural=True)
        print(u"Generated \"{}\" with {} messages.".format(templateSettings["output"], len(template)))
コード例 #2
0
ファイル: pohybdl.py プロジェクト: KDE/pology
def hybdl(path, path0, accnohyb=False):

    cat = Catalog(path)
    cat0 = Catalog(path0, monitored=False)

    nhybridized = 0
    nstopped = 0
    for msg in cat:

        if "no-hybdl" in manc_parse_flag_list(msg, "|"):
            continue

        # Unembed diff if message was diffed for review.
        # Replace ediff with manual review flag.
        diffed = False
        for flag in msg.flag:
            if flag.startswith("ediff"):
                msg.flag.remove(flag)
                diffed = True
        if diffed:
            msg_ediff_to_new(msg, msg)
            msg.flag.add(u"reviewed")

        # Fetch original message.
        msg0 = cat0.get(msg)
        if msg0 is None:
            warning_on_msg(
                _("@info", "Message does not exist in the original catalog."),
                msg, cat)
            nstopped += 1
            continue
        if len(msg.msgstr) != len(msg0.msgstr):
            warning_on_msg(
                _(
                    "@info", "Number of translations not same as in "
                    "the original message."), msg, cat)
            nstopped += 1
            continue
        if msg.msgstr == msg0.msgstr:
            # No changes, nothing new to hybridize.
            continue

        # Hybridize translation.
        textsh = []
        textshinv = []
        for text0, text in zip(msg0.msgstr, msg.msgstr):
            texth = tohi(text0, text, parthyb=True)
            textsh.append(texth)
            if not accnohyb:
                texthinv = tohi(text, text0, parthyb=True)
                textshinv.append(texthinv)
        if accnohyb or textsh == textshinv:
            for i, texth in zip(range(len(msg.msgstr)), textsh):
                msg.msgstr[i] = texth
            nhybridized += 1
        else:
            nstopped += 1
            msgh = MessageUnsafe(msg)
            msgh.msgstr = textsh
            msghinv = MessageUnsafe(msg)
            msghinv.msgstr = textshinv
            msg_ediff(msghinv, msgh, emsg=msgh, colorize=True)
            report_msg_content(msgh, cat, delim=("-" * 20))

    if nstopped == 0:
        if cat.sync():
            report("! %s (%d)" % (path, nhybridized))
    else:
        warning(
            n_("@info", "%(num)d message in '%(file)s' cannot be "
               "cleanly hybridized.",
               "%(num)d messages in '%(file)s' cannot be "
               "cleanly hybridized.",
               num=nstopped,
               file=path))
        nhybridized = 0

    return nhybridized
コード例 #3
0
ファイル: updateTemplates.py プロジェクト: victorzhangl/0ad
def generateTemplatesForMessagesFile(messagesFilePath):

    with open(messagesFilePath, 'r') as fileObject:
        settings = json.load(fileObject)

    rootPath = os.path.dirname(messagesFilePath)

    for templateSettings in settings:
        if "skip" in templateSettings and templateSettings["skip"] == "yes":
            continue

        inputRootPath = rootPath
        if "inputRoot" in templateSettings:
            inputRootPath = os.path.join(rootPath,
                                         templateSettings["inputRoot"])

        template = Catalog(os.path.join(rootPath, templateSettings["output"]),
                           create=True,
                           truncate=True)
        h = template.update_header(
            templateSettings["project"],
            "Translation template for %project.",
            "Copyright © " + "2014" + " " +
            templateSettings["copyrightHolder"],
            "This file is distributed under the same license as the %project project.",
            plforms="nplurals=2; plural=(n != 1);")
        h.remove_field("Report-Msgid-Bugs-To")
        h.remove_field("Last-Translator")
        h.remove_field("Language-Team")
        h.remove_field("Language")
        h.author = Monlist()

        for rule in templateSettings["rules"]:
            if "skip" in rule and rule["skip"] == "yes":
                continue

            options = rule.get("options", {})
            extractorClass = getattr(
                __import__("extractors.extractors", {}, {},
                           [rule["extractor"]]), rule["extractor"])
            extractor = extractorClass(inputRootPath, rule["filemasks"],
                                       options)
            formatFlag = None
            if "format" in options:
                formatFlag = options["format"]
            for message, plural, context, location, comments in extractor.run(
            ):
                msg = Message({
                    "msgid":
                    message,
                    "msgid_plural":
                    plural,
                    "msgctxt":
                    context,
                    "auto_comment":
                    comments,
                    "flag": [formatFlag] if formatFlag
                    and string.find(message, "%") != -1 else None,
                    "source": [location]
                })
                if template.get(msg):
                    template.get(msg).source.append(Monpair(location))
                else:
                    template.add(msg)

        template.set_encoding("utf-8")
        template.sync(fitplural=True)
        print(u"Generated \"{}\" with {} messages.".format(
            templateSettings["output"], len(template)))