Example #1
0
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)))
Example #2
0
def diff_pairs (pspecs, merge,
                colorize=False, wrem=True, wadd=True, shdr=False, noobs=False,
                quiet=False):

    # Create diffs of messages.
    # Note: Headers will be collected and diffed after all messages,
    # to be able to check if any decoration to their message keys is needed.
    wrappings = {}
    ecat = Catalog("", create=True, monitored=False)
    hspecs = []
    ndiffed = 0
    update_progress = None
    if len(pspecs) > 1 and not quiet:
        update_progress = init_file_progress([vp[1] for fp, vp in pspecs],
                            addfmt=t_("@info:progress", "Diffing: %(file)s"))
    for fpaths, vpaths in pspecs:
        upprogf = None
        if update_progress:
            upprogf = lambda: update_progress(vpaths[1])
            upprogf()
        # Quick check if files are binary equal.
        if fpaths[0] and fpaths[1] and filecmp.cmp(*fpaths):
            continue
        cats = []
        for fpath in fpaths:
            try:
                cats.append(Catalog(fpath, create=True, monitored=False))
            except:
                error_wcl(_("@info",
                            "Cannot parse catalog '%(file)s'.",
                            file=fpath), norem=[fpath])
        tpos = len(ecat)
        cndiffed = diff_cats(cats[0], cats[1], ecat,
                             merge, colorize, wrem, wadd, noobs, upprogf)
        hspecs.append(([not x.created() and x.header or None
                        for x in cats], vpaths, tpos, cndiffed))
        ndiffed += cndiffed
        # Collect and count wrapping policy used for to-catalog.
        wrapping = cats[1].wrapping()
        if wrapping not in wrappings:
            wrappings[wrapping] = 0
        wrappings[wrapping] += 1
    if update_progress:
        update_progress()

    # Find appropriate length of context for header messages.
    hmsgctxt = get_msgctxt_for_headers(ecat)
    init_ediff_header(ecat.header, hmsgctxt=hmsgctxt)

    # Create diffs of headers.
    # If some of the messages were diffed,
    # header must be added even if there is no difference.
    incpos = 0
    for hdrs, vpaths, pos, cndiffed in hspecs:
        ehmsg, anydiff = diff_hdrs(hdrs[0], hdrs[1], vpaths[0], vpaths[1],
                                   hmsgctxt, ecat, colorize)
        if anydiff or cndiffed:
            ecat.add(ehmsg, pos + incpos)
            incpos += 1
    # Add diffed headers to total count only if header stripping not in effect.
    if not shdr:
        ndiffed += incpos

    # Set the most used wrapping policy for the ediff catalog.
    if wrappings:
        wrapping = sorted(wrappings.items(), key=lambda x: x[1])[-1][0]
        ecat.set_wrapping(wrapping)
        if wrapping is not None:
            ecat.header.set_field(u"X-Wrapping", u", ".join(wrapping))

    return ecat, ndiffed
Example #3
0
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)))
def generateLongStringTranslationFromPotIntoPo(inputFilePath, outputFilePath):

    templateCatalog = Catalog(inputFilePath)
    longStringCatalog = Catalog(outputFilePath, create=True, truncate=True)

    # Fill catalog with English strings.
    for message in templateCatalog:
        longStringCatalog.add(message)

    # If language codes were specified on the command line, filder by those.
    filters = sys.argv[1:]

    # Load existing translation catalogs.
    existingTranslationCatalogs = []
    l10nFolderPath = os.path.dirname(inputFilePath)

    # .pot is one letter longer than .po, but the dot that separates the locale
    # code from the rest of the filename in .po files makes up for that.
    charactersToSkip = len(os.path.basename(inputFilePath))

    for filename in os.listdir(l10nFolderPath):
        if len(filename
               ) > 3 and filename[-3:] == ".po" and filename[:4] != "long":
            if not filters or filename[:-charactersToSkip] in filters:
                if os.path.basename(inputFilePath)[:-4] == filename.split(
                        '.')[-2]:
                    existingTranslationCatalogs.append(
                        os.path.join(l10nFolderPath, filename))

    # If any existing translation has more characters than the average expansion, use that instead.
    for pofile in existingTranslationCatalogs:
        print(u"Merging", pofile)
        translationCatalog = Catalog(pofile)
        for longStringCatalogMessage in longStringCatalog:
            translationMessage = translationCatalog.select_by_key(
                longStringCatalogMessage.msgctxt,
                longStringCatalogMessage.msgid)
            if not translationMessage:
                continue

            if not longStringCatalogMessage.msgid_plural:
                if len(translationMessage[0].msgstr[0]) > len(
                        longStringCatalogMessage.msgstr[0]):
                    longStringCatalogMessage.msgstr = translationMessage[
                        0].msgstr
                    translationMessage = longStringCatalogMessage
                continue

            longestSingularString = translationMessage[0].msgstr[0]
            longestPluralString = translationMessage[0].msgstr[1] if len(
                translationMessage[0].msgstr) > 1 else longestSingularString

            candidateSingularString = longStringCatalogMessage.msgstr[0]
            candidatePluralString = ""  # There might be between 0 and infinite plural forms.
            for candidateString in longStringCatalogMessage.msgstr[1:]:
                if len(candidateString) > len(candidatePluralString):
                    candidatePluralString = candidateString

            changed = False
            if len(candidateSingularString) > len(longestSingularString):
                longestSingularString = candidateSingularString
                changed = True
            if len(candidatePluralString) > len(longestPluralString):
                longestPluralString = candidatePluralString
                changed = True

            if changed:
                longStringCatalogMessage.msgstr = [
                    longestSingularString, longestPluralString
                ]
                translationMessage = longStringCatalogMessage

    longStringCatalog.set_encoding("utf-8")
    longStringCatalog.sync()
def generateLongStringTranslationFromPotIntoPo(inputFilePath, outputFilePath):

    templateCatalog = Catalog(inputFilePath)
    longStringCatalog = Catalog(outputFilePath, create=True, truncate=True)

    # Fill catalog with English strings.
    for message in templateCatalog:
        longStringCatalog.add(message)

    # If language codes were specified on the command line, filder by those.
    filters = sys.argv[1:]

    # Load existing translation catalogs.
    existingTranslationCatalogs = []
    l10nFolderPath = os.path.dirname(inputFilePath)

    # .pot is one letter longer than .po, but the dot that separates the locale
    # code from the rest of the filename in .po files makes up for that.
    charactersToSkip = len(os.path.basename(inputFilePath))

    for filename in os.listdir(l10nFolderPath):
        if len(filename) > 3 and filename[-3:] == ".po" and filename[:4] != "long":
            if not filters or filename[:-charactersToSkip] in filters:
                if os.path.basename(inputFilePath)[:-4] == filename.split('.')[-2]:
                    existingTranslationCatalogs.append(os.path.join(l10nFolderPath, filename))

    # If any existing translation has more characters than the average expansion, use that instead.
    for pofile in existingTranslationCatalogs:
        print(u"Merging", pofile)
        translationCatalog = Catalog(pofile)
        for longStringCatalogMessage in longStringCatalog:
            translationMessage = translationCatalog.select_by_key(longStringCatalogMessage.msgctxt, longStringCatalogMessage.msgid)
            if not translationMessage:
                continue

            if not longStringCatalogMessage.msgid_plural:
                if len(translationMessage[0].msgstr[0]) > len(longStringCatalogMessage.msgstr[0]):
                    longStringCatalogMessage.msgstr = translationMessage[0].msgstr
                    translationMessage = longStringCatalogMessage
                continue

            longestSingularString = translationMessage[0].msgstr[0]
            longestPluralString = translationMessage[0].msgstr[1] if len(translationMessage[0].msgstr) > 1 else longestSingularString

            candidateSingularString = longStringCatalogMessage.msgstr[0]
            candidatePluralString = "" # There might be between 0 and infinite plural forms.
            for candidateString in longStringCatalogMessage.msgstr[1:]:
                if len(candidateString) > len(candidatePluralString): candidatePluralString = candidateString

            changed = False
            if len(candidateSingularString) > len(longestSingularString):
                longestSingularString = candidateSingularString
                changed = True
            if len(candidatePluralString) > len(longestPluralString):
                longestPluralString   = candidatePluralString
                changed = True

            if changed:
                longStringCatalogMessage.msgstr = [longestSingularString, longestPluralString]
                translationMessage = longStringCatalogMessage

    longStringCatalog.set_encoding("utf-8")
    longStringCatalog.sync()