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 as 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))
    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("Default strings will be used.")
        str_cache = {}
    if not extend:
        string_cache = str_cache
    log.debug("buildTranslation >>>")
    return string_cache, fileFound
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).
albow/translate.py