Ejemplo n.º 1
0
def setLang(newlang):
    """Set the actual language. Returns old and new languages and string_cache in a tulpe.

    newlang: str: new laguage to load in the format <language>_<country>"""
    global lang
    oldLang = "" + lang
    if not lang == newlang:
        sc, result = buildTranslation(newlang)
        lang = newlang
        if newlang == 'en_US':
            result = True
            try:
                resource.setCurLang(u"English (US)")
            except:
                resource.__curLang = u"English (US)"
    else:
        result = True
    return oldLang, lang, result
Ejemplo n.º 2
0
def setLang(newlang):
    """Set the actual language. Returns old and new languages and string_cache in a tulpe.

    newlang: str: new laguage to load in the format <language>_<country>"""
    global lang
    oldLang = "" + lang
    if not lang == newlang:
        sc, result = buildTranslation(newlang)
        lang = newlang
        if newlang == 'en_US':
            result = True
            try:
                resource.setCurLang(u"English (US)")
            except:
                resource.__curLang = u"English (US)"
    else:
        result = True
    return oldLang, lang, result
Ejemplo n.º 3
0
def buildTranslation(lang, extend=False, langPath=None):
    """Finds the file corresponding to 'lang' builds up string_cache.
    If the file is not valid, does nothing.
    Errors encountered during the process are silently ignored.
    Returns string_cache (dict) and wether the file exists (bool)."""
    log.debug("buildTranslation <<<")
    tm = time()
    if not langPath:
        langPath = getLangPath()
    str_cache = {}
    global string_cache
    fileFound = False
    lang = u"%s" % lang
    fName = os.path.join(langPath, lang + ".trn")
    log.debug("fName: %s" % fName)
    if os.access(fName, os.F_OK) and os.path.isfile(fName) and os.access(
            fName, os.R_OK):
        fileFound = True
        rawData = codecs.open(fName, "r", "utf-8").read()
        log.debug("fName is valid and read.")
        log.debug("Type of file data is %s" %
                  ("%s" % type(rawData)).strip("<type '").strip(">")[:-1])
        log.debug("Parsing file and building JSON resource.")
        log.debug("  * Start on %s" % tm)
        start = re.search(r"^o\d+[ ]", rawData, re.M | re.S)
        if start:
            start = start.start()
        else:
            log.warning(
                "    *** %s malformed. Could not find entry point.\n    Translation not loaded."
            )
            # exiting without further operations
            return {}, False
        data = rawData[start:]
        trnPattern = re.compile(r"^o\d+[ ]|^t\d+[ ]", re.M | re.S)
        grps = re.findall(trnPattern, data)
        if len(grps) % 2 != 0:
            grps1 = re.findall(r"^o\d+[ ]", data, re.M | re.S)
            grps2 = re.findall(r"^t\d+[ ]", data, re.M | re.S)
            log.warning(
                "    Unpaired original and translated strings. %s is longer (oXX: %s, tXX: %s)."
                % ({
                    True: "tXX",
                    False: "oXX"
                }[len(grps1) < len(grps2)], len(grps1), len(grps2)))
            bugStrs = []

            def compLists(lst1, lst1N, lst2, lst2N, repl, bugStrs=bugStrs):
                bug = []
                for item in lst1:
                    itm = repl + item[1:]
                    if itm in lst2:
                        idx = lst2.index(itm)
                        lst2.pop(idx)
                    else:
                        bug.append(item)
                bugStrs += ["Not found in %s" % lst1N, bug]
                bugStrs += ["Not found in %s" % lst2N, lst2]

            if len(grps1) < len(grps2):
                compLists(grps1, "grps1", grps2, "grps2", u"t")
                log.warning("    Compared oXX tXX:")
                log.warning("    %s" % bugStrs)
            else:
                compLists(grps2, "grps2", grps1, "grps1", u"o")
                log.warning("    Compared tXX oXX:")
                log.warning("    %s" % bugStrs)
            return {}, False
        n1 = len(grps) / 2
        result = u""
        n = 1
        n2 = 0
        r = u"" + data.replace(u"\\", u"\\\\").replace(u"\"", u'\\"').replace(
            u"\r\n", u"\n").replace(u"\r", u"\n")
        log.debug("    Replacing oXX/tXX.")
        while n:
            r, n = re.subn(r"^o\d+[ ]|\no\d+[ ]",
                           "\",\"",
                           r,
                           flags=re.M | re.S)
            r, n = re.subn(r"^t\d+[ ]|\nt\d+[ ]",
                           "\":\"",
                           r,
                           flags=re.M | re.S)
            n2 += n
            if n2 == n1:
                n = 0
        log.debug("    Replaced %s occurences." % n2)
        result += r[2:]
        result = u"{" + result.replace(u"\r\n", u"\\n").replace(
            u"\n", u"\\n").replace(u"\t", u"\\t") + u"\"}"
        log.debug("    Conversion done. Loading JSON resource.")
        try:
            str_cache = json.loads(result)
            if extend:
                string_cache.update([(a, b) for (a, b) in str_cache.items()
                                     if a not in string_cache.keys()])
        except Exception, e:
            log.debug("Error while loading JSON resource:")
            log.debug("    %s" % e)
            log.debug("Dumping JSON data in %s.json" % lang)
            f = open('%s.json' % lang, 'w')
            f.write(result)
            f.close()
            return {}, False
#         log.debug("    Setting up font.") # Forgotten this here???
        if not extend:
            line = rawData.splitlines()[0]
            if "#-# " in line:
                lngNm = line.split("#-# ")[1].strip()
            else:
                lngNm = os.path.splitext(os.path.basename(fName))[0]
            try:
                resource.setCurLang(lngNm)
            except:
                resource.__curLang = lngNm
        tm1 = time()
        log.debug("  * End on %s duration %s" % (tm, tm1 - tm))
Ejemplo n.º 4
0
def buildTranslation(lang,suppressAlert=False):
    """Finds the file corresponding to 'lang' builds up string_cache.
    If the file is not valid, does nothing.
    Errors encountered during the process are silently ignored.
    Returns string_cache (dict) and wether the file exists (bool)."""
    log.debug("buildTranslation <<<")
    tm = time()
    global string_cache
    fileFound = False
    lang = "%s"%lang
    fName = os.path.join(langPath, lang + ".trn")
    log.debug("fName: %s"%fName)
    if os.access(fName, os.F_OK) and os.path.isfile(fName) and os.access(fName, os.R_OK):
        fileFound = True
        rawData = codecs.open(fName, "r", "utf-8").read()
        log.debug("fName is valid and read.")
        log.debug("Type of file data is %s"%("%s"%type(rawData)).strip("<type '").strip(">")[:-1])
        log.debug("Parsing file and building JSON resource.")
        log.debug("  * Start on %s"%tm)
        start = re.search(r"^o\d+[ ]", rawData, re.M|re.S)
        if start:
            start = start.start()
        else:
            log.warning("    *** %s malformed. Could not find entry point.\n    Translation not loaded.")
            # exiting without further operations
            return {}, False
        data = rawData[start:]
        trnPattern = re.compile(r"^o\d+[ ]|^t\d+[ ]", re.M|re.S)
        grps = re.findall(trnPattern, data)
        if len(grps) % 2 != 0:
            grps1 = re.findall(r"^o\d+[ ]", data, re.M|re.S)
            grps2 = re.findall(r"^t\d+[ ]", data, re.M|re.S)
            log.warning("    Unpaired original and translated strings. %s is longer (oXX: %s, tXX: %s)."%({True: "tXX", False: "oXX"}[len(grps1) < len(grps2)], len(grps1), len(grps2)))
            bugStrs = []
            def compLists(lst1, lst1N, lst2, lst2N, repl, bugStrs=bugStrs):
                bug = []
                for item in lst1:
                    itm = repl + item[1:]
                    if itm in lst2:
                        idx = lst2.index(itm)
                        lst2.pop(idx)
                    else:
                        bug.append(item)
                bugStrs += ["Not found in %s"%lst1N, bug]
                bugStrs += ["Not found in %s"%lst2N, lst2]
            if len(grps1) < len(grps2):
                compLists(grps1, "grps1", grps2, "grps2", u"t")
                log.warning("    Compared oXX tXX:")
                log.warning("    %s"%bugStrs)
            else:
                compLists(grps2, "grps2", grps1, "grps1", u"o")
                log.warning("    Compared tXX oXX:")
                log.warning("    %s"%bugStrs)
            return {}, False
        n1 = len(grps) / 2
        result = u""
        n = 1
        n2 = 0
        r = u"" + data.replace(u"\\", u"\\\\").replace(u"\"", u'\\"').replace(u"\r\n", u"\n").replace(u"\r", u"\n")
        log.debug("    Replacing oXX/tXX.")
        while n:
            r, n = re.subn(r"^o\d+[ ]|\no\d+[ ]", "\",\"", r, flags=re.M|re.S)
            r, n = re.subn(r"^t\d+[ ]|\nt\d+[ ]", "\":\"", r, flags=re.M|re.S)
            n2 += n
            if n2 == n1:
                n = 0
        log.debug("    Replaced %s occurences."%n2)
        result += r[2:]
        result = u"{" + result.replace(u"\r\n", u"\\n").replace(u"\n", u"\\n").replace(u"\t", u"\\t") + u"\"}"
        log.debug("    Conversion done. Loading JSON resource.")
        string_cache = json.loads(result)
        log.debug("    Setting up font.")
        line = rawData.splitlines()[0]
        if "#-# " in line:
            lngNm = line.split("#-# ")[1].strip()
        else:
            lngNm = os.path.splitext(os.path.basename(fName))[0]
        resource.setCurLang(lngNm)
        tm1 = time()
        log.debug("  * End on %s duration %s"%(tm, tm1 - tm))
    else:
        log.debug("fName is not valid beacause:")
        if not os.access(fName, os.F_OK):
            log.debug("  * Can't access file.")
        if not os.path.isfile(fName):
            log.debug("  * It's not a file.")
        if not os.access(fName, os.R_OK):
            log.debug("  * Is not readable.")
    log.debug("buildTranslation >>>")
    return string_cache, fileFound