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/// The language an agent is told to write in, by name.
25///
26/// `[graph] language` takes a code or a name, and a code reached the prompt
27/// verbatim: "Write all prose in ja" is an instruction a model can read as
28/// noise, and the questions agents asked came back in English on a repository
29/// configured for Japanese. Naming the language is the whole fix. The same
30/// name is what a CLI's own response-language setting expects.
31pub fn name(language: &str) -> &str {
32 if is_japanese(language) {
33 return "Japanese";
34 }
35 if is_english(language) {
36 return "English";
37 }
38 match language.trim() {
39 "de" => "German",
40 "fr" => "French",
41 "es" => "Spanish",
42 "ko" => "Korean",
43 "zh" => "Chinese",
44 // Anything else is passed through: the setting has always accepted a
45 // language name, and inventing a mapping for one magi cannot verify
46 // would be worse than repeating what the operator wrote.
47 other => other,
48 }
49}
50
51/// `[graph] language` of the repository at `repo`, read best-effort for
52/// callers with no run (and so no config of their own). An unreadable config
53/// is English: a hold reason must still get written.
54pub fn of_repo(repo: &std::path::Path) -> String {
55 crate::config::Config::discover(repo, None)
56 .map(|(c, _)| c.graph.language)
57 .unwrap_or_else(|_| "en".to_owned())
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn every_japanese_spelling_is_accepted() {
66 for l in ["ja", "JA", "jp", "Japanese", " japanese ", "日本語"] {
67 assert!(is_japanese(l), "{l:?}");
68 }
69 }
70
71 #[test]
72 fn everything_else_is_not_japanese() {
73 for l in ["en", "", "de", "fr", "jap"] {
74 assert!(!is_japanese(l), "{l:?}");
75 }
76 }
77}