pofile 5.0.0-beta.0

Rust core for parsing and serializing GNU gettext PO files.
Documentation
  • Coverage
  • 99.18%
    241 out of 243 items documented0 out of 98 items with examples
  • Size
  • Source code size: 147.83 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 17.7 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 27s Average build duration of successful builds.
  • all releases: 27s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • sebastian-software/pofile-ts
    1 0 2
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • swernerx

pofile

pofile is the Rust core behind pofile-ts.

It provides:

  • PO parsing and serialization
  • ICU MessageFormat parsing and compilation
  • Catalog compilation for runtime lookup
  • Message ID generation and plural helpers
  • A host interface for locale-aware formatting and tag rendering

The crate is intended to be usable directly from Rust and to serve as the shared core for thin host bindings such as Node.js.

Installation

cargo add pofile

Parse and stringify PO files

use pofile::{parse_po, stringify_po, SerializeOptions};

let po = parse_po(
    r#"
msgid ""
msgstr ""
"Language: de\n"

msgid "Hello"
msgstr "Hallo"
"#,
);

assert_eq!(po.items[0].msgid, "Hello");
assert_eq!(po.items[0].msgstr, vec!["Hallo"]);

let rendered = stringify_po(&po, SerializeOptions::default());
assert!(rendered.contains(r#"msgid "Hello""#));

Compile ICU messages

use pofile::{compile_icu, CompileIcuOptions, MessageValue, MessageValues};

let compiled = compile_icu(
    "{count, plural, one {# file} other {# files}}",
    &CompileIcuOptions::new("en"),
)
.expect("message should compile");

let values = MessageValues::from([("count".to_owned(), MessageValue::from(2usize))]);
assert_eq!(compiled.format(&values), "2 files");

Compile catalogs

use pofile::{
    compile_catalog, generate_message_id, Catalog, CatalogEntry, CatalogTranslation,
    CompileCatalogOptions, MessageValue, MessageValues,
};

let catalog = Catalog::from([(
    "Hello {name}!".to_owned(),
    CatalogEntry {
        translation: Some(CatalogTranslation::Singular("Hallo {name}!".to_owned())),
        ..CatalogEntry::default()
    },
)]);

let compiled = compile_catalog(&catalog, &CompileCatalogOptions::new("de"))
    .expect("catalog should compile");
let key = generate_message_id("Hello {name}!", None);
let values = MessageValues::from([("name".to_owned(), MessageValue::from("Sebastian"))]);

assert_eq!(compiled.format(&key, &values), "Hallo Sebastian!");

Host formatting

pofile owns parsing, AST traversal, plural selection, and catalog dispatch. Host-specific formatting can be provided through FormatHost.

That lets direct Rust integrations and host bindings share the same execution model without duplicating ICU/plural logic in each embedding.