mf2-locale-data 2.0.0

CLDR locale data for Rust MF2 catalogs (build side): all-locale plural, direction, number, currency and unit tables and the LOCALE entries built from them.
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Number data (`plans/05-tooling.md` §7, `plans/02-catalog-format.md`
//! §4.2–§4.4): the shipped all-locale table (`data/numbers.txt`), CLDR's
//! locale resolution for it, and the `number.symbols` / `number.patterns`
//! LOCALE entries of a catalog, sliced to what its corpus needs.
//!
//! A catalog has **one** numbering system — its locale's default, or the
//! one its tag names with `-u-nu-` — because MF2 (at the pinned spec) has no
//! `numberingSystem` option. Its digits travel inside `number.symbols`.

pub mod pattern;
pub mod table;
pub mod tag;

use std::collections::{BTreeMap, BTreeSet};
use std::sync::OnceLock;

use mf2_catalog::format::locale_key;
use mf2_catalog::number::Style;
use mf2_catalog::writer::number::{SymbolsSpec, patterns, symbols};
use mf2_model::{Declaration, Expression, FunctionRef, Message, OptionValue, Pattern, PatternPart};

pub use self::table::{Record, Table};
pub use self::tag::Tag;

use crate::error::Error;

/// The shipped table: `data/numbers.txt`, generated by `cargo xtask
/// locale-data` from the `cargo xtask cldr-sync` cache.
const TABLE: &str = include_str!("../data/numbers.txt");

fn table() -> Result<&'static Table<'static>, Error> {
    static PARSED: OnceLock<Result<Table<'static>, String>> = OnceLock::new();
    match PARSED.get_or_init(|| Table::parse(TABLE).map_err(|e| e.to_string())) {
        Ok(t) => Ok(t),
        Err(message) => Err(Error::Table {
            line: 0,
            message: message.clone(),
        }),
    }
}

/// A configured set of currency codes, unit identifiers or calendars
/// (`mf2.toml` `[locale_data] currencies` / `units` / `calendars`,
/// `plans/02-catalog-format.md` §4.4).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Selection {
    /// These (the corpus's literal values by default, plus any listed in
    /// `mf2.toml`).
    Listed(BTreeSet<String>),
    /// Every one CLDR has (`"all"`, or a non-literal value in the corpus).
    All,
}

impl Default for Selection {
    fn default() -> Selection {
        Selection::Listed(BTreeSet::new())
    }
}

impl Selection {
    /// Adds `v` (nothing to add to `All`).
    pub fn add(&mut self, v: &str) {
        if let Selection::Listed(set) = self {
            set.insert(v.to_owned());
        }
    }
}

/// What a corpus needs of its locale's number data: the slicing rule of
/// `plans/02-catalog-format.md` §4.4, decided by the build from the functions
/// and literal options the corpus uses ([`NumberNeeds::add_message`]). Build
/// one with `default()` and set fields.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct NumberNeeds {
    /// Numbers are formatted with the locale's symbols: `fn-number` is on
    /// and the corpus formats a numeric function or has a placeholder that
    /// can receive a number → `number.symbols`.
    pub symbols: bool,
    /// `:percent` is used → `number.symbols` and the percent pattern.
    pub percent: bool,
    /// `:currency` is used → `number.symbols`, its patterns and
    /// `currency.data`.
    pub currency: Option<CurrencyNeeds>,
    /// `:unit` is used → `number.symbols` and `unit.data`.
    pub unit: Option<UnitNeeds>,
}

/// What a corpus's `:currency` may need.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
// Four independent facts about the corpus's option values, each selecting
// its own part of the entries: flags are the plain reading.
#[allow(clippy::struct_excessive_bools)]
pub struct CurrencyNeeds {
    /// `currencySign=accounting` may occur (a literal `accounting`, or a
    /// variable value) → the accounting patterns.
    pub accounting: bool,
    /// `currencyDisplay=never` may occur (literal or variable) → the
    /// no-symbol patterns.
    pub hidden: bool,
    /// `currencyDisplay=narrowSymbol` may occur → the narrow symbols.
    pub narrow: bool,
    /// `currencyDisplay=name` may occur → the display names and name
    /// patterns (and `plural.cardinal`).
    pub names: bool,
    /// The currencies (`currency=` codes).
    pub codes: Selection,
}

impl CurrencyNeeds {
    /// Everything, for every currency (the options unknown).
    pub const ALL: CurrencyNeeds = CurrencyNeeds {
        accounting: true,
        hidden: true,
        narrow: true,
        names: true,
        codes: Selection::All,
    };
}

/// What a corpus's `:unit` may need.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnitNeeds {
    /// The units (`unit=` identifiers; an `X-per-Y` CLDR lacks brings `X`
    /// and `Y`).
    pub ids: Selection,
    /// The widths, indexed by `mf2_catalog::unit::Width` (long, short,
    /// narrow): the literal `unitDisplay` values, `short` (the default) when
    /// an expression has none, all three for a variable value.
    pub widths: [bool; 3],
    /// Carry the display names (no MF2 formatting path reads them).
    pub names: bool,
}

impl Default for UnitNeeds {
    /// No unit yet; the default width.
    fn default() -> UnitNeeds {
        UnitNeeds {
            ids: Selection::default(),
            widths: [false, true, false],
            names: false,
        }
    }
}

impl UnitNeeds {
    /// Every unit, every width, without display names.
    pub const ALL: UnitNeeds = UnitNeeds {
        ids: Selection::All,
        widths: [true; 3],
        names: false,
    };
}

/// An option of an expression, as far as the build can know it.
enum Opt<'m> {
    /// Not given.
    Absent,
    /// A literal value.
    Literal(&'m str),
    /// A variable: any value.
    Variable,
}

fn option<'m>(f: &'m FunctionRef<'_>, name: &str) -> Opt<'m> {
    match f.options.get(name) {
        None => Opt::Absent,
        Some(OptionValue::Literal(l)) => Opt::Literal(l.value.as_ref()),
        // A variable, or a kind of value this version does not know: any.
        Some(_) => Opt::Variable,
    }
}

impl NumberNeeds {
    /// The conservative needs of a corpus known only by its function set
    /// (`ns:name` identifiers, as FUNCS lists them): every numeric function
    /// formats with symbols, `:percent` needs its pattern, `:currency` and
    /// `:unit` everything (their option values being unknown).
    pub fn from_functions<'a>(functions: impl IntoIterator<Item = &'a str>) -> NumberNeeds {
        let mut n = NumberNeeds::default();
        for f in functions {
            match f {
                "number" | "integer" | "offset" => n.symbols = true,
                "percent" => n.percent = true,
                "currency" => n.currency = Some(CurrencyNeeds::ALL),
                "unit" => n.unit = Some(UnitNeeds::ALL),
                _ => {}
            }
        }
        n
    }

    /// Adds what `message` needs (§4.4): its numeric functions, the literal
    /// `currency=` codes and `unit=` identifiers, the `currencySign`,
    /// `currencyDisplay` and `unitDisplay` values (a variable value counts
    /// as every value), and `symbols` for a placeholder whose variable has
    /// no function (it can receive a number) — call it only with `fn-number`
    /// on. Codes and units that only arguments carry at run time are not in
    /// the corpus: configure them (`mf2.toml`).
    pub fn add_message(&mut self, message: &Message<'_>) {
        for d in message.declarations() {
            match d {
                Declaration::Input(i) => self.add_function(i.value.function.as_ref()),
                Declaration::Local(l) => {
                    if l.value.function().is_none() && matches!(l.value, Expression::Variable(_)) {
                        self.symbols = true;
                    }
                    self.add_function(l.value.function());
                }
                _ => {}
            }
        }
        let patterns: Vec<&Pattern<'_>> = match message {
            Message::Pattern(p) => vec![&p.pattern],
            Message::Select(s) => s.variants.iter().map(|v| &v.value).collect(),
            _ => Vec::new(),
        };
        for p in patterns {
            for part in p {
                if let PatternPart::Expression(e) = part {
                    if e.function().is_none() && matches!(e, Expression::Variable(_)) {
                        self.symbols = true;
                    }
                    self.add_function(e.function());
                }
            }
        }
    }

    fn add_function(&mut self, f: Option<&FunctionRef<'_>>) {
        let Some(f) = f else {
            return;
        };
        match f.name.as_ref() {
            "number" | "integer" | "offset" => self.symbols = true,
            "percent" => self.percent = true,
            "currency" => {
                let c = self.currency.get_or_insert_with(CurrencyNeeds::default);
                match option(f, "currency") {
                    // A code that is not well-formed is the runtime's Bad
                    // Option, not the build's error: nothing to slice in.
                    Opt::Literal(code)
                        if code.len() == 3 && code.bytes().all(|b| b.is_ascii_alphabetic()) =>
                    {
                        c.codes.add(&code.to_ascii_uppercase());
                    }
                    Opt::Variable => c.codes = Selection::All,
                    Opt::Literal(_) | Opt::Absent => {}
                }
                if matches!(
                    option(f, "currencySign"),
                    Opt::Literal("accounting") | Opt::Variable
                ) {
                    c.accounting = true;
                }
                match option(f, "currencyDisplay") {
                    Opt::Literal("never") => c.hidden = true,
                    Opt::Literal("name") => c.names = true,
                    Opt::Literal("narrowSymbol") => c.narrow = true,
                    Opt::Variable => {
                        c.hidden = true;
                        c.names = true;
                        c.narrow = true;
                    }
                    Opt::Literal(_) | Opt::Absent => {}
                }
            }
            "unit" => {
                let u = self.unit.get_or_insert_with(|| UnitNeeds {
                    widths: [false; 3],
                    ..UnitNeeds::default()
                });
                match option(f, "unit") {
                    Opt::Literal(id) => u.ids.add(id),
                    Opt::Variable => u.ids = Selection::All,
                    Opt::Absent => {}
                }
                match option(f, "unitDisplay") {
                    Opt::Literal("long") => u.widths[0] = true,
                    Opt::Literal("narrow") => u.widths[2] = true,
                    Opt::Variable => u.widths = [true; 3],
                    // `short`, the default, and an invalid value (which
                    // formats with the default after *Bad Option*).
                    Opt::Literal(_) | Opt::Absent => u.widths[1] = true,
                }
            }
            _ => {}
        }
    }

    /// Whether any number entry is needed.
    pub const fn any(&self) -> bool {
        self.symbols || self.percent || self.currency.is_some() || self.unit.is_some()
    }

    /// Whether the entries need `plural.cardinal`: a unit pattern, or a
    /// currency's display name, is chosen by the formatted number's plural
    /// category.
    pub fn needs_cardinal(&self) -> bool {
        self.unit.is_some() || self.currency.as_ref().is_some_and(|c| c.names)
    }

    /// The `number.patterns` styles these needs select.
    pub fn styles(&self) -> Vec<Style> {
        let mut out = Vec::new();
        if self.percent {
            out.push(Style::Percent);
        }
        if let Some(c) = &self.currency {
            out.extend([Style::Currency, Style::CurrencyAlpha]);
            if c.hidden {
                out.push(Style::CurrencyNoSymbol);
            }
            if c.accounting {
                out.extend([Style::Accounting, Style::AccountingAlpha]);
                if c.hidden {
                    out.push(Style::AccountingNoSymbol);
                }
            }
        }
        out
    }
}

/// The table field of a pattern style.
pub const fn style_field(style: Style) -> &'static str {
    match style {
        Style::Percent => "percent-pattern",
        Style::Currency => "currency",
        Style::CurrencyAlpha => "currency-alpha",
        Style::CurrencyNoSymbol => "currency-none",
        Style::Accounting => "accounting",
        Style::AccountingAlpha => "accounting-alpha",
        Style::AccountingNoSymbol => "accounting-none",
    }
}

/// A locale's number data as CLDR resolves it for one tag.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NumberData {
    /// The CLDR locale the data comes from (after truncation and parents;
    /// `und` is root).
    pub locale: &'static str,
    /// The catalog's numbering system: `-u-nu-` when it names a numeric
    /// system (`native` → the locale's native one), else the locale's
    /// default.
    pub numbering_system: &'static str,
    /// The system whose symbols and patterns apply: the numbering system when
    /// the locale has data for it, else `latn` (`en-u-nu-arab`: Latin symbols,
    /// Arabic-Indic digits — CLDR's root has no other system).
    pub symbols_system: &'static str,
    /// The ten digits; `None` for `latn`.
    pub digits: Option<&'static str>,
    /// CLDR's `minimumGroupingDigits`.
    pub minimum_grouping_digits: u8,
    pub decimal: &'static str,
    pub group: &'static str,
    pub minus: &'static str,
    pub plus: &'static str,
    pub percent: &'static str,
    /// The decimal pattern (for its grouping).
    pub decimal_pattern: &'static str,
    /// The percent and currency patterns CLDR has, by style.
    pub patterns: BTreeMap<Style, &'static str>,
    /// The `currencyDisplay=name` patterns (`{0}` the number, `{1}` the
    /// name) by plural category code (0 zero … 5 other), of the symbols'
    /// system, else `latn`'s (`ckb`'s `arab` has none).
    pub currency_name_patterns: Vec<(u8, &'static str)>,
    /// The CLDR locale chain the data resolves through: `locale`, its
    /// ancestors, root.
    pub chain: Vec<&'static str>,
}

/// Resolves `tag` to a CLDR locale of the table: a region that implies a
/// script other than the language's gains it first (`zh-TW` → `zh-Hant-TW`,
/// when a `zh-Hant` locale exists), then the tag, its parents and
/// truncations, root.
fn resolve_locale(t: &'static Table<'static>, tag: &Tag) -> &'static str {
    let mut tag = tag.clone();
    if tag.script.is_none()
        && let Some(region) = &tag.region
        && let Some(script) = t.likely.get(format!("{}-{region}", tag.language).as_str())
    {
        tag.script = Some((*script).to_owned());
    }
    t.lookup(&tag.base())
}

fn field(
    t: &'static Table<'static>,
    locale: &'static str,
    system: &'static str,
    name: &'static str,
) -> Result<&'static str, Error> {
    t.system_field(locale, system, name)
        .ok_or_else(|| Error::Missing {
            locale: locale.to_owned(),
            system: system.to_owned(),
            field: name,
        })
}

/// The number data for `tag` (BCP 47; `_` is read as `-`, case ignored).
pub fn number_data(tag: &str) -> Result<NumberData, Error> {
    let t = table()?;
    let parsed = Tag::parse(tag);
    let locale = resolve_locale(t, &parsed);
    let default = t.locale_field(locale, "default").unwrap_or("latn");
    let nu: &'static str = match parsed.nu.as_deref() {
        Some("native") => t.locale_field(locale, "native").unwrap_or(default),
        Some(n) => t
            .digits
            .get_key_value(n)
            .map(|(k, _)| *k)
            .or((n == "latn").then_some("latn"))
            .unwrap_or(default),
        None => default,
    };
    let system = if t.has_system(locale, nu) { nu } else { "latn" };
    let digits = if nu == "latn" {
        None
    } else {
        Some(
            t.digits
                .get(nu)
                .map(AsRef::as_ref)
                .ok_or_else(|| Error::Missing {
                    locale: locale.to_owned(),
                    system: nu.to_owned(),
                    field: "digits",
                })?,
        )
    };
    let min = t.locale_field(locale, "min-grouping").unwrap_or("1");
    let minimum_grouping_digits = min.parse().map_err(|_| Error::Table {
        line: 0,
        message: format!("{locale}: min-grouping {min:?}"),
    })?;
    let mut pats = BTreeMap::new();
    for style in Style::ALL {
        if let Some(p) = t.system_field(locale, system, style_field(style)) {
            pats.insert(style, p);
        }
    }
    let names_system = if t
        .system_field(locale, system, "currency-name-other")
        .is_some()
    {
        system
    } else {
        "latn"
    };
    let currency_name_patterns = crate::unit::CATEGORIES
        .iter()
        .zip(0u8..)
        .filter_map(|(c, k)| {
            Some((
                k,
                t.system_field(locale, names_system, &format!("currency-name-{c}"))?,
            ))
        })
        .collect();
    Ok(NumberData {
        currency_name_patterns,
        chain: t.chain(locale),
        locale,
        numbering_system: nu,
        symbols_system: system,
        digits,
        minimum_grouping_digits,
        decimal: field(t, locale, system, "decimal")?,
        group: field(t, locale, system, "group")?,
        minus: field(t, locale, system, "minus")?,
        plus: field(t, locale, system, "plus")?,
        percent: field(t, locale, system, "percent")?,
        decimal_pattern: field(t, locale, system, "decimal-pattern")?,
        patterns: pats,
    })
}

impl NumberData {
    /// The `number.symbols` payload (§4.2).
    pub fn symbols_entry(&self) -> Result<Vec<u8>, Error> {
        let grouping = pattern::parse(self.decimal_pattern)?.grouping;
        Ok(symbols(&SymbolsSpec {
            grouping,
            minimum_grouping_digits: self.minimum_grouping_digits,
            decimal: self.decimal,
            group: self.group,
            minus: self.minus,
            plus: self.plus,
            percent: self.percent,
            digits: self.digits,
        })?)
    }

    /// The `number.patterns` payload (§4.3) for `styles` (a style CLDR lacks
    /// for this locale — only ever an `…Alpha` variant — is left out; the
    /// reader falls back to its base).
    pub fn patterns_entry(&self, styles: &[Style]) -> Result<Vec<u8>, Error> {
        let mut parsed = Vec::new();
        for style in styles {
            if let Some(p) = self.patterns.get(style) {
                parsed.push((*style, pattern::parse(p)?));
            }
        }
        let specs: Vec<_> = parsed.iter().map(|(s, p)| (*s, p.spec())).collect();
        Ok(patterns(&specs)?)
    }
}

/// The number LOCALE entries for `tag` and `needs`, as
/// `mf2_catalog::writer::Options::locale_entries` takes them: `number.symbols`
/// when any number is formatted, `number.patterns` when a style is needed,
/// `currency.data` for `:currency`, `unit.data` for `:unit`; nothing when
/// `needs` needs nothing. (`plural.cardinal`, which units and currency names
/// need too — [`NumberNeeds::needs_cardinal`] — is `crate::locale_entries`'.)
pub fn number_locale_entries(tag: &str, needs: &NumberNeeds) -> Result<Vec<(u32, Vec<u8>)>, Error> {
    if !needs.any() {
        return Ok(Vec::new());
    }
    let data = number_data(tag)?;
    let mut out = vec![(locale_key::NUMBER_SYMBOLS, data.symbols_entry()?)];
    let styles = needs.styles();
    if !styles.is_empty() {
        out.push((locale_key::NUMBER_PATTERNS, data.patterns_entry(&styles)?));
    }
    if let Some(c) = &needs.currency {
        let list = crate::currency::currency_data(&data.chain, &c.codes)?;
        out.push((
            locale_key::CURRENCY_DATA,
            crate::currency::currency_entry(&data, &list, c)?,
        ));
    }
    if let Some(u) = &needs.unit {
        let (per, list) = crate::unit::unit_data(&data.chain, &u.ids)?;
        out.push((
            locale_key::UNIT_DATA,
            crate::unit::unit_entry(&per, &list, u)?,
        ));
    }
    Ok(out)
}

/// Every CLDR locale in the number table, bytewise ascending (`und` is root).
pub fn number_locales() -> Result<Vec<&'static str>, Error> {
    Ok(table()?.locales.keys().copied().collect())
}