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/// Whether two language codes name the same language.
82pub fn same_language(a: &str, b: &str) -> bool {
83    canonical(a) == canonical(b)
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn spellings_of_one_language_are_the_same() {
92        assert!(same_language("en", "eng"));
93        assert!(same_language("ger", "deu"));
94        assert!(same_language("DE", "ger"));
95        assert!(same_language("chi", "zho"));
96    }
97
98    #[test]
99    fn different_languages_differ() {
100        assert!(!same_language("eng", "spa"));
101        assert!(!same_language("nob", "nor"));
102    }
103
104    #[test]
105    fn a_code_in_iso_639_2_is_three_letters() {
106        assert_eq!(iso639_2("en"), "eng");
107        assert_eq!(iso639_2("DE"), "ger");
108        assert_eq!(iso639_2("deu"), "ger");
109        assert_eq!(iso639_2("spa"), "spa");
110        assert_eq!(iso639_2("tlh"), "tlh");
111    }
112
113    #[test]
114    fn an_unknown_code_equals_only_itself() {
115        assert!(same_language("tlh", "TLH"));
116        assert!(!same_language("tlh", "eng"));
117    }
118}