Skip to main content

Crate bilingua_model

Crate bilingua_model 

Source
Expand description

Bilingua document model: what every file parser extracts and what it writes back.

A parser (Filter) turns a source file into a Document: translatable Units (a paragraph, a cell, a string) and a Skeleton that holds everything else byte for byte. Each unit holds one or more Segments (one before segmentation; sentences after bilingua-seg splits it). Formatting inside the text is kept as inline Inline::Tag codes whose original markup lives in Unit::codes, so a writer can put it back exactly.

JSON shape: Inline and TagKind serialise exactly like catcore::Inline and catcore::TagKind ({"text": ".."} / {"tag": {"id", "kind": "open|close|standalone", "display"}}), so one editor UI shows bilingual files (catcore) and source files (Bilingua) the same way. Every field added since is optional and left out of the JSON when empty: a tag’s free flag, a segment’s origin, a unit’s notes and lock_reason, a skeleton’s parser.

Bilingual files (XLIFF and the like) use the same model: a segment may carry a target, a state and an origin (with a match percentage), and a unit may carry notes and a lock reason. Monolingual formats leave them empty.

profile holds what one consumer needs and no other (the Localization OS profile, and profile::tokens, which converts inline codes to and from LocOS’s text tokens, one token per tag).

Re-exports§

pub use guard::LimitError;
pub use guard::LimitKind;
pub use guard::Limits;
pub use pytext::py_is_word_char;
pub use pytext::py_is_word_start_char;
pub use pytext::py_isdecimal_char;
pub use pytext::py_isalnum_char;
pub use pytext::py_isalpha_char;
pub use pytext::py_isspace_char;
pub use pytext::py_has_alnum;
pub use pytext::py_has_alpha;
pub use pytext::py_isalnum;
pub use pytext::py_isalpha;
pub use pytext::py_isspace;
pub use pytext::py_lstrip;
pub use pytext::py_rstrip;
pub use pytext::py_strip;
pub use encoding_rs;

Modules§

b64
Raw bytes in JSON as a base64 string (standard alphabet, padded). Reading also accepts the old form, an array of integers, so skeletons saved before this change still load.
codepage
Windows code pages per language: which legacy encoding an undeclared, non-UTF-8 text file most likely uses when its project’s source language is known (Localization OS decision 22, code_pages.py).
enc
Reading and writing text files in the encoding they came in (Localization OS read_text_upload, decisions 22 and B-bytes).
garble
“The decoded letters look wrong”: the script-mix checks Localization OS runs on a text it had to read with a guessed code page (text_looks_garbled, decision 22). Heuristics, deliberately conservative: a warning is never an error.
guard
Safety limits and the XML guard, one place for every parser (D27 step 5).
profile
Profiles: named sets of choices one consumer of Bilingua needs, kept out of the general API. There is one, locos: what the Localization OS adapter needs to give LocOS the files and ids it had before Bilingua.
pytext
Python’s character tests, exactly: str.isalpha, str.isalnum and str.isspace of Python 3.13 (Unicode 15.1), which Localization OS uses for its “has a letter or digit” and whitespace rules.

Macros§

parser_info
The producing parser of the running crate: parser_info!() expands to a ParserInfo with the calling crate’s name, version and crate::SKELETON_SCHEMA. Every format crate defines that constant (pub const SKELETON_SCHEMA: u32 = 1;) and bumps it only when its skeleton changes. parser_info!(n) stamps an explicit schema instead.

Structs§

CodeData
The original markup of one inline code, kept so the writer can reproduce it. A paired code fills open and close; a standalone code fills standalone. Build it with CodeData::paired, CodeData::standalone or CodeData::new and the with_* methods.
Document
An extracted file: its units and its skeleton. Build it with Document::new and the with_* methods; edit targets with Document::set_target and friends, which check the tags. Default is an empty document of no format (no writer accepts it).
ExtractOptions
Options for Filter::extract. Build them with ExtractOptions::new and the with_* methods (ExtractOptions::new().with_target_lang("de-DE")); new options are added without breaking callers.
FormatOptionKey
One option a format understands in ExtractOptions::format_options: its full key ("csv.columns"), the kind of value, and a line of help for tools that list the options.
MergeOptions
Options for Filter::merge_with. The default is what Filter::merge does. They live here, once, and every format that stamps a language, fills plurals or writes text honours them:
Note
A comment on a unit: a translator query, a developer instruction, a reviewer remark. Build it with Note::new.
Origin
How a target came to be, with the match percentage when it came from memory. Build it with Origin::new.
ParserInfo
Which parser made a skeleton: the crate name, its version (for information) and the skeleton schema number. The schema is an integer each format crate bumps only when its skeleton layout changes, so a new crate release that did not touch the layout still merges old skeletons (see check_parser).
Segment
One translatable piece of a unit: its source, and its target once there is one. Build it with Segment::new and the with_* methods.
Unit
One translatable unit of the file (a paragraph, a cell, a string): its segments, the original markup of its inline codes and its context. Build it with Unit::new or Unit::from_segments and the with_* methods.
UnitContext
What helps a translator, an AI lane or a preview understand a unit. Build it with UnitContext::new and the with_* methods; extra takes anything without a field of its own.
Warning
A notice about a file that was read anyway: it never stops a merge. kind is a stable word for programs (“text_encoding”); message is plain text safe to show the person who uploaded the file.
ZipEntry
One entry of a zip skeleton, in the archive’s order. Build it with ZipEntry::new or ZipEntry::raw.

Enums§

Error
Every Bilingua failure. Match with a wildcard arm: a 1.x release may add variants.
Granularity
How finely units are split into segments.
Inline
One piece of segment content, in logical order.
OptionKind
The kind of value a format option takes.
OriginKind
Where a segment’s target came from. Match with a wildcard arm: a 1.x release may add kinds (a JSON file with a kind this release does not know reads as Other).
OutputEncoding
How a merge writes text files.
SegState
Where a segment is in its life, from untouched to signed off. Monolingual files only use New, Draft, Translated and Confirmed; bilingual files and review workflows use the rest.
SkelPart
One piece of a text skeleton: literal output, or the place where a unit’s text goes. Closed: anything else a format needs lives in its own parts’ text or in Unit::hidden.
Skeleton
Everything in the file that is not translatable text. New kinds of skeleton come as new variants (match with a wildcard arm); the fields of each variant are fixed for 1.x (a format records more in its parts, in the encoding tag’s ;name=value items, or in Unit::hidden).
TagKind
The kind of an inline code. Closed: a code opens, closes or stands alone.
ZipData
The content of one zip entry in a skeleton. Closed: copied bytes or rebuilt text.

Traits§

Filter
A file parser. Implementations live in one crate per format (bilingua-html, bilingua-docx, …).
IntoOptionString
What a builder takes for an optional text field: "de-DE", a String, or an Option of either (None clears the field). impl Into<Option<String>> would refuse a plain &str, so the builders take this instead.

Functions§

check_format_options
Checks every format_options entry against the keys the filters declare: an unknown key or a value of the wrong kind is an Error::FormatOption. The bilingua facade calls this with every filter’s keys, so an option meant for another format (the adapter sends docx.translate_comments with every file) is accepted.
check_option_map
check_format_options for any map of options (MergeOptions::format_options too).
check_own_options
The check every filter makes when it is called directly (not through the bilingua facade), first thing in extract (with &opts.format_options) and in merge_with (with its MergeOptions::format_options): an option in one of the filter’s own families (its id, and the <family>. prefix of every key it declares) that it does not declare is refused (a misspelt csv.colums would otherwise be ignored), and so is a value of the wrong kind for a key it declares. Keys of other formats are left alone: one option map may be meant for many formats, and only the facade, which knows every filter’s keys, can tell a good foreign key from a misspelt one.
check_parser
Refuses a document whose skeleton came from another crate or from a newer schema than current. Older schemas and skeletons with no record (written before stamps existed) are accepted; a crate that stops reading some old schema uses check_parser_min.
check_parser_min
Like check_parser, but also refuses schemas older than oldest, the oldest one the crate still reads.
merge_text
Joins neighbouring Text pieces.
plain_text
Plain text with the tags removed.