Skip to main content

magi/
lang.rs

1//! One reading of `[graph] language`, shared by everything that has a
2//! translated wording table.
3//!
4//! Only English and Japanese are translated. The setting has always accepted
5//! a code or a name, so the Japanese spellings are matched loosely; anything
6//! else falls back to English rather than shipping a guess.
7
8/// Is this the default, where nothing needs saying? Empty, `en` or `english`.
9pub fn is_english(language: &str) -> bool {
10    let l = language.trim();
11    l.is_empty() || l.eq_ignore_ascii_case("en") || l.eq_ignore_ascii_case("english")
12}
13
14/// Whether `language` names Japanese: `ja`, `jp`, `japanese` or `日本語`,
15/// trimmed and case-insensitive.
16pub fn is_japanese(language: &str) -> bool {
17    let l = language.trim();
18    l.eq_ignore_ascii_case("ja")
19        || l.eq_ignore_ascii_case("jp")
20        || l.eq_ignore_ascii_case("japanese")
21        || l.eq_ignore_ascii_case("日本語")
22}
23
24/// `[graph] language` of the repository at `repo`, read best-effort for
25/// callers with no run (and so no config of their own). An unreadable config
26/// is English: a hold reason must still get written.
27pub fn of_repo(repo: &std::path::Path) -> String {
28    crate::config::Config::discover(repo, None)
29        .map(|(c, _)| c.graph.language)
30        .unwrap_or_else(|_| "en".to_owned())
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn every_japanese_spelling_is_accepted() {
39        for l in ["ja", "JA", "jp", "Japanese", " japanese ", "日本語"] {
40            assert!(is_japanese(l), "{l:?}");
41        }
42    }
43
44    #[test]
45    fn everything_else_is_not_japanese() {
46        for l in ["en", "", "de", "fr", "jap"] {
47            assert!(!is_japanese(l), "{l:?}");
48        }
49    }
50}