Skip to content
elcoospPublic

About

Extract and compile translations from HTML, XML, and MJML to standard PO files. The robust, WASM-ready Rust CLI that bridges your codebase with professional localization platforms like Crowdin and Weblate. πŸŒβœ‰οΈπŸ¦€

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

51 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

xml2po Logo

Markup-aware translation workflows for XML, HTML and MJML
Extract to PO, translate anywhere (gettext, Weblate, Crowdin, CAT tools via XLIFF), compile back β€” without ever losing a translation.

Rust Crates.io License MIT WASM Support


xml2po streamlines localization for XML-flavored templates: it extracts translatable strings into standard PO files, merges translator work back without data loss, and compiles translations into the original documents while preserving whitespace, entities and markup byte-for-byte.

Highlights

  • Inline markup placeholders β€” <p>Hello <b>bold</b> world</p> is one msgid: Hello <b>bold</b> world. Sentences stay whole, translators reorder words freely, and compilation re-inserts the original tags (attributes included). Translations whose markup doesn't match are rejected safely and counted in the report.
  • ITS-lite semantics β€” HTML5 translate="no|yes" and its:translate are honored by default. Non-translatable inline content (<span translate="no">ACME</span>) is kept verbatim inside the msgid, itstool-style.
  • Lossless PO layer β€” full plural round-trip (msgstr[N]), #, flags (fuzzy translations are skipped unless you ask for them), #| previous msgids, #~ obsolete entries preserved across merges, typed header metadata (PoMetadata), and strict duplicate detection.
  • CI gates β€” check fails on catalog drift or low coverage; stats --json / check --json for machine-readable pipelines.
  • XLIFF 1.2 bridge β€” exchange catalogs with Trados, memoQ, Lokalise, Transifex and friends (catalog_to_xliff / catalog_from_xliff).
  • WebAssembly ready β€” compiles to wasm32-unknown-unknown for in-browser localization workflows.
  • Error messages with line:column β€” XML failures point at the exact position, not a byte offset.

Installation

CLI

cargo install xml2po

Library

[dependencies]
xml2po = "0.3"

Quickstart

Command line

# Extract translatable strings (inline placeholders on by default)
xml2po extract src/*.html --output messages.po

# Refresh a catalog after source changes: translations are kept,
# removed strings become #~ obsolete entries
xml2po update src/*.html --output messages.po

# Compile a translated document
xml2po compile --input page.html --po fr.po --output fr.html

# CI: fail when the catalog drifts or coverage drops below 90%
xml2po check --min-coverage 90 messages.po src/*.html
xml2po stats --json messages.po

Library

use xml2po::{extract_messages, apply_translations, get_strategy};
use xml2po::po::PoCatalog;

let content = r#"<p>Hello <b>bold</b> world</p>"#;
let strategy = get_strategy("html")?;

// One message per sentence β€” inline tags folded in.
let messages = extract_messages(content, "page.html", strategy)?;
assert_eq!(messages[0].msgid, "Hello <b>bold</b> world");

// Translate and compile: the original <b> tag is re-inserted.
let mut catalog = PoCatalog::from_messages(&messages);
catalog.set_translation(
    xml2po::message::CatalogKey::new(None, "Hello <b>bold</b> world"),
    "Hola <b>negrita</b> mundo",
);
let translated = apply_translations(content, &catalog.translations(), strategy)?;
assert_eq!(translated, "<p>Hola <b>negrita</b> mundo</p>");

How inline placeholders work

During extraction, inline elements (b, em, span, a, strong, br, …) fold into the surrounding message as attribute-free tags, and literal text is XML-escaped (& β†’ &amp;, < β†’ &lt;) so msgids stay unambiguous:

<p>Name: <span translate="no">ACME</span> inc.</p>
msgid "Name: <span translate=\"no\">ACME</span> inc."

During compilation the tag sequences of msgid and msgstr must match (same tags, same order). Text between tags may be reordered, split or merged freely β€” word order can change between languages. Each translation tag is replaced by the corresponding source tag with its original attributes:

<p>Name: <span translate="no">ACME</span> GmbH</p>
Translation Result
Name: <span translate="no">ACME</span> Ltd. βœ… applied
Name: <span>ACME</span> Ltd. (missing attr) βœ… tags match by name β€” applied, source form restored
Name: ACME Ltd. (tags dropped) ❌ rejected, source kept, skipped_invalid +1
Name: </span><span>ACME (reordered) ❌ rejected

Opt out any time: ExtractionOptions::builder().inline_placeholders(false) or the CLI flag --no-inline restores classic per-segment extraction.

Supported formats & elements

Format Translatable elements Inline elements Translatable attributes
MJML mj-title, mj-text, mj-button, strong, em strong, em β€”
HTML p, h1–h6, span, div, a, button, label, li, td, th, title, option, strong, em, b, i the HTML phrasing set (a, abbr, b, br, em, i, kbd, mark, q, small, span, strong, sub, sup, time, u, var, …) alt, title, placeholder, aria-label
XML everything (generic fallback) β€” (opt in via a custom strategy) β€”

Note

Smart filtering β€” the extraction engine automatically excludes URLs, e-mail addresses, file paths, lone entities (&nbsp;), hex colors, pure numbers/symbols and placeholder-only strings (%s, {{name}}).

Custom formats: implement ExtractionStrategy β€” is_translatable_element, is_ignored_element, translatable_attributes and (new in 0.3) is_inline_element β€” and pass it to extract_messages / apply_translations.

Smart merge: never lose a translation

extract --merge (or the update subcommand) implements gettext msgmerge semantics:

  • strings still present keep their translation, flags, comments and plural forms;
  • strings removed from the sources become #~ obsolete entries;
  • strings that come back are resurrected with their old translation;
  • fuzzy translations are kept but skipped during compilation unless --use-fuzzy (or PoCatalog::translations_with(true)) is set.

CI integration

- name: Check translations
  run: |
    xml2po update src/templates/*.html --output locales/messages.po
    xml2po check --min-coverage 90 locales/messages.po src/templates/*.html
  • check exits 0 only when the catalog is in sync and coverage is at or above --min-coverage.
  • check --json prints {"clean":true,"coverage":95.0,"added":[],"removed":[]}.
  • stats --json prints {"total":42,"translated":40,"untranslated":2,"coverage":95.2}.
  • --recursive --exclude 'drafts/*' extracts whole directory trees with glob exclusions.

Tip

WebAssembly usage β€” compile to wasm32-unknown-unknown and call extract_to_po / compile_with_po from JavaScript via wasm-bindgen:

const po = extractToPo(mjmlContent, "mjml", "template.mjml");
const translated = compileWithPo(htmlContent, po, "html");

Cargo features

Feature Description Default
mjml MJML format extraction and compilation βœ…
html HTML format extraction and compilation βœ…
xml Generic XML fallback strategy βœ…
chrono Timestamps in generated PO headers βœ…
serde Serialize/Deserialize for messages, options and catalogs ❌
xliff XLIFF 1.2 export/import βœ…
cli The command-line interface binary βœ…
parallel Rayon-parallel extraction of multiple input files βœ…
cli-encoding UTF-16 (BOM) input support in the CLI βœ…

PO feature matrix

PO construct Extract Parse Serialize Compile
msgctxt βœ… (opt-in) βœ… βœ… βœ…
msgid_plural + msgstr[N] β€” βœ… βœ… msgstr[0]
#: references (file:line) βœ… βœ… βœ… β€”
#, flags β€” βœ… βœ… fuzzy policy
`# ` previous msgids β€” βœ… βœ…
#~ obsolete entries βœ… (merge) βœ… βœ… skipped
Header metadata βœ… βœ… (PoMetadata) βœ… β€”
Wrapping at 79 cols / --no-wrap βœ… βœ… βœ… β€”

Development

# Run the test suite (unit + CLI integration + doc tests)
cargo test --all-features

# Lint with the pedantic configuration used by CI (warnings are errors)
cargo clippy --all-features --all-targets -- -W clippy::pedantic --deny warnings

# Check formatting
cargo fmt --all -- --check

# Test WebAssembly build
rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

# Measure coverage (requires cargo-llvm-cov)
cargo llvm-cov --all-features

A runnable library example lives in examples/translate_flow.rs: cargo run --example translate_flow.

Important

When contributing a new format, implement the ExtractionStrategy trait (elements, ignored elements, translatable attributes, inline elements) and add a variant to Format. Refer to the built-in strategies for the pattern.

License

MIT β€” see LICENSE.

About

Extract and compile translations from HTML, XML, and MJML to standard PO files. The robust, WASM-ready Rust CLI that bridges your codebase with professional localization platforms like Crowdin and Weblate. πŸŒβœ‰οΈπŸ¦€

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages