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.
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());- Full XLIFF 2.x object model (not just parsing)
- Type-safe API for building and modifying documents
- JSON round-trip for integration with other systems
- Built on TypesXML (streaming XML parser with validation support)
- Parse XLIFF files — Load an existing XLIFF 2.x file into a fully typed object model using
XliffParser - Build programmatically — Construct
XliffDocumentinstances from scratch using the provided model classes - Write XLIFF files — Serialize any
XliffDocumentback to a well-formed XML file usingXliffDocument.writeDocument() - JSON round-trip — Convert XLIFF ⇄ JSON (lossless) using
XliffToJson, and reconstruct it back usingJsonToXliff. Built on the round-trip JSON conversion provided by TypesXML - Validate — Each model element exposes an
isValid()method that checks structural and semantic constraints against the XLIFF 2.x specification
- Parse XLIFF 2.x files in Node.js or TypeScript
- Generate XLIFF documents programmatically
- Convert XLIFF to/from JSON
- Validate XLIFF structure and content
- Build localization or translation pipelines
TypesXLIFF provides type-safe classes for building, parsing, and serializing XLIFF documents. It covers:
- XLIFF versions: 2.0, 2.1 and 2.2
- Core structural elements:
<xliff>,<file>,<skeleton>,<group>,<unit>,<segment>,<ignorable>,<notes>,<note>,<originalData>,<data>,<source>and<target> - Inline elements:
<cp>,<ph>,<pc>,<sc>,<ec>,<mrk>,<sm>and<em> - Metadata module:
<metadata>,<metaGroup>and<meta> - Translation Candidates module:
<matches>and<match> - Glossary module:
<glossary>,<glossEntry>,<term>,<translation>and<definition>
Each class includes validation (isValid()) and XML serialization (toElement()) methods.
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();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);- TypesXML — XML object model and SAX parser
- TypesBCP47 — BCP 47 language tag utilities
npm install typesxliffnpm install
npm run build