#[non_exhaustive]pub struct Document {
pub format: String,
pub name: Option<String>,
pub source_lang: Option<String>,
pub target_lang: Option<String>,
pub granularity: Granularity,
pub units: Vec<Unit>,
pub skeleton: Skeleton,
pub warnings: Vec<Warning>,
pub source_encoding: Option<String>,
/* private fields */
}Expand description
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).
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.format: StringParser id, e.g. “html”, “docx”, “json”.
name: Option<String>Original file name, when known.
source_lang: Option<String>The source language (a BCP 47 tag, en, de-DE), when known.
target_lang: Option<String>The target language, when known.
granularity: GranularityHow finely the units are segmented.
units: Vec<Unit>The translatable units, in file order.
skeleton: SkeletonEverything else in the file.
warnings: Vec<Warning>Things the reader noticed but did not refuse the file for (text that looks garbled in the code page it was read with, …). Left out of the JSON when empty.
source_encoding: Option<String>The encoding a text file was decoded from when it was not UTF-8 (utf-16le, windows-1252, shift_jis,
…); None for UTF-8 (with or without BOM) and for zip and binary files. The bilingua facade fills it
from the skeleton; left out of the JSON when unset.
Implementations§
Source§impl Document
impl Document
Sourcepub fn plural_categories(&self, kind: PluralKind) -> Option<Vec<&'static str>>
pub fn plural_categories(&self, kind: PluralKind) -> Option<Vec<&'static str>>
The categories the target language uses for kind, in CLDR order and always with other (one, few,
many, other for Polish cardinals), read from target_lang through the shared CLDR table (the Babel data
Localization OS uses; aliases such as deu and iw resolve). None for a select, without a target
language, or for one the table does not know. Units hold no copy of them.
Source§impl Document
impl Document
Sourcepub fn new(
format: impl Into<String>,
units: Vec<Unit>,
skeleton: Skeleton,
) -> Document
pub fn new( format: impl Into<String>, units: Vec<Unit>, skeleton: Skeleton, ) -> Document
A document of format (the filter id) with its units and skeleton; no name, no languages, sentence
granularity, no warnings.
Sourcepub fn with_options(self, opts: &ExtractOptions) -> Document
pub fn with_options(self, opts: &ExtractOptions) -> Document
Takes the languages and the granularity from the options the file was opened with (what every parser does).
Sourcepub fn with_name(self, name: impl IntoOptionString) -> Document
pub fn with_name(self, name: impl IntoOptionString) -> Document
Sets (or with None clears) the file name.
Sourcepub fn with_source_lang(self, lang: impl IntoOptionString) -> Document
pub fn with_source_lang(self, lang: impl IntoOptionString) -> Document
Sets (or clears) the source language.
Sourcepub fn with_target_lang(self, lang: impl IntoOptionString) -> Document
pub fn with_target_lang(self, lang: impl IntoOptionString) -> Document
Sets (or clears) the target language.
Sourcepub fn with_granularity(self, granularity: Granularity) -> Document
pub fn with_granularity(self, granularity: Granularity) -> Document
Sets the granularity.
Sourcepub fn detected_source_encoding(&self) -> Option<String>
pub fn detected_source_encoding(&self) -> Option<String>
The encoding of a text skeleton (or of a LocOS-profile markdown, po or subtitle skeleton), when it is not
UTF-8: the base of its tag (utf-16le-bom and utf-16le
both give utf-16le). None for UTF-8, binary plists, zip and binary skeletons.
Sourcepub fn with_warnings(self, warnings: Vec<Warning>) -> Document
pub fn with_warnings(self, warnings: Vec<Warning>) -> Document
Replaces the warnings.
Sourcepub fn segments(&self) -> impl Iterator<Item = (&Unit, &Segment)>
pub fn segments(&self) -> impl Iterator<Item = (&Unit, &Segment)>
Every segment with its unit, in order.
Sourcepub fn segment_count(&self) -> usize
pub fn segment_count(&self) -> usize
The number of segments.
Sourcepub fn find(&self, seg_id: &str) -> Option<(&Unit, &Segment)>
pub fn find(&self, seg_id: &str) -> Option<(&Unit, &Segment)>
The segment with this id and its unit.
Sourcepub fn set_target(
&mut self,
seg_id: &str,
target: Vec<Inline>,
) -> Result<(), Error>
pub fn set_target( &mut self, seg_id: &str, target: Vec<Inline>, ) -> Result<(), Error>
Sets a segment’s target. Rejects unknown segments, untranslatable units, segments in state
SegState::Locked, and tags the source lacks or repeats. A missing tag is allowed (the writer leaves it out), as in catcore.
The first call builds an index of segment ids (one pass over the document); later calls find the segment
through it, so setting every segment of a 12 000 unit file is linear, not quadratic. The index is checked
on each use and rebuilt when the units were edited since (a segment added, removed or renamed through the
public fields); for many segments at once Document::set_targets is still the lighter call. When two
segments share an id, the first one in the document wins as long as the units are not edited between
calls.
Sourcepub fn set_target_repeating(
&mut self,
seg_id: &str,
target: Vec<Inline>,
) -> Result<(), Error>
pub fn set_target_repeating( &mut self, seg_id: &str, target: Vec<Inline>, ) -> Result<(), Error>
Document::set_target that lets a translation repeat a standalone placeholder tag (the # of an ICU
plural, Chrome $x$, go {{..}}), as Localization OS does; paired tags still keep the exactly-once rule.
Sourcepub fn set_targets<I, S>(&mut self, items: I) -> Result<(), Error>
pub fn set_targets<I, S>(&mut self, items: I) -> Result<(), Error>
Sets many targets in one pass: one id index is built for the whole call, so 100 000 segments take
milliseconds where 100 000 Document::set_target calls scan the document each time. Items are applied in
order with the same rules as set_target; the first error stops the call and is returned, items before it
stay applied. When an id appears more than once, the segment is set by each item in turn (the last wins).
Sourcepub fn set_states<I, S>(&mut self, items: I) -> Result<(), Error>
pub fn set_states<I, S>(&mut self, items: I) -> Result<(), Error>
Sets many states in one pass (see Document::set_targets).
Sourcepub fn set_target_empty(&mut self, seg_id: &str) -> Result<(), Error>
pub fn set_target_empty(&mut self, seg_id: &str) -> Result<(), Error>
Marks a segment as translated to nothing on purpose (see Segment::empty_target): the writer writes an
empty value for it. State Draft, as for any new target. Rejects unknown segments, untranslatable units
and locked segments.