Skip to main content

spoolframe_types/
language.rs

1//! Language codes as files write them: ISO 639-2 in streams (`eng`, and
2//! both `ger` and `deu`), often ISO 639-1 in subtitle file names (`en`).
3//! [`same_language`] treats all spellings of one language as equal.
4
5/// Two- and three-letter codes of one language: 639-1, 639-2/B, 639-2/T.
6/// The languages files commonly carry; a code not here only equals itself.
7const ALIASES: &[&[&str]] = &[
8    &["ar", "ara"],
9    &["bg", "bul"],
10    &["ca", "cat"],
11    &["cs", "cze", "ces"],
12    &["da", "dan"],
13    &["de", "ger", "deu"],
14    &["el", "gre", "ell"],
15    &["en", "eng"],
16    &["es", "spa"],
17    &["et", "est"],
18    &["fa", "per", "fas"],
19    &["fi", "fin"],
20    &["fr", "fre", "fra"],
21    &["he", "heb"],
22    &["hi", "hin"],
23    &["hr", "hrv"],
24    &["hu", "hun"],
25    &["id", "ind"],
26    &["is", "ice", "isl"],
27    &["it", "ita"],
28    &["ja", "jpn"],
29    &["ko", "kor"],
30    &["lt", "lit"],
31    &["lv", "lav"],
32    &["ms", "may", "msa"],
33    &["nb", "nob"],
34    &["nl", "dut", "nld"],
35    &["no", "nor"],
36    &["pl", "pol"],
37    &["pt", "por"],
38    &["ro", "rum", "ron"],
39    &["ru", "rus"],
40    &["sk", "slo", "slk"],
41    &["sl", "slv"],
42    &["sr", "srp"],
43    &["sv", "swe"],
44    &["th", "tha"],
45    &["tr", "tur"],
46    &["uk", "ukr"],
47    &["vi", "vie"],
48    &["zh", "chi", "zho"],
49];
50
51/// One spelling for every code of a language: its first in [`ALIASES`],
52/// or the code itself, lowercased.
53fn canonical(code: &str) -> String {
54    let code = code.to_ascii_lowercase();
55    ALIASES
56        .iter()
57        .find(|codes| codes.contains(&code.as_str()))
58        .map_or(code, |codes| codes[0].to_owned())
59}
60
61/// A language code as BCP 47 (HLS's `LANGUAGE`) writes it: the two-letter
62/// code where the language has one (`eng` and `en` are `en`), else the
63/// code as it is, lowercased.
64pub fn bcp47(code: &str) -> String {
65    canonical(code)
66}
67
68/// A language code as ISO 639-2 writes it, three letters, the
69/// bibliographic code where there are two (`en` and `eng` are `eng`,
70/// `de` is `ger`): what MP4's language field holds, and ffmpeg's MP4
71/// muxer writes `und` for any other. A code not known stays as it is,
72/// lowercased.
73pub fn iso639_2(code: &str) -> String {
74    let code = code.to_ascii_lowercase();
75    ALIASES
76        .iter()
77        .find(|codes| codes.contains(&code.as_str()))
78        .map_or(code, |codes| codes[1].to_owned())
79}
80
81/// The language a device's tag names, as [`bcp47`] writes it: `en-US`, and
82/// `en_US` as a POSIX locale has it, are `en`. `None` for a tag that names
83/// none (`und`, `mul`, `mis`, `zxx`), a private one (`x-foo`) or anything
84/// else that is no language: a track tagged `und` must not match it.
85pub fn of_tag(tag: &str) -> Option<String> {
86    let primary = tag.split(['-', '_']).next()?;
87    let language = (2..=3).contains(&primary.len())
88        && primary.bytes().all(|b| b.is_ascii_alphabetic())
89        && !["und", "mul", "mis", "zxx"]
90            .iter()
91            .any(|none| primary.eq_ignore_ascii_case(none));
92    language.then(|| bcp47(primary))
93}
94
95/// Whether two language codes name the same language.
96pub fn same_language(a: &str, b: &str) -> bool {
97    canonical(a) == canonical(b)
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn a_device_s_tag_is_its_language() {
106        for (tag, language) in [
107            ("en-US", "en"),
108            ("EN_us", "en"),
109            ("it", "it"),
110            ("zh-Hant-TW", "zh"),
111            ("deu-DE", "de"),
112            ("sco", "sco"),
113        ] {
114            assert_eq!(of_tag(tag).as_deref(), Some(language), "{tag}");
115        }
116    }
117
118    #[test]
119    fn a_tag_that_names_no_language_is_none() {
120        for tag in ["", "und", "UND-US", "mul", "x-private", "C", "POSIX", "e1"] {
121            assert_eq!(of_tag(tag), None, "{tag}");
122        }
123    }
124
125    #[test]
126    fn spellings_of_one_language_are_the_same() {
127        assert!(same_language("en", "eng"));
128        assert!(same_language("ger", "deu"));
129        assert!(same_language("DE", "ger"));
130        assert!(same_language("chi", "zho"));
131    }
132
133    #[test]
134    fn different_languages_differ() {
135        assert!(!same_language("eng", "spa"));
136        assert!(!same_language("nob", "nor"));
137    }
138
139    #[test]
140    fn a_code_in_iso_639_2_is_three_letters() {
141        assert_eq!(iso639_2("en"), "eng");
142        assert_eq!(iso639_2("DE"), "ger");
143        assert_eq!(iso639_2("deu"), "ger");
144        assert_eq!(iso639_2("spa"), "spa");
145        assert_eq!(iso639_2("tlh"), "tlh");
146    }
147
148    #[test]
149    fn an_unknown_code_equals_only_itself() {
150        assert!(same_language("tlh", "TLH"));
151        assert!(!same_language("tlh", "eng"));
152    }
153}