def convertStringsFile(src, dest):

    dir = os.path.dirname(dest)

    if not os.path.exists(dir):
        try:
            os.makedirs(dir)
        except OSError:
            # can't create directory to hold .po file
            return

    # Parse the binary plist .strings file:
    parser = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_
    data = NSData.dataWithContentsOfMappedFile_(src)
    strings, format, error = parser(data, NSPropertyListImmutable, None, None)
    if error:
        raise ParseError(error)

    # The format of GNUtext MO files is described here:
    # http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html

    strings = dict(strings)
    originals = strings.keys()
    originals.sort()

    descriptors = []
    keys = ''
    values = ''

    for original in originals:
        translation = strings[original]

        origStr = original.encode("UTF-8")
        transStr = translation.encode("UTF-8")

        descriptors.append((len(keys), len(origStr), len(values),
            len(transStr)))
        keys += origStr + '\0' # <NUL> terminated
        values += transStr + '\0'

    # The header is 28 bytes, each descriptor is 8 bytes, with two descriptors
    # per string (one pointing at original, one pointing at translation)
    keysOffset = 28 + len(originals) * 2 * 8
    valuesOffset = keysOffset + len(keys)

    keyDescriptors = []
    valueDescriptors = []
    for origOffset, origLen, transOffset, transLen in descriptors:
        keyDescriptors.append(origLen)
        keyDescriptors.append(keysOffset + origOffset)
        valueDescriptors.append(transLen)
        valueDescriptors.append(valuesOffset + transOffset)

    result = struct.pack(
        "Iiiiiii",
        0x950412DEL,           # magic number
        0,                     # file format revision
        len(originals),        # number of strings
        28,                    # offset of table with original strings
        28 + len(originals) * 8, # offset of table with translation strings
        0,                     # size of hashing table
        0                      # offset of hashing table
    )
    result += array.array("i", keyDescriptors).tostring()
    result += array.array("i", valueDescriptors).tostring()
    result += keys
    result += values

    with open(dest, "wb") as outFile:
        outFile.write(result)
Esempio n. 2
0
def convertStringsFile(src, dest):

    dir = os.path.dirname(dest)

    if not os.path.exists(dir):
        try:
            os.makedirs(dir)
        except OSError:
            # can't create directory to hold .po file
            return

    # Parse the binary plist .strings file:
    parser = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_
    data = NSData.dataWithContentsOfMappedFile_(src)
    strings, format, error = parser(data, NSPropertyListImmutable, None, None)
    if error:
        raise ParseError(error)

    # The format of GNUtext MO files is described here:
    # http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html

    strings = dict(strings)
    originals = strings.keys()
    originals.sort()

    descriptors = []
    keys = ''
    values = ''

    for original in originals:
        translation = strings[original]

        origStr = original.encode("UTF-8")
        transStr = translation.encode("UTF-8")

        descriptors.append(
            (len(keys), len(origStr), len(values), len(transStr)))
        keys += origStr + '\0'  # <NUL> terminated
        values += transStr + '\0'

    # The header is 28 bytes, each descriptor is 8 bytes, with two descriptors
    # per string (one pointing at original, one pointing at translation)
    keysOffset = 28 + len(originals) * 2 * 8
    valuesOffset = keysOffset + len(keys)

    keyDescriptors = []
    valueDescriptors = []
    for origOffset, origLen, transOffset, transLen in descriptors:
        keyDescriptors.append(origLen)
        keyDescriptors.append(keysOffset + origOffset)
        valueDescriptors.append(transLen)
        valueDescriptors.append(valuesOffset + transOffset)

    result = struct.pack(
        "Iiiiiii",
        0x950412DEL,  # magic number
        0,  # file format revision
        len(originals),  # number of strings
        28,  # offset of table with original strings
        28 + len(originals) * 8,  # offset of table with translation strings
        0,  # size of hashing table
        0  # offset of hashing table
    )
    result += array.array("i", keyDescriptors).tostring()
    result += array.array("i", valueDescriptors).tostring()
    result += keys
    result += values

    with open(dest, "wb") as outFile:
        outFile.write(result)