1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! One reading of `[graph] language`, shared by everything that has a
//! translated wording table.
//!
//! Only English and Japanese are translated. The setting has always accepted
//! a code or a name, so the Japanese spellings are matched loosely; anything
//! else falls back to English rather than shipping a guess.
/// Is this the default, where nothing needs saying? Empty, `en` or `english`.
pub fn is_english(language: &str) -> bool {
let l = language.trim();
l.is_empty() || l.eq_ignore_ascii_case("en") || l.eq_ignore_ascii_case("english")
}
/// Whether `language` names Japanese: `ja`, `jp`, `japanese` or `日本語`,
/// trimmed and case-insensitive.
pub fn is_japanese(language: &str) -> bool {
let l = language.trim();
l.eq_ignore_ascii_case("ja")
|| l.eq_ignore_ascii_case("jp")
|| l.eq_ignore_ascii_case("japanese")
|| l.eq_ignore_ascii_case("日本語")
}
/// The language an agent is told to write in, by name.
///
/// `[graph] language` takes a code or a name, and a code reached the prompt
/// verbatim: "Write all prose in ja" is an instruction a model can read as
/// noise, and the questions agents asked came back in English on a repository
/// configured for Japanese. Naming the language is the whole fix. The same
/// name is what a CLI's own response-language setting expects.
pub fn name(language: &str) -> &str {
if is_japanese(language) {
return "Japanese";
}
if is_english(language) {
return "English";
}
match language.trim() {
"de" => "German",
"fr" => "French",
"es" => "Spanish",
"ko" => "Korean",
"zh" => "Chinese",
// Anything else is passed through: the setting has always accepted a
// language name, and inventing a mapping for one magi cannot verify
// would be worse than repeating what the operator wrote.
other => other,
}
}
/// `[graph] language` of the repository at `repo`, read best-effort for
/// callers with no run (and so no config of their own). An unreadable config
/// is English: a hold reason must still get written.
pub fn of_repo(repo: &std::path::Path) -> String {
crate::config::Config::discover(repo, None)
.map(|(c, _)| c.graph.language)
.unwrap_or_else(|_| "en".to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_japanese_spelling_is_accepted() {
for l in ["ja", "JA", "jp", "Japanese", " japanese ", "日本語"] {
assert!(is_japanese(l), "{l:?}");
}
}
#[test]
fn everything_else_is_not_japanese() {
for l in ["en", "", "de", "fr", "jap"] {
assert!(!is_japanese(l), "{l:?}");
}
}
}