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/// Whether `language` names Japanese: `ja`, `jp`, `japanese` or `日本語`,
9/// trimmed and case-insensitive.
10pub fn is_japanese(language: &str) -> bool {
11 let l = language.trim();
12 l.eq_ignore_ascii_case("ja")
13 || l.eq_ignore_ascii_case("jp")
14 || l.eq_ignore_ascii_case("japanese")
15 || l.eq_ignore_ascii_case("日本語")
16}
17
18/// `[graph] language` of the repository at `repo`, read best-effort for
19/// callers with no run (and so no config of their own). An unreadable config
20/// is English: a hold reason must still get written.
21pub fn of_repo(repo: &std::path::Path) -> String {
22 crate::config::Config::discover(repo, None)
23 .map(|(c, _)| c.graph.language)
24 .unwrap_or_else(|_| "en".to_owned())
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn every_japanese_spelling_is_accepted() {
33 for l in ["ja", "JA", "jp", "Japanese", " japanese ", "日本語"] {
34 assert!(is_japanese(l), "{l:?}");
35 }
36 }
37
38 #[test]
39 fn everything_else_is_not_japanese() {
40 for l in ["en", "", "de", "fr", "jap"] {
41 assert!(!is_japanese(l), "{l:?}");
42 }
43 }
44}