bilingua_model/plural.rs
1//! Plural groups (unit model step 6, BILI-119a): which forms of one message belong together. A unit that is one
2//! form of a plural, selectordinal or select message carries a [`PluralSlot`] ([`crate::Unit::plural`]); every
3//! form of one message shares its `group`. The target categories are not stored: [`crate::Document::plural_categories`]
4//! reads them from the target language.
5//!
6//! The slot is for consumers (editors, QA). No writer reads it: they keep reading `extra["plural"]`,
7//! `extra["icu"]` and their skeletons. Under the LocOS profile no slot is filled.
8
9use serde::{Deserialize, Serialize};
10
11/// The kind of a plural group. Serialises lowercase; a kind this release does not know reads as `other`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[serde(rename_all = "lowercase")]
14#[non_exhaustive]
15pub enum PluralKind {
16 /// Cardinal plural (`plural`, gettext `msgstr[N]`, xcstrings `variations.plural`).
17 Cardinal,
18 /// Ordinal plural (ICU `selectordinal`).
19 Ordinal,
20 /// ICU `select` (a key such as `female`, not a number).
21 Select,
22 /// A kind written by a newer release.
23 #[serde(other)]
24 Other,
25}
26
27/// One form of a plural group. Build it with [`PluralSlot::new`] and the `with_*` methods. Optional fields are
28/// left out of the JSON when unset, `added` when false.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30#[non_exhaustive]
31pub struct PluralSlot {
32 /// Shared by every form of one message (one plural or select argument), unique in the document.
33 pub group: Box<str>,
34 /// Cardinal, ordinal or select.
35 pub kind: PluralKind,
36 /// The CLDR category (`one`, `few`), an exact value (`=0`) or a select key (`female`). gettext has none: its
37 /// order is not CLDR order (see `index`).
38 #[serde(default, skip_serializing_if = "Option::is_none")]
39 pub category: Option<Box<str>>,
40 /// The gettext `msgstr[N]` index.
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub index: Option<u8>,
43 /// The ICU argument name (`count`), when known.
44 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub arg: Option<Box<str>>,
46 /// The enclosing group, for a plural inside a select (or any nesting).
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub parent: Option<Box<str>>,
49 /// A target category the source does not have, added at extract so it can be translated (and a unit inside
50 /// such a form). Untranslated, it writes what the source `other` form gives.
51 #[serde(default, skip_serializing_if = "is_false")]
52 pub added: bool,
53}
54
55fn is_false(b: &bool) -> bool {
56 !*b
57}
58
59impl PluralSlot {
60 /// A form of `group` of this kind, with no category, index, argument or parent.
61 pub fn new(group: impl Into<Box<str>>, kind: PluralKind) -> PluralSlot {
62 PluralSlot { group: group.into(), kind, category: None, index: None, arg: None, parent: None, added: false }
63 }
64 /// Sets the category.
65 pub fn with_category(mut self, category: impl Into<Box<str>>) -> PluralSlot {
66 self.category = Some(category.into());
67 self
68 }
69 /// Sets the gettext index.
70 pub fn with_index(mut self, index: u8) -> PluralSlot {
71 self.index = Some(index);
72 self
73 }
74 /// Sets the argument name.
75 pub fn with_arg(mut self, arg: impl Into<Box<str>>) -> PluralSlot {
76 self.arg = Some(arg.into());
77 self
78 }
79 /// Sets the enclosing group.
80 pub fn with_parent(mut self, parent: impl Into<Box<str>>) -> PluralSlot {
81 self.parent = Some(parent.into());
82 self
83 }
84 /// Marks the form as added for the target language (or not).
85 pub fn with_added(mut self, added: bool) -> PluralSlot {
86 self.added = added;
87 self
88 }
89 /// The group.
90 pub fn group(&self) -> &str {
91 &self.group
92 }
93 /// The kind.
94 pub fn kind(&self) -> PluralKind {
95 self.kind
96 }
97 /// The category.
98 pub fn category(&self) -> Option<&str> {
99 self.category.as_deref()
100 }
101 /// The gettext index.
102 pub fn index(&self) -> Option<u8> {
103 self.index
104 }
105 /// The argument name.
106 pub fn arg(&self) -> Option<&str> {
107 self.arg.as_deref()
108 }
109 /// The enclosing group.
110 pub fn parent(&self) -> Option<&str> {
111 self.parent.as_deref()
112 }
113 /// True for a form the target needs and the source does not have.
114 pub fn is_added(&self) -> bool {
115 self.added
116 }
117}
118
119/// The categories of `kind` for a target language tag, in CLDR order with `other`; `None` for a select, an
120/// unknown kind, no language or one the table does not know. Babel's aliases (`deu`, `iw`, `und`) resolve.
121pub(crate) fn categories(target: Option<&str>, kind: PluralKind) -> Option<Vec<&'static str>> {
122 let ordinal = match kind {
123 PluralKind::Cardinal => false,
124 PluralKind::Ordinal => true,
125 _ => return None,
126 };
127 let lang = crate::cldr::language(target?);
128 crate::cldr::bits_babel(&lang, ordinal).map(crate::cldr::names)
129}