TypesXLIFF - TypeScript Library for XLIFF Parsing, Generation and Validation
TypesXLIFF is a TypeScript / Node.js library for parsing, generating, and validating XLIFF
2.x files (2.0, 2.1 and 2.2). It includes a fully typed object model and JSON conversion for
processing translation and localization data.
The source code is available on GitHub under the Eclipse
Public License v1.0. Developers can clone, adapt, and ship the library under the
terms of that license, or contact Maxprograms for commercial arrangements.
Quick example
Load an XLIFF file and read the first source segment:
import { XliffParser } from 'typesxliff';
const parser = new XliffParser();
await parser.parseFile("file.xlf");
const doc = parser.getXliffDocument();
const segment = doc?.files[0].units[0].segments[0];
console.log(segment?.source.toString());
Why TypesXLIFF
Most TypeScript tools for XLIFF either parse the XML into plain objects without a structured model,
or rely on generic XML libraries that expose no XLIFF-specific semantics. TypesXLIFF sits between
those extremes: it provides a fully typed class hierarchy that mirrors the XLIFF 2.x specification,
so you can build, inspect, and validate documents without writing raw XML.
- Full XLIFF 2.x object model — not just parsing into plain objects
- Type-safe API for building and modifying documents programmatically
- JSON round-trip for integration with other systems
- Built on TypesXML — streaming XML parser with validation support
Features
Parsing
- Load any XLIFF 2.x file into a fully typed object model using
XliffParser
- Optional XML Catalog support for schema resolution and default attribute population
Building & serialization
- Construct
XliffDocument instances from scratch using the provided model classes
- Serialize any
XliffDocument back to a well-formed XML file using
XliffDocument.writeDocument()
Validation
- Each model element exposes an
isValid() method that checks structural and semantic
constraints against the XLIFF 2.x specification
JSON conversion
- Convert XLIFF ⇄ JSON (lossless) using
XliffToJson and JsonToXliff,
built on the round-trip JSON conversion provided by TypesXML
Use Cases
- Parse XLIFF 2.x files in Node.js or TypeScript
- Generate XLIFF documents programmatically
- Convert XLIFF to/from JSON
- Validate XLIFF structure and content
- Extract terminology and bilingual term pairs from XLIFF content
- Build localization or translation pipelines
Parsing an existing XLIFF file
import { Catalog } from "typesxml";
import { XliffParser, XliffDocument } from "typesxliff";
const parser = new XliffParser();
// Using a catalog is optional. When provided, the SAX parser can resolve grammar
// schemas and populate default attribute values declared in them.
// A sample catalog covering XLIFF 2.0, 2.1 and 2.2 is included in the catalog/ folder.
parser.setCatalog(new Catalog('/path/to/typesxliff/catalog/catalog.xml'));
parser.parseFile('/path/to/file.xlf');
const doc: XliffDocument | undefined = parser.getXliffDocument();
Building and writing an XLIFF document
import { XliffDocument, XliffFile, XliffUnit, XliffSegment, XliffSource } from "typesxliff";
const doc = new XliffDocument("2.1", "en", "es");
const file = new XliffFile("f1");
const unit = new XliffUnit("u1");
const segment = new XliffSegment("s1");
const source = new XliffSource();
source.addContent("Hello, world!");
segment.setSource(source);
unit.addItem(segment);
file.addEntry(unit);
doc.addFile(file);
doc.writeDocument('/path/to/output.xlf', true);
Built with TypesXLIFF
These tools are built on TypesXLIFF and show practical uses of the library:
A command-line tool for validating XLIFF 2.x files. It uses TypesXLIFF's object model and
isValid() methods to check structural and semantic constraints against the XLIFF 2.x
specification, and supports custom grammar catalogs for schema resolution.
npm install -g xliff-validation
xliffvalidator -xliff file.xlf
A TypeScript library and command-line tool for extracting term candidates from XLIFF 2.x files using
the YAKE! statistical algorithm. Supports monolingual extraction (source terms ranked by casing,
position, and frequency) and bilingual extraction (source/target translation pairs from segments
with confirmed translations).
npm install -g typesterms
termextractor -xliff file.xlf
bilingualextractor -xliff file.xlf