locize / xliff

xliff2js and js2xliff converter xliff utils
MIT License
80 stars 37 forks source link

TypeError: Cannot read properties of undefined (reading 'id') #56

Closed adadgio closed 1 year ago

adadgio commented 1 year ago

🐛 Bug Report

The following error occurs on seamingly valid XLIFF files (using https://dev.maxprograms.com/Validation/ to validate).

To Reproduce

Xliff contents:

<?xml version="1.0" encoding="utf-8" ?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2">
    <file date="2020-02-11T14:49:15Z" source-language="fr" target-language="es" datatype="plaintext" original="not.available">
        <header>
            <tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/>
            <note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
        </header>
        <body>
            <trans-unit id="a1" resname="item1">
                <source>item1</source>
                <target>Transaltion 1</target>
            </trans-unit>
            <trans-unit id="a2" resname="item2">
                <source>item2</source>
                <target>Translation 2</target>
            </trans-unit>
        </body>
    </file>
</xliff>
import xliff from 'xliff'

const fileEs = fs.readFileSync(`${ROOT_DIR}/src/models/refs/test.xlf`, 'utf-8')

;(async () => {
    try {
        const result = await xliff.xliff2js(fileEs)
        console.log(result)

    } catch (e: any) {
        // general error here
        console.log(e)
    }
})()

Error:

ts-node src/models/refs/xliff.ts
TypeError: Cannot read properties of undefined (reading 'id')

Expected behavior

A JSON valid output (or a readable error).

Your Environment

adrai commented 1 year ago

You are using the wrong xliff2js function...

Try xliff.xliff12ToJs().

adadgio commented 1 year ago

Will try. Thank you.