Comment editor: own-language notes are not translated, the whole block takes the click, Cancel as tall as Done
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run

A short note in the interface language's script inside a book of another script
(and a longer one the detector tells unreliably) is the reader's own: it is left
out of the translation plan. The comment block, not only its text, takes the
double click and the right button.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-25 02:54:13 +03:00
parent d3477f140b
commit 7aee0467db
2 changed files with 102 additions and 15 deletions

View file

@ -688,16 +688,21 @@ fn comment_block(ui: &mut Ui, n: &Notation, text: &str, slot: Slot, out: &mut No
bottom: 6,
});
}
let hovered = ui.rect_contains_pointer(ui.available_rect_before_wrap());
let r = frame.show(ui, |ui| {
ui.set_width(ui.available_width());
let resp = ui.add(
egui::Label::new(RichText::new(shown).color(comment_color(ui)).size(14.5))
.sense(Sense::click()),
);
comment_response(&resp, slot, out);
ui.label(RichText::new(shown).color(comment_color(ui)).size(14.5));
});
let _ = hovered;
// The whole block, not only its text, takes the double click and the right button.
let resp = ui.interact(
r.response.rect,
ui.id().with((
"comment",
slot.node().index(),
matches!(slot, Slot::Before(_)),
)),
Sense::click(),
);
comment_response(&resp, slot, out);
if let Some(m) = mark {
let at = r.response.rect.right_top() + vec2(-8.0, 7.0);
translation_mark(ui, at, m, text);
@ -761,7 +766,11 @@ fn editor_block(ui: &mut Ui, editor: &mut Option<&mut CommentEditor>, out: &mut
ui.add(egui::Label::new(RichText::new(title).strong()).truncate());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if narrow {
if ui.button("×").on_hover_text(tr("Cancel (Esc)")).clicked() {
if ui
.add(egui::Button::new("×").min_size(vec2(0.0, 30.0)))
.on_hover_text(tr("Cancel (Esc)"))
.clicked()
{
out.editor = Some(EditorOutcome::Cancel);
}
if crate::widgets::primary(ui, "✔")
@ -771,7 +780,11 @@ fn editor_block(ui: &mut Ui, editor: &mut Option<&mut CommentEditor>, out: &mut
out.editor = Some(EditorOutcome::Done);
}
} else {
if ui.button(tr("Cancel")).on_hover_text("Esc").clicked() {
if ui
.add(egui::Button::new(tr("Cancel")).min_size(vec2(0.0, 30.0)))
.on_hover_text("Esc")
.clicked()
{
out.editor = Some(EditorOutcome::Cancel);
}
if crate::widgets::primary(ui, tr("Done"))

View file

@ -357,12 +357,44 @@ const LANGUAGES: &[whatlang::Lang] = {
]
};
fn detect(text: &str) -> Option<whatlang::Lang> {
fn detect_info(text: &str) -> Option<whatlang::Info> {
static DETECTOR: std::sync::OnceLock<whatlang::Detector> = std::sync::OnceLock::new();
DETECTOR
.get_or_init(|| whatlang::Detector::with_allowlist(LANGUAGES.to_vec()))
.detect(text)
.map(|i| i.lang())
}
/// The language of one comment for the plan: a short text in the script of `target`
/// that the detector cannot tell apart reliably (Russian notes read as Ukrainian or
/// Bulgarian) is taken for `target` — the reader's own notes are not translated.
fn comment_lang(text: &str, target: Option<whatlang::Lang>) -> Option<whatlang::Lang> {
let info = detect_info(text)?;
if let Some(t) = target
&& info.lang() != t
&& !info.is_reliable()
&& info.script() == script_of(t)
{
return Some(t);
}
Some(info.lang())
}
/// The script a language is written in, as the detector names it.
fn script_of(lang: whatlang::Lang) -> whatlang::Script {
use whatlang::Lang::*;
match lang {
Rus | Ukr | Bel | Bul | Mkd | Srp => whatlang::Script::Cyrillic,
Ell => whatlang::Script::Greek,
Kat => whatlang::Script::Georgian,
Hye => whatlang::Script::Armenian,
Heb => whatlang::Script::Hebrew,
Ara | Pes => whatlang::Script::Arabic,
Cmn => whatlang::Script::Mandarin,
Jpn => whatlang::Script::Katakana,
Kor => whatlang::Script::Hangul,
Hin => whatlang::Script::Devanagari,
_ => whatlang::Script::Latin,
}
}
/// The language as the detector knows it (`None`: it does not tell Kazakh, so every
@ -401,16 +433,16 @@ const SHORT: usize = 25;
pub fn plan(texts: &[String], target: Lang) -> Option<Plan> {
let target = language_of(target);
let mut letters: HashMap<whatlang::Lang, usize> = HashMap::new();
let detected: Vec<(usize, Option<whatlang::Lang>)> = texts
let detected: Vec<(usize, Option<whatlang::Lang>, Option<whatlang::Script>)> = texts
.iter()
.map(|t| {
let p = prose(t);
let n = p.chars().filter(|c| c.is_alphabetic()).count();
let lang = (n >= SHORT).then(|| detect(&p)).flatten();
let lang = (n >= SHORT).then(|| comment_lang(&p, target)).flatten();
if let Some(l) = lang {
*letters.entry(l).or_default() += n;
}
(n, lang)
(n, lang, whatlang::detect_script(&p))
})
.collect();
let mut game_lang = letters.iter().max_by_key(|(_, n)| **n).map(|(l, _)| *l);
@ -428,10 +460,22 @@ pub fn plan(texts: &[String], target: Lang) -> Option<Plan> {
}
}
}
// A short note in the script of the interface language, in a book of another
// script ("Размен в пользу белых" in a Dutch book), is the reader's own: not foreign.
let own_script = |script: Option<whatlang::Script>| {
target.is_some_and(|t| script == Some(script_of(t)))
&& game_lang.is_some_and(|g| script != Some(script_of(g)))
};
let foreign: Vec<usize> = detected
.iter()
.enumerate()
.filter(|(_, (n, lang))| *n > 0 && lang.or(game_lang).is_some_and(|l| Some(l) != target))
.filter(|(_, (n, lang, script))| {
*n > 0
&& match lang {
Some(l) => Some(*l) != target,
None => game_lang.is_some_and(|g| Some(g) != target) && !own_script(*script),
}
})
.map(|(i, _)| i)
.collect();
let foreign_letters: usize = foreign.iter().map(|&i| detected[i].0).sum();
@ -1573,3 +1617,33 @@ mod tests {
assert!(seen[0].get("response_format").is_none());
}
}
#[cfg(test)]
mod own_comment_tests {
use super::*;
#[test]
fn a_comment_in_the_interface_language_is_not_translated() {
// A Dutch book with the reader's own Russian notes: the notes stay.
let texts: Vec<String> = [
"Wit heeft een klein voordeel dankzij het loperpaar en de betere pionnenstructuur.",
"Zwart moet nu actief spelen, anders wordt de stelling langzaam slechter.",
"Een rustige zet die de spanning in het centrum handhaaft.",
"я не знаю насколько хорош этот ход",
"Размен в пользу белых",
]
.into_iter()
.map(String::from)
.collect();
let plan = plan(&texts, Lang::Ru).expect("the Dutch comments need a translation");
assert_eq!(plan.source, whatlang::Lang::Nld);
assert!(
!plan
.texts
.iter()
.any(|t| t.starts_with("я не знаю") || t.starts_with("Размен")),
"own Russian notes in the plan: {:?}",
plan.texts
);
}
}