mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-29 20:27:05 -06:00
Strings in code stay English and are the lookup keys; `crates/ui-egui/src/i18n` maps them to display text at render time from one `*.tsv` catalog per language (`LANGUAGES` registry, `tl!`/`t`/`tr`/`tr_ctx`/`tr_id`/`tn`/`fmt`). Command ids, menu paths, `ui.menu.list`, the control channel, the CLI, MCP, status messages and undo labels never see translated text, so agents, scripts and the existing tests are unaffected (under `cargo test` the UI is always English). Wired: in-window menus and the macOS native menu (rebuilt on a language change), dynamic labels (Undo X, Reset X, Last Effect: X as templates), the command palette (matches English and translated text), dock tabs, panel tooltips, the toolbar (category names now cut by characters, not bytes), the dialog frame, the shared widgets, the Preferences dialog, the shortcut editor, the hint and status bars, the Home screen, and the literals of every panel and dialog. Preference `interfaceLanguage` (User Interface › Language): `auto` or a code. `auto` follows `VECTORCRAFT_LOCALE`, `LC_ALL`/`LC_MESSAGES`/`LANG`/`LANGUAGE`, the macOS preferred languages and, on Windows (which sets no `LANG`), the user locale from `HKCU\Control Panel\International`. `zh-TW`/`zh-HK`/`zh-MO`/ `zh-Hant-*` resolve to `zh-hant`; Simplified locales stay English until a `zh-hans` catalog exists. `zh-hant.tsv` (~2,850 rows) is clean-room, in the vocabulary used in Taiwan; terms shared with PhotoCraft reuse its wording, vector terms use everyday vector-drawing vocabulary. Tests: catalog well-formedness (placeholders, ellipses, command ids, no Simplified characters), locale resolution, and two coverage tests that fail when a menu string, panel/tool/preference label, dialog button, hint-bar fragment or `tl!` literal has no row in a language marked `complete_menus`. `VECTORCRAFT_I18N_DUMP=file cargo test -p vectorcraft-ui-egui dump_source_strings` lists the strings for translators. Also: two clippy findings under rustc 1.97 fixed in passing. Integrated with main (squashed onto it, 42 commits on): the Czech interface (#213) moves from its Rust table to `i18n/cs.tsv` on the registry (all 556 rows, Czech plural rule, its menu-coverage and glyph tests kept); Find Font's resolution notes and the canvas context menu's labels are translated and covered by the catalog tests (`menus::CONTEXT_LABELS`); the glyph test checks CJK text only when the craft-fonts build input is embedded (#220 moved the Japanese fonts out of this repository); a language saved by an older version carries over to `interfaceLanguage`; Windows' locale lookup runs reg.exe by its full path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
144 lines
5.7 KiB
Rust
144 lines
5.7 KiB
Rust
//! Text Import Options: placing a text file asks how to read it (platform and character set) and
|
||
//! what to clean up (line breaks inside paragraphs, blank lines between them, runs of spaces). OK
|
||
//! places it as area type (`file.place` with `text`).
|
||
//!
|
||
//! Fields: `platform` (`windows` | `mac`), `characterSet` (`unicode` | `ansi`),
|
||
//! `removeLineReturns`, `removeParagraphReturns`, `replaceSpaces` (on or off), `spaces` (how many
|
||
//! spaces make a tab, 2–100), and `__params` (the `file.place` params naming the file).
|
||
|
||
use serde_json::{Value, json};
|
||
|
||
use super::{DialogSpec, form};
|
||
use crate::state::Dialog;
|
||
use crate::{VectorcraftApp, widgets};
|
||
|
||
/// The dialog kind of Text Import Options.
|
||
pub const KIND: &str = "textImport";
|
||
|
||
pub(super) const SPEC: DialogSpec =
|
||
DialogSpec { heading: |_| tl!("Text Import Options").into(), body, confirm, min_width: 340.0, ..DialogSpec::FORM };
|
||
|
||
/// The label column of the Encoding rows.
|
||
const LABEL: f32 = 104.0;
|
||
|
||
/// Is the file `p` names (`path` or `name`) a text file?
|
||
pub fn is_text(p: &Value) -> bool {
|
||
let name = p.get("path").or_else(|| p.get("name")).and_then(Value::as_str).unwrap_or_default();
|
||
vectorcraft_engine::cmd::fileio::TEXT_EXTS.contains(&vectorcraft_engine::cmd::fileio::extension(name).as_str())
|
||
}
|
||
|
||
/// Ask how to place the text file `params` (`file.place` params) names.
|
||
pub fn open(app: &mut VectorcraftApp, params: &Value) -> Value {
|
||
let fields = json!({
|
||
"platform": if cfg!(target_os = "macos") { "mac" } else { "windows" },
|
||
"characterSet": "unicode",
|
||
"removeLineReturns": false,
|
||
"removeParagraphReturns": false,
|
||
"replaceSpaces": false,
|
||
"spaces": 3,
|
||
"__params": params,
|
||
});
|
||
app.ui.dialog = Some(Dialog::new(KIND, fields));
|
||
json!({ "dialog": KIND })
|
||
}
|
||
|
||
fn body(_: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||
widgets::subheader(ui, tl!("Encoding"));
|
||
form::choice(ui, d, "platform", tl!("Platform:"), (LABEL, 180.0), &[("windows", "Windows"), ("mac", "Mac")]);
|
||
form::choice(ui, d, "characterSet", tl!("Character Set:"), (LABEL, 180.0), &[("unicode", "Unicode"), ("ansi", "ANSI")]);
|
||
ui.add_space(10.0);
|
||
widgets::subheader(ui, tl!("Line Breaks"));
|
||
for (key, label) in
|
||
[("removeLineReturns", tl!("Join the lines of each paragraph")), ("removeParagraphReturns", tl!("Remove blank lines between paragraphs"))]
|
||
{
|
||
let on = d.bool(key);
|
||
if widgets::check(ui, label, on, true) {
|
||
d.fields.insert(key.into(), json!(!on));
|
||
}
|
||
}
|
||
ui.add_space(10.0);
|
||
widgets::subheader(ui, tl!("Spaces"));
|
||
let on = d.bool("replaceSpaces");
|
||
ui.horizontal(|ui| {
|
||
if widgets::check(ui, tl!("Replace"), on, true) {
|
||
d.fields.insert("replaceSpaces".into(), json!(!on));
|
||
}
|
||
ui.add_enabled_ui(on, |ui| {
|
||
if let Some(n) = widgets::plain_field(ui, "text-import-spaces", d.f64("spaces", 3.0), "", 0, 44.0) {
|
||
d.fields.insert("spaces".into(), json!(n.round().clamp(2.0, 100.0)));
|
||
}
|
||
widgets::dim_label(ui, tl!("or more spaces with a tab"));
|
||
});
|
||
});
|
||
false
|
||
}
|
||
|
||
/// `file.place`'s `text` options from the fields.
|
||
fn options(d: &Dialog) -> Value {
|
||
let mut o = json!({
|
||
"platform": d.str("platform"),
|
||
"characterSet": d.str("characterSet"),
|
||
"removeLineReturns": d.bool("removeLineReturns"),
|
||
"removeParagraphReturns": d.bool("removeParagraphReturns"),
|
||
});
|
||
if d.bool("replaceSpaces") {
|
||
o["replaceSpaces"] = json!(d.f64("spaces", 3.0).round().clamp(2.0, 100.0) as u64);
|
||
}
|
||
o
|
||
}
|
||
|
||
/// Places the file with the options; closes first, so an error shows in the status bar.
|
||
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||
let mut params = d.fields.get("__params").cloned().unwrap_or_else(|| json!({}));
|
||
params["text"] = options(d);
|
||
app.ui.dialog = None;
|
||
crate::place::run(app, ¶ms)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use vectorcraft_engine::Session;
|
||
use vectorcraft_engine::doc::NodeKind;
|
||
|
||
fn frame(app: &mut VectorcraftApp) -> String {
|
||
crate::tests_labels::painted_text(app, |app, ui| super::super::show(app, ui.ctx()))
|
||
}
|
||
|
||
#[test]
|
||
fn placing_a_text_file_asks_for_the_import_options_then_places_area_type() {
|
||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||
let text = b"one\r\ntwo\r\n\r\nthree\r\n";
|
||
let file = json!({"name": "notes.txt", "dataBase64": vectorcraft_format::base64_encode(text)});
|
||
let r = app.run("file.place", file).unwrap();
|
||
assert_eq!(r["dialog"], KIND);
|
||
let text = frame(&mut app);
|
||
for label in [
|
||
"Text Import Options",
|
||
"Encoding",
|
||
"Platform:",
|
||
"Character Set:",
|
||
"Unicode",
|
||
"Line Breaks",
|
||
"Join the lines of each paragraph",
|
||
"Spaces",
|
||
"OK",
|
||
"Cancel",
|
||
] {
|
||
assert!(text.contains(label), "{label} in {text}");
|
||
}
|
||
let d = app.ui.dialog.as_mut().unwrap();
|
||
d.fields.insert("removeLineReturns".into(), json!(true));
|
||
d.fields.insert("removeParagraphReturns".into(), json!(true));
|
||
assert_eq!(options(d)["removeLineReturns"], true);
|
||
let r = super::super::confirm(&mut app).unwrap();
|
||
assert!(app.ui.dialog.is_none());
|
||
let st = app.session.doc().unwrap();
|
||
let n = st.doc.node(vectorcraft_engine::doc::NodeId(r["ids"][0].as_u64().unwrap())).unwrap();
|
||
let NodeKind::Text(t) = &n.kind else { panic!("type") };
|
||
assert_eq!(t.plain_text(), "one two\nthree", "one paragraph per block");
|
||
assert_eq!(st.history.undo.len(), 1);
|
||
}
|
||
}
|