bilingua-model 0.10.0

Bilingua: the extracted-document model shared by every Bilingua file parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! 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.
//!
//! A caller switches a profile on with `ExtractOptions::with_profile("locos")` (and `MergeOptions` from
//! [`Profile::merge_options`]); the `bilingua` facade refuses a name [`find`] does not know. Format crates may switch
//! behaviour on [`ExtractOptions::is_profile`] / [`MergeOptions::is_profile`] (Track D adds the per-format LocOS
//! behaviours that way); [`annotate`] adds what the profile promises to every unit after extraction.
//!
//! [`tokens`] converts inline codes to and from the text tokens Localization OS uses, one token per tag.

use crate::{CodeData, Document, Error, ExtractOptions, Inline, MergeOptions, OutputEncoding, TagKind, Unit};
use serde::Serialize;

pub mod tokens;

/// The Localization OS profile's name.
pub const LOCOS: &str = "locos";
/// Its version: bumped when what the profile produces changes, so the adapter can tell stored documents apart.
pub const LOCOS_VERSION: u32 = 1;
/// `UnitContext::extra` key with the unit's LocOS paragraph index (`p_index`), set under the LocOS profile by the
/// formats that have one. Left unset where Bilingua cannot give LocOS's number yet (the adapter then numbers by
/// position).
pub const P_INDEX: &str = "locos.p_index";
/// `UnitContext::extra` key with the unit's LocOS style (`a11y:*`, `meta:*`, ...), set under the LocOS profile by
/// the formats that have one; unset otherwise.
pub const STYLE: &str = "locos.style";
/// `UnitContext::extra` key, LocOS profile: how many images LocOS's accessibility scan anchors at this unit
/// (LocOS `skeleton["a11y_images"]["images"]` is the sum over the units). Unset: none.
pub const A11Y_IMAGES: &str = "locos.a11y_images";
/// `UnitContext::extra` key, LocOS profile: the images anchored at this unit whose alt text is empty, one `where`
/// (the image's file name) per line, in file order (LocOS `missing_alt` entries of kind `empty`). Unset: none.
pub const A11Y_EMPTY_ALT: &str = "locos.a11y_empty_alt";
/// `UnitContext::extra` key, LocOS profile: every missing-alt finding anchored at this unit, of both kinds, as a JSON
/// list of `{"kind": "missing" | "empty", "where": <image name>}` in file order (LocOS `missing_alt` entries; their
/// `unit` is this unit's `p_index`). Set by DOCX; unset: none.
pub const A11Y_MISSING_ALT: &str = "locos.a11y_missing_alt";

/// A named profile and its version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Profile {
    /// Its name (`"locos"`).
    pub name: &'static str,
    /// Its version.
    pub version: u32,
}

/// The Localization OS profile.
pub fn locos() -> Profile {
    Profile { name: LOCOS, version: LOCOS_VERSION }
}

/// The oldest version of the LocOS profile this release can still produce on request. A release that bumps
/// [`LOCOS_VERSION`] keeps the old behaviour reachable (`resolve("locos@1")`), the way LocOS keeps its own walk and
/// reading stamps, so a document stored under the old version still rebuilds; it raises this only when an old version
/// cannot be replayed any more, and the adapter then asks for a re-import.
pub const LOCOS_OLDEST_VERSION: u32 = 1;

/// Every profile Bilingua knows.
pub fn profiles() -> Vec<Profile> {
    vec![locos()]
}

/// The profile with this name.
pub fn find(name: &str) -> Option<Profile> {
    profiles().into_iter().find(|p| p.name == name)
}

/// The profile a caller asks for, at a version: `spec` is a name (`"locos"`) or a name and a version
/// (`"locos@1"`); `version`, when given, must agree with a version in `spec`. No version means the current one. An
/// unknown name is `Error::UnknownProfile`; a version this release cannot produce (newer than the current one, or
/// older than the oldest it replays) is `Error::ParserVersion`, so the caller can ask for a re-import.
pub fn resolve(spec: &str, version: Option<u32>) -> Result<Profile, Error> {
    let (name, in_spec) = match spec.split_once('@') {
        Some((n, v)) => {
            let v: u32 = v.parse().map_err(|_| Error::UnknownProfile(spec.to_string()))?;
            (n, Some(v))
        }
        None => (spec, None),
    };
    let current = find(name).ok_or_else(|| Error::UnknownProfile(spec.to_string()))?;
    let want = match (in_spec, version) {
        (Some(a), Some(b)) if a != b => return Err(Error::UnknownProfile(format!("{spec} (asked for version {b})"))),
        (a, b) => a.or(b).unwrap_or(current.version),
    };
    if !current.reads(want) {
        return Err(Error::ParserVersion {
            found: format!("profile {name} version {want}"),
            expected: format!("profile {name} versions {} to {}", current.oldest(), current.version),
        });
    }
    Ok(Profile { name: current.name, version: want })
}

/// Refuses a profile name Bilingua does not know (`Error::UnknownProfile`); `None` is fine.
pub fn check(name: Option<&str>) -> Result<(), Error> {
    match name {
        Some(n) if find(n).is_none() => Err(Error::UnknownProfile(n.to_string())),
        _ => Ok(()),
    }
}

/// Formats whose files Localization OS writes back in their own encoding (UTF-16 included) rather than as UTF-8:
/// ini, strings and dxf; plain text (LocOS `encode_like`: its BOM, UTF-16, or Windows-1252 when the text fits);
/// properties (an ISO-8859-1 file stays ISO-8859-1, `\uXXXX` for the rest). The LocOS PO and subtitle writers keep
/// the file's encoding themselves. XAML (LocOS `xaml_filter`: a UTF-16 file keeps its BOM and UTF-16, a declared
/// legacy charset stays, with a character reference for what it cannot hold) and resjson (`resjson_filter`: the
/// UTF-16 BOM and code units kept) too, and the CAT XML files whose LocOS writer splices the translation into the
/// file's own text and encodes it back in the file's codec (`ttx_filter.build_ttx`, `wordfast.build_txml`,
/// `cat_xliff.build_units` for TXLF): a UTF-16 TTX, TXML or TXLF stays UTF-16 with its BOM. W5: HTML (LocOS
/// `encode_like`: a page in a declared legacy code page goes back in it, its meta charset too, when every character
/// fits, else UTF-8), XSLT (`xslt_filter`: its UTF-16 or legacy codec, a character reference for what it cannot hold)
/// and the HTML Help files (`htmlhelp_filter`: the source's ANSI code page, a localized build the target's, a
/// character neither holds refused).
/// Wave 6 (B-SIDE rc-1,
/// pl-2, csv-1, csv-2, xtg-1): rc (`rc_filter._encode`: UTF-16 kept, an ANSI or declared page kept when the text
/// fits, else UTF-8 with its `#pragma code_page` set to 65001), plist (`apple_filter`: a UTF-16 plist stays UTF-16),
/// csv and tsv (`csv_filter`: the code page when every character fits, else UTF-8 with a BOM; UTF-16 kept) and
/// XPress Tags (`quark_tags_filter`: UTF-16 and the legacy pages kept, else UTF-8 with `<e9>`).
const LOCOS_KEEPS_ENCODING: &[&str] = &[
    "ini", "strings", "dxf", "txt", "properties", "xaml", "resjson", "ttx", "txml", "txlf", "html", "xslt", "hhc", "hhk", "hhp", "rc", "plist", "csv", "tsv", "xtg",
];

impl Profile {
    /// The oldest version of this profile the release can still produce.
    pub fn oldest(&self) -> u32 {
        match self.name {
            LOCOS => LOCOS_OLDEST_VERSION,
            _ => self.version,
        }
    }

    /// True when this release can produce version `version` of the profile (the current one or an older one it
    /// still replays).
    pub fn reads(&self, version: u32) -> bool {
        let current = find(self.name).map_or(self.version, |p| p.version);
        (self.oldest()..=current).contains(&version)
    }

    /// Extraction options with this profile switched on.
    pub fn extract_options(&self) -> ExtractOptions {
        ExtractOptions::new().with_profile(self.name)
    }

    /// The merge options this profile uses for a document of `format` (a filter id). Localization OS: UTF-8 out,
    /// except for the formats it writes in their own encoding (ini, strings, dxf, txt, properties, xaml, resjson, ttx,
    /// txml, txlf, html, xslt, hhc, hhk, hhp, rc, plist, csv, tsv and xtg: `Keep`), and the target language
    /// stamped and plurals filled even when nothing is translated (lead decision 11-04).
    pub fn merge_options(&self, format: &str) -> MergeOptions {
        let enc = if LOCOS_KEEPS_ENCODING.contains(&format) { OutputEncoding::Keep } else { OutputEncoding::Utf8 };
        MergeOptions::new().with_output_encoding(enc).with_stamp_target_always(true).with_profile(self.name)
    }
}

/// What the profile adds to an extracted document: under the LocOS profile, `context.extra[P_INDEX]` on every unit
/// whose format knows LocOS's paragraph index (odf, Qt ts and wxl from their `p_index`, icml from its paragraph
/// index, idml from its `p<n>` unit ids); the inline codes LocOS keeps as unit text turned back into that text (key
/// and value formats, JSON, PO, ARB placeholders, FTL placeables); `context.style` cleared where LocOS has none and
/// `locos.style` set where it has one (dtd; ODF `heading` and `comment`, its paragraph styles cleared). Nothing for
/// other profiles. The `bilingua` facade calls this.
pub fn annotate(doc: &mut Document, profile: Option<&str>) {
    if profile != Some(LOCOS) {
        return;
    }
    let format = doc.format.clone();
    for u in &mut doc.units {
        // BILI-118 (unit model step 5): code types and attrs stay off under the LocOS profile, so its dicts keep
        // today's shape (design section 4, rule 7)
        for c in u.codes.values_mut() {
            c.clear_typing();
        }
        // structure roles (unit model step 4) are not part of what LocOS reads: the format crates leave them unset
        // under the profile, and this keeps any that slipped through out of LocOS's dicts
        u.context.role = None;
        u.context.level = None;
        // plural groups (unit model step 6) likewise: no crate fills them under the profile; any that slipped
        // through stay out of LocOS's dicts
        u.plural = None;
        let p = match format.as_str() {
            "icml" => u.hidden("p").map(str::to_string),
            "idml" => u.id.strip_prefix('p').filter(|n| n.parse::<usize>().is_ok()).map(str::to_string),
            // TXML: the position among every `<segment>` of a translatable or localizable block, protected ones
            // counted (LocOS `wordfast.walk_txml`); TXLF: the cat_xliff slot key. Both are the crate's `key` code.
            "txml" | "txlf" => u.hidden("key").map(str::to_string),
            // TTX: the position among every `<Tu>` (LocOS `ttx_filter.walk`), the crate's context key
            "ttx" => u.context.key.clone().filter(|k| k.parse::<usize>().is_ok()),
            // the formats that record LocOS's own index (odf, Qt ts, wxl)
            _ => u.context.extra.get("p_index").map(str::to_string),
        };
        if let Some(p) = p {
            u.context.extra.insert(P_INDEX.to_string(), p);
        }
        // Track D, X3: the inline codes LocOS keeps as plain text in the unit become that text again
        if let Some(pick) = locos_text_codes(&format) {
            // FTL keeps the codes: its writer finds the placeables in the translation again, as LocOS's does
            flatten_codes(u, pick, format != "ftl");
        }
        // LocOS's style for the unit: the adapter takes `locos.style`, else `context.style`
        if LOCOS_NO_STYLE.contains(&format.as_str()) {
            u.context.style = None;
        }
        // e-mail (`email_filter`): the Subject and the plain-text paragraphs have no style; an HTML part keeps the
        // html filter's own (`a11y:img-alt`, ...)
        if format == "eml" && matches!(u.context.style.as_deref(), Some("Subject" | "text/plain")) {
            u.context.style = None;
        }
        if format == "dtd" {
            if let Some(k) = u.context.key.clone() {
                u.context.extra.insert(STYLE.to_string(), format!("dtd:{k}"));
            }
        }
        // ODF (`odf_filter.parse`): `heading` for a `text:h`, `comment` for a comment paragraph
        // (`office_comments.COMMENT_STYLE`), nothing for every other paragraph (not its automatic or named style)
        if matches!(format.as_str(), "odt" | "ods" | "odp" | "odg") {
            u.context.style = None;
            match u.context.kind.as_deref() {
                Some("heading") => {
                    u.context.extra.insert(STYLE.to_string(), "heading".to_string());
                }
                Some("comment") => {
                    u.context.extra.insert(STYLE.to_string(), "comment".to_string());
                }
                _ => {}
            }
        }
    }
}

/// Formats whose LocOS units carry no style (`""`) where Bilingua sets `context.style`.
/// TTML: LocOS's units have no style (`ttml_filter`), where Bilingua keeps the caption's `style` attribute.
/// Wave 6 (B-SIDE ass-2,
/// tex-2, pl-1, mo-2): LocOS `ass_filter`, `latex_filter`, `apple_filter` (plist, stringsdict) and the PO filter
/// behind `mo_filter` give every unit an empty style.
const LOCOS_NO_STYLE: &[&str] = &[
    "po", "wxl", "xaml", "resx", "strings", "properties", "json", "json5", "resjson", "arb", "yaml", "txml", "txlf", "ttml", "ass", "tex", "plist", "stringsdict", "mo",
];

/// For the formats whose LocOS filter keeps (some) inline codes as plain unit text, which codes: every code in the
/// key/value formats (placeholders such as `%@`, `{0}`, `[ProductName]`, and JSON's HTML), only `placeholder` codes
/// in ARB (LocOS keeps its ICU plural and select arguments as tokens) and FTL (its placeables). YAML: every code
/// (LocOS's yaml filter has no inline map at all).
fn locos_text_codes(format: &str) -> Option<fn(&CodeData) -> bool> {
    match format {
        // yaml: LocOS `yaml_filter` offers the plain value (no inline map): Rails `%{name}`, `{name}`, HTML, `%d`
        // mo (B-SIDE W6 mo-1): LocOS reads an MO through its PO filter, whose placeholders are tokens only for entries
        // flagged c-format / python-format, and an MO has no flags
        "po" | "mo" | "strings" | "properties" | "dtd" | "wxl" | "xaml" | "resx" | "json" | "json5" | "resjson" | "yaml" => Some(|_| true),
        "arb" | "ftl" => Some(|c| c.label.as_deref() == Some("placeholder")),
        _ => None,
    }
}

/// Replaces the tags `pick` selects in every segment's source by the markup they stand for (the code's own
/// open, close or standalone text, else the tag's display), joining the text runs, and drops their codes unless
/// `drop` is false.
fn flatten_codes(u: &mut Unit, pick: fn(&CodeData) -> bool, drop: bool) {
    let mut gone = Vec::new();
    for s in &mut u.segments {
        let mut out: Vec<Inline> = Vec::with_capacity(s.source.len());
        for i in std::mem::take(&mut s.source) {
            let raw = match &i {
                Inline::Tag { id, kind, display, .. } => match u.codes.get(id) {
                    Some(c) if pick(c) => {
                        gone.push(id.clone());
                        let m = match kind {
                            TagKind::Open => c.open.as_deref(),
                            TagKind::Close => c.close.as_deref(),
                            TagKind::Standalone => c.standalone.as_deref(),
                        };
                        Some(m.unwrap_or(display).to_string())
                    }
                    _ => None,
                },
                Inline::Text(_) => None,
            };
            let i = raw.map(Inline::Text).unwrap_or(i);
            match (out.last_mut(), &i) {
                (Some(Inline::Text(a)), Inline::Text(b)) => a.push_str(b),
                _ => out.push(i),
            }
        }
        s.source = out;
    }
    if drop {
        for id in gone {
            u.codes.remove(&id);
        }
    }
}

/// The language tag Localization OS declares for a target locale (its `languages_catalog.html_lang`): underscores
/// become hyphens and the case is canonical (`es_ES` gives `es-ES`, `zh_hant_tw` gives `zh-Hant-TW`); `None` for
/// a blank or malformed code (spaces, quotes, an empty subtag, more than 64 characters).
pub fn lang_tag(code: &str) -> Option<String> {
    let tag = code.trim().replace('_', "-");
    if tag.is_empty() || tag.len() > 64 {
        return None;
    }
    let mut parts = tag.split('-');
    let first = parts.next()?;
    if !(2..=8).contains(&first.len()) || !first.bytes().all(|b| b.is_ascii_alphabetic()) {
        return None;
    }
    let mut out = vec![first.to_ascii_lowercase()];
    let mut ext = false;
    for sub in parts {
        if sub.is_empty() || sub.len() > 8 || !sub.bytes().all(|b| b.is_ascii_alphanumeric()) {
            return None;
        }
        let alpha = sub.bytes().all(|b| b.is_ascii_alphabetic());
        if sub.len() == 1 {
            ext = true;
            out.push(sub.to_ascii_lowercase());
        } else if ext {
            out.push(sub.to_ascii_lowercase());
        } else if sub.len() == 4 && alpha {
            let mut s = sub.to_ascii_lowercase();
            s[..1].make_ascii_uppercase();
            out.push(s);
        } else if (sub.len() == 2 && alpha) || (sub.len() == 3 && sub.bytes().all(|b| b.is_ascii_digit())) {
            out.push(sub.to_ascii_uppercase());
        } else {
            out.push(sub.to_ascii_lowercase());
        }
    }
    Some(out.join("-"))
}

/// True when Localization OS writes `code` right to left (its `text_direction`): a script subtag decides
/// (`ku-Arab` yes, `ug-Latn` no), else the base language (`ar`, `he`, `fa-IR`, `ur`). False for a code
/// [`lang_tag`] refuses.
pub fn is_rtl(code: &str) -> bool {
    const SCRIPTS: [&str; 11] = ["arab", "hebr", "syrc", "thaa", "nkoo", "adlm", "rohg", "samr", "mand", "mend", "yezi"];
    const LANGS: [&str; 22] =
        ["ar", "arc", "azb", "ckb", "dv", "fa", "glk", "he", "iw", "ji", "ks", "lrc", "mzn", "nqo", "pnb", "prs", "ps", "sd", "syr", "ug", "ur", "yi"];
    let Some(tag) = lang_tag(code) else { return false };
    let parts: Vec<&str> = tag.split('-').collect();
    for sub in &parts[1..] {
        if sub.len() == 1 {
            break;
        }
        if sub.len() == 4 && sub.bytes().all(|b| b.is_ascii_alphabetic()) {
            return SCRIPTS.contains(&sub.to_ascii_lowercase().as_str());
        }
    }
    LANGS.contains(&parts[0])
}

/// One translatable segment as the Localization OS adapter numbers it ([`locos_units`]).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct LocosUnit {
    /// LocOS's paragraph index (`TextUnit.p_index`).
    pub p_index: i64,
    /// The Bilingua segment id the index stands for (what [`locos_set_target`] takes).
    pub segment_id: String,
    /// The segment's source as LocOS text, tokens included ([`tokens::to_locos_text_coded`]).
    pub text: String,
    /// The side map of the tokens in `text`, for [`tokens::from_locos_text`].
    pub map: tokens::LocosMap,
    /// LocOS's style for the unit: `locos.style`, else `context.style`, else empty.
    pub style: String,
}

/// The translatable segments of a document extracted under the LocOS profile at paragraph granularity, numbered the
/// way Localization OS numbers its units: `p_index` is `context.extra[P_INDEX]` when every segment's unit has one
/// and they are all distinct, else the position among the translatable segments; the text is the segment's source
/// with its codes as LocOS tokens, numbered by the format's LocOS rule.
pub fn locos_units(doc: &Document) -> Vec<LocosUnit> {
    let segs: Vec<(&Unit, &crate::Segment)> = doc.units.iter().filter(|u| u.translate).flat_map(|u| u.segments.iter().map(move |s| (u, s))).collect();
    let metas: Vec<Option<i64>> = segs.iter().map(|(u, _)| u.context.extra.get(P_INDEX).and_then(|p| p.parse().ok())).collect();
    let distinct = metas.iter().flatten().collect::<std::collections::BTreeSet<_>>().len() == metas.len();
    let use_meta = !segs.is_empty() && metas.iter().all(Option::is_some) && distinct;
    segs.iter()
        .enumerate()
        .map(|(i, (u, s))| {
            let (text, map) = tokens::to_locos_text_coded(&doc.format, &s.source, &u.codes);
            let style = u.context.extra.get(STYLE).filter(|s| !s.is_empty()).map(str::to_string).or_else(|| u.context.style.clone()).unwrap_or_default();
            let p_index = match (use_meta, metas[i]) {
                (true, Some(p)) => p,
                _ => i as i64,
            };
            LocosUnit { p_index, segment_id: s.id.clone(), text, map, style }
        })
        .collect()
}

/// Sets a target the way Localization OS means it: an empty LocOS target is a translation to nothing (written as
/// an empty value, [`Document::set_target_empty`]); one of white space only is written as that white space (LocOS
/// writes it, and the adapter's JSON document carries it, where [`Document::set_target`] would read it as no
/// translation); anything else is [`Document::set_target`], except that a translation may repeat a standalone
/// placeholder tag (`#` in ICU, Chrome `$x$`, go `{{..}}`: LocOS accepts it, where `set_target` refuses it as
/// "tag appears more often"); paired tags keep the exactly-once rule and a tag the source lacks is still refused.
/// For the formats whose LocOS writer repairs a translation's tags ([`tokens::locos_repairs_tags`]: the XLIFF
/// family, TXML, TTX, the XLIFF packages, and since W5 the XML formats and MDX) a target may hold any tag of the
/// source more often than the source, or unbalanced: LocOS's rule (`cat_xliff._render`, the `xml_its` render): a code
/// written more often than the source has it loses the extra tags (its text stays), a close that closes nothing is
/// dropped, a pair left open is closed at the end ([`tokens::repair_locos_text_for`] lists each repair); MDX writes
/// a repeated tag again, as LocOS's `_rematerialize` does. A tag the source lacks is still refused.
/// MicroDVD `.sub` and XPress Tags take the same repairs, applied to the target before it is set.
pub fn locos_set_target(doc: &mut Document, seg_id: &str, target: Vec<Inline>) -> Result<(), Error> {
    if LOCOS_WRITES_REPAIRED.contains(&doc.format.as_str()) {
        let Some((ui, si)) = doc.locate(seg_id) else { return Err(Error::UnknownSegment(seg_id.to_string())) };
        // pairs only: a repeated standalone placeholder is still written each time
        let repaired = tokens::repair_tags(&doc.units[ui].segments[si].source, target, false);
        return set_like_locos(doc, seg_id, repaired);
    }
    if !tokens::locos_repairs_tags(&doc.format) {
        return set_like_locos(doc, seg_id, target);
    }
    let Some((ui, si)) = doc.locate(seg_id) else { return Err(Error::UnknownSegment(seg_id.to_string())) };
    let repaired = tokens::repair_tags(&doc.units[ui].segments[si].source, target.clone(), true);
    set_like_locos(doc, seg_id, repaired)?;
    // the CAT writers apply the rule themselves after stripping the target's edges (as LocOS's do), and MDX writes
    // each tag as typed, so there the target keeps every tag as it came, repeated or unbalanced; the repaired copy
    // only had to pass the checks. The XML writers (W5) get the repaired target.
    if tokens::locos_keeps_typed_tags(&doc.format) && target.iter().any(|i| matches!(i, Inline::Tag { .. })) {
        doc.units[ui].segments[si].target = crate::merge_text(target);
    }
    Ok(())
}

/// Formats whose LocOS writer writes a translation that repeats a style pair, the repeat without its tags (its
/// text stays), where `set_target` refuses it as "tag appears more often" (B-SIDE W6 st-2, xtg-2): MicroDVD
/// `subtitle_text_filter`, XPress Tags `quark_tags_filter`, and InDesign IDML and InCopy ICML (`idml_filter`: a repeated
/// token is dropped, its text stays). The target is repaired by the CAT writers' rule
/// ([`tokens::repair_tags`]: at most as often as the source, a stray close dropped, an open pair closed at the end)
/// and set as repaired.
const LOCOS_WRITES_REPAIRED: &[&str] = &["sub", "xtg", "idml", "icml"];

/// The body of [`locos_set_target`] once the tags are repaired.
fn set_like_locos(doc: &mut Document, seg_id: &str, target: Vec<Inline>) -> Result<(), Error> {
    if target.iter().all(|i| matches!(i, Inline::Text(t) if t.is_empty())) {
        return doc.set_target_empty(seg_id);
    }
    let blank = target.iter().all(|i| matches!(i, Inline::Text(t) if t.trim().is_empty()));
    if !blank {
        return doc.set_target_repeating(seg_id, target);
    }
    // validated as any target (unknown segment, locked unit), then kept as it is
    doc.set_target_repeating(seg_id, target.clone())?;
    let (ui, si) = doc.locate(seg_id).ok_or_else(|| Error::UnknownSegment(seg_id.to_string()))?;
    let seg = &mut doc.units[ui].segments[si];
    let text: String = target.iter().map(|i| if let Inline::Text(t) = i { t.as_str() } else { "" }).collect();
    seg.target = vec![Inline::Text(text)];
    seg.empty_target = false;
    seg.state = crate::SegState::Draft;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lang_tags_like_locos() {
        for (code, want) in [("es_ES", Some("es-ES")), ("EN-us", Some("en-US")), ("zh_hant_tw", Some("zh-Hant-TW")), ("es-419", Some("es-419")), ("x", None), ("", None), ("de DE", None), ("de--DE", None), ("\"><b>", None)] {
            assert_eq!(lang_tag(code).as_deref(), want, "{code}");
        }
        assert!(is_rtl("ar-SA") && is_rtl("ku-Arab") && is_rtl("he") && !is_rtl("ug-Latn") && !is_rtl("de") && !is_rtl("<x>"));
    }

    #[test]
    fn resolve_names_and_versions() {
        assert_eq!(resolve("locos", None).unwrap(), locos());
        assert_eq!(resolve("locos@1", None).unwrap(), locos());
        assert_eq!(resolve("locos", Some(LOCOS_VERSION)).unwrap(), locos());
        assert_eq!(resolve("locos@1", Some(1)).unwrap().version, 1);
        assert!(matches!(resolve("locos@1", Some(2)), Err(Error::UnknownProfile(_))));
        assert!(matches!(resolve("locos@x", None), Err(Error::UnknownProfile(_))));
        assert!(matches!(resolve("nope", None), Err(Error::UnknownProfile(_))));
        assert!(matches!(resolve("locos", Some(LOCOS_VERSION + 1)), Err(Error::ParserVersion { .. })));
        assert!(matches!(resolve("locos@0", None), Err(Error::ParserVersion { .. })));
        assert!(locos().reads(1) && !locos().reads(0) && !locos().reads(LOCOS_VERSION + 1));
    }
}