예제 #1
0
def loadLangList(appDir):
    global i18nLangList
    
    result = []

    try:
        data = loadEntireFile(os.path.join(appDir, "langlist.txt"), True)
        data = data.decode("utf-8")
        
        # Remove BOM if present
        if data.startswith(u"\ufeff"):
            data = data[1:]
        
        for line in data.split("\n"):
            line = line.strip()
            try:
                localeStr, langTitle = line.split(u"\t", 1)
                localeStr = localeStr.encode("ascii")
                
                result.append((localeStr, langTitle))
            except:
                # Bad line
                pass
        
        i18nLangList = result
        return

    except:
        i18nLangList = [("C", u"English")]
def loadLangList(appDir):
    global i18nLangList

    result = []

    try:
        data = loadEntireFile(os.path.join(appDir, "langlist.txt"), True)
        data = data.decode("utf-8")

        # Remove BOM if present
        if data.startswith(u"\ufeff"):
            data = data[1:]

        for line in data.split("\n"):
            line = line.strip()
            try:
                localeStr, langTitle = line.split(u"\t", 1)
                localeStr = localeStr.encode("ascii")

                result.append((localeStr, langTitle))
            except:
                # Bad line
                pass

        i18nLangList = result
        return

    except:
        i18nLangList = [("C", u"English")]
예제 #3
0
def getI18nXrcData(appDir, globalConfigSubDir, baseXrcName):
    """
    Returns the XML data of translated XRC file.
    This function assumes that loadI18nDict() was previously
    called with same appDir.
    """
    global i18nPoPath, i18nLocale
    
    if i18nPoPath is None:
        # No translation
        return loadEntireFile(os.path.join(appDir, baseXrcName + ".xrc"), True)

    # Retrieve modification time of .po and .xrc file to compare with cache
    # Cache is only valid if newer than both
    poModTime = os.stat(pathEnc(i18nPoPath)).st_mtime
    poModTime2 = os.stat(pathEnc(os.path.join(appDir, baseXrcName + ".xrc"))).st_mtime

    poModTime = max(poModTime, poModTime2)

    # First test for cache in appDir
    try:
        cachePath = os.path.join(appDir,
                baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?


    # then test for cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            # Valid cache found
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?


    # No valid cache -> build content

    untranslated = loadEntireFile(
            os.path.join(appDir, baseXrcName + ".xrc"), True)

    xmlDoc = minidom.parseString(untranslated)
    elementsContainingText = xmlDoc.getElementsByTagName("label") + \
            xmlDoc.getElementsByTagName("title") + \
            xmlDoc.getElementsByTagName("item")

    for le in elementsContainingText:
        childs = le.childNodes
        if len(childs) != 1:
            continue

        child = childs[0]
        if child.nodeType != child.TEXT_NODE:
            continue

        child.data = _(child.data)

    translated = xmlDoc.toxml("utf-8")

    xmlDoc.unlink()


    # The following conversion is only needed when running a Windows
    # binary created by py2exe with wxPython 2.6.
    # Otherwise some mysterious unicode error may ocurr.

    translated = translated.decode("utf-8")
    result = []
    for c in translated:
        o = ord(c)
        if o > 127:
            result.append("&#%i;" % o)
        else:
            result.append(chr(o))

    translated = "".join(result)


    # Add version tag to ensure valid cache
    toCache = Consts.VERSION_STRING + "\n" + translated

    # Try to store content as cache in appDir
    try:
        cachePath = os.path.join(appDir,
                baseXrcName + "_" + i18nLocale + ".xrc")
        
        writeEntireFile(cachePath, toCache, True)
        
        return translated
    except:
        pass
    
    # Try to store content as cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                baseXrcName + "_" + i18nLocale + ".xrc")
        
        writeEntireFile(cachePath, toCache, True)

        return translated
    except:
        pass
    
    
    # Cache saving failed

    return translated
def getI18nXrcData(appDir, globalConfigSubDir, baseXrcName):
    """
    Returns the XML data of translated XRC file.
    This function assumes that loadI18nDict() was previously
    called with same appDir.
    """
    global i18nPoPath, i18nLocale

    if i18nPoPath is None:
        # No translation
        return loadEntireFile(os.path.join(appDir, baseXrcName + ".xrc"), True)

    # Retrieve modification time of .po and .xrc file to compare with cache
    # Cache is only valid if newer than both
    poModTime = os.stat(pathEnc(i18nPoPath)).st_mtime
    poModTime2 = os.stat(pathEnc(os.path.join(appDir,
                                              baseXrcName + ".xrc"))).st_mtime

    poModTime = max(poModTime, poModTime2)

    # First test for cache in appDir
    try:
        cachePath = os.path.join(appDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?

    # then test for cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        if os.path.exists(pathEnc(cachePath)) and \
                os.stat(pathEnc(cachePath)).st_mtime > poModTime:
            # Valid cache found
            data = _stripI18nXrcCache(loadEntireFile(cachePath, True))
            if data is not None:
                # Valid cache found
                return data
    except:
        traceback.print_exc()  # Really?

    # No valid cache -> build content

    untranslated = loadEntireFile(os.path.join(appDir, baseXrcName + ".xrc"),
                                  True)

    xmlDoc = minidom.parseString(untranslated)
    elementsContainingText = xmlDoc.getElementsByTagName("label") + \
            xmlDoc.getElementsByTagName("title") + \
            xmlDoc.getElementsByTagName("item")

    for le in elementsContainingText:
        childs = le.childNodes
        if len(childs) != 1:
            continue

        child = childs[0]
        if child.nodeType != child.TEXT_NODE:
            continue

        child.data = _(child.data)

    translated = xmlDoc.toxml("utf-8")

    xmlDoc.unlink()

    # The following conversion is only needed when running a Windows
    # binary created by py2exe with wxPython 2.6.
    # Otherwise some mysterious unicode error may ocurr.

    translated = translated.decode("utf-8")
    result = []
    for c in translated:
        o = ord(c)
        if o > 127:
            result.append("&#%i;" % o)
        else:
            result.append(chr(o))

    translated = "".join(result)

    # Add version tag to ensure valid cache
    toCache = Consts.VERSION_STRING + "\n" + translated

    # Try to store content as cache in appDir
    try:
        cachePath = os.path.join(appDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        writeEntireFile(cachePath, toCache, True)

        return translated
    except:
        pass

    # Try to store content as cache in globalConfigSubDir
    try:
        cachePath = os.path.join(globalConfigSubDir,
                                 baseXrcName + "_" + i18nLocale + ".xrc")

        writeEntireFile(cachePath, toCache, True)

        return translated
    except:
        pass

    # Cache saving failed

    return translated