From 48634accbc67d835c46f7dc4db05243f9ca9debf Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 15 Nov 2026 18:33:45 -0300 Subject: [PATCH 1/4] feat(gp): GeoLibre tools in the interface language from GeoLibre's language packs (#2224) Read GeoLibre's Whitebox language packs (languages.geolibre.app), downloaded on demand: checked as GeoLibre checks them, kept in the app data folder, and used by the catalogue's fallback for tool names, summaries, parameter labels and help, and toolbox names. Find Tools also matches the shown (translated) name and summary, without case or accents. Tools pane: Translate tools / Remove translations; commands gp.installGeolibreLanguage, gp.importGeolibreLanguage, gp.uninstallGeolibreLanguage. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 134 +++++++ crates/engine/src/tests.rs | 16 +- crates/geolibre/src/langpack.rs | 594 ++++++++++++++++++++++++++++++++ crates/geolibre/src/lib.rs | 1 + crates/ui-egui/src/i18n/mod.rs | 17 +- crates/ui-egui/src/panes.rs | 120 ++++++- docs/user-guide/analysis.md | 16 + 7 files changed, 884 insertions(+), 14 deletions(-) create mode 100644 crates/geolibre/src/langpack.rs diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 76f660e3c7..1cf6c88ae7 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -46,8 +46,24 @@ pub fn commands() -> Vec { C::new("gp.geolibre", "GeoLibre Tools", "Analyze › Geoprocessing", |_, _| { let mut status = geolibre_status(); status["passedOver"] = json!(geolibre_passed_over()); + status["languages"] = geolibre_languages(); Ok(status) }).roles(&[]).effects(crate::policy::Effects::NONE).view(), + C::new("gp.installGeolibreLanguage", "Download GeoLibre Tool Translations", "Analyze › Geoprocessing", install_geolibre_language_cmd) + .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) + .view() + .params(r#"{"locale": "the language's code, such as \"pt\" (one GeoLibre publishes a pack for)"}"#), + C::new("gp.importGeolibreLanguage", "Import GeoLibre Tool Translations", "Analyze › Geoprocessing", import_geolibre_language) + .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved).with(crate::policy::Effect::Files)) + .view() + .params(r#"{"path": "a GeoLibre language pack (.json)"}"#), + C::new("gp.uninstallGeolibreLanguage", "Remove GeoLibre Tool Translations", "Analyze › Geoprocessing", uninstall_geolibre_language) + .roles(&[]) + .effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Reserved)) + .view() + .params(r#"{"locale": "the language's code"}"#), C::new("gp.installGeolibre", "Install GeoLibre Tools", "Analyze › Geoprocessing", |_, _| { let path = install_geolibre(&|_, _| {})?; #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] @@ -135,6 +151,124 @@ pub fn geolibre_passed_over() -> Option { None } +/// The GeoLibre tool translations (#2224): the languages GeoLibre publishes a pack for, the ones +/// kept in the data folder, and the one in use. +fn geolibre_languages() -> Value { + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + { + json!({"available": rustgis_geolibre::langpack::LOCALES, "installed": rustgis_geolibre::langpack::kept(), + "active": rustgis_geolibre::langpack::active_locale()}) + } + #[cfg(not(all(feature = "geolibre", not(target_arch = "wasm32"))))] + { + json!({"available": [], "installed": [], "active": null}) + } +} + +/// Whether GeoLibre publishes tool translations for `locale`, and this build can download them. +pub fn geolibre_language_available(locale: &str) -> bool { + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + { + rustgis_geolibre::langpack::url(locale).is_some() + } + #[cfg(not(all(feature = "geolibre", not(target_arch = "wasm32"))))] + { + let _ = locale; + false + } +} + +/// Whether tool translations for `locale` are in use (kept in the data folder, read the first +/// time this is asked, then from memory). +pub fn geolibre_language_in_use(locale: &str) -> bool { + #[cfg(feature = "geolibre")] + { + rustgis_geolibre::langpack::in_use(locale) + } + #[cfg(not(feature = "geolibre"))] + { + let _ = locale; + false + } +} + +/// `english`, a GeoLibre tool's text (its name, summary, a parameter's label or help, a toolbox +/// name), in `locale`, from the translations kept for it; the interface's catalogue falls back to +/// it as to a plugin's `i18n`. `None` without them. +pub fn geolibre_translate(locale: &str, english: &str) -> Option { + #[cfg(feature = "geolibre")] + { + rustgis_geolibre::langpack::translate(locale, english) + } + #[cfg(not(feature = "geolibre"))] + { + let _ = (locale, english); + None + } +} + +/// Changes whenever [`geolibre_translate`] may answer differently (translations installed or +/// removed, the tools reloaded), so a cache of its answers starts over. +pub fn geolibre_language_revision() -> u64 { + #[cfg(feature = "geolibre")] + { + rustgis_geolibre::langpack::revision() + } + #[cfg(not(feature = "geolibre"))] + 0 +} + +/// Downloads GeoLibre's tool translations for `locale` (about 2 MB), keeps them and puts them in +/// use; returns how many tools they name. Blocks on one request: the app calls it from a +/// background thread. Native only. +pub fn install_geolibre_language(locale: &str) -> Result { + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + { + rustgis_geolibre::langpack::download(locale) + } + #[cfg(not(all(feature = "geolibre", not(target_arch = "wasm32"))))] + { + let _ = locale; + Err("this build can't download GeoLibre tool translations".into()) + } +} + +/// `gp.installGeolibreLanguage`: one request of about 2 MB, which gives up after a minute, run +/// where it is called (like `gp.installGeolibre`); the app's button runs it on a thread. +fn install_geolibre_language_cmd(_s: &mut Session, p: &Value) -> CmdResult { + let locale = ps(p, "locale").map(str::trim).filter(|l| !l.is_empty()).ok_or("give the language's \"locale\", such as \"pt\"")?; + let tools = install_geolibre_language(locale)?; + Ok(json!({"locale": locale, "tools": tools})) +} + +fn import_geolibre_language(_s: &mut Session, p: &Value) -> CmdResult { + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + { + let path = ps(p, "path").map(str::trim).filter(|p| !p.is_empty()).ok_or("give the pack's \"path\"")?; + let (locale, tools) = rustgis_geolibre::langpack::import(std::path::Path::new(path))?; + Ok(json!({"locale": locale, "tools": tools})) + } + #[cfg(not(all(feature = "geolibre", not(target_arch = "wasm32"))))] + { + let _ = p; + Err("this build can't import GeoLibre tool translations".into()) + } +} + +fn uninstall_geolibre_language(_s: &mut Session, p: &Value) -> CmdResult { + #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] + { + let locale = ps(p, "locale").map(str::trim).filter(|l| !l.is_empty()).ok_or("give the language's \"locale\"")?; + rustgis_geolibre::langpack::remove(locale)?; + Ok(geolibre_languages()) + } + #[cfg(not(all(feature = "geolibre", not(target_arch = "wasm32"))))] + { + let _ = p; + Err("this build keeps no GeoLibre tool translations".into()) + } +} + /// Downloads and installs the pinned GeoLibre runner release for this platform; returns the /// program's path. `progress` gets the bytes received and the expected total. Blocks: the app /// calls it from a background thread, then picks the runner with `gp.setGeolibre`. Native only. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 79d1154b86..ffe63ebd47 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -5081,6 +5081,9 @@ fn hostile_params_never_panic(shard: usize) { | "gp.installGeolibre" // Deletes the installed GeoLibre runner. | "gp.uninstallGeolibre" + // Downloads GeoLibre's tool translations, and deletes the kept ones. + | "gp.installGeolibreLanguage" + | "gp.uninstallGeolibreLanguage" | "plugin.registry" | "plugin.installFromRegistry" // Ask Earth Search when no API is given. @@ -14274,8 +14277,17 @@ fn plugin_edits_are_one_undo_step() { assert!(forward(&mut s, "eng-edit", "layer.remove", json!({"layer": "Nope"})).is_err()); assert_eq!(s.map().layers.len(), n); // Some things are off limits whatever the permissions. - for op in ["script.run", "project.new", "edit.undo", "plugin.uninstall", "plugin:eng-edit/h", "gp.setGeolibre", "gp.uninstallGeolibre"] - { + for op in [ + "script.run", + "project.new", + "edit.undo", + "plugin.uninstall", + "plugin:eng-edit/h", + "gp.setGeolibre", + "gp.uninstallGeolibre", + "gp.installGeolibreLanguage", + "gp.uninstallGeolibreLanguage", + ] { assert!(forward(&mut s, "eng-edit", op, json!({})).is_err(), "{op}"); } // Files need their own permission. diff --git a/crates/geolibre/src/langpack.rs b/crates/geolibre/src/langpack.rs new file mode 100644 index 0000000000..9833fee9ea --- /dev/null +++ b/crates/geolibre/src/langpack.rs @@ -0,0 +1,594 @@ +//! The GeoLibre tools in the interface's language, from GeoLibre's language packs (#2224). +//! +//! GeoLibre keeps the translations of its tool catalogue (each tool's name and description, its +//! parameters' labels and help, the toolbox names) out of its builds, in packs of about 2 MB a +//! language that it downloads when asked +//! ([geolibre-language-packs](https://github.com/opengeos/geolibre-language-packs)). RustGIS reads +//! the same packs: [`download`] fetches the one for a language, [`import`] reads one from a file, +//! and either is kept in the app's data folder, where [`translate`] finds it the first time that +//! language asks for a GeoLibre string. A pack is untrusted input, checked as GeoLibre checks it +//! ([`parse`]): its format, version and scope, string values only, at most [`MAX_BYTES`]. +//! +//! [`translate`] maps the English text the tools show (the [`ToolSpec`] strings) to the pack's, +//! by tool id and parameter name, so the interface's catalogue lookup can fall back to it like +//! a plugin's `i18n`. The English names stay the tools' names everywhere else (scripts, the +//! command line, layer names, search by the English words). + +use std::collections::{BTreeMap, HashMap, HashSet}; +use std::sync::Mutex; +use std::sync::atomic::{AtomicU64, Ordering}; + +use rustgis_analysis::ToolSpec; +use serde_json::{Map, Value}; + +/// Where GeoLibre publishes its packs: `{BASE_URL}/v1/whitebox/{locale}.json`. +pub const BASE_URL: &str = "https://languages.geolibre.app"; + +/// The largest pack read (GeoLibre's limit); the published ones are about 2 MB. +pub const MAX_BYTES: usize = 5 * 1024 * 1024; + +/// The most strings a pack may hold (GeoLibre's limit). +const MAX_LEAVES: usize = 100_000; + +/// The languages GeoLibre publishes a pack for (its `v1/index.json`, the English template left +/// out), by the codes RustGIS's languages use. +pub const LOCALES: &[&str] = + &["ar", "az", "de", "es", "fa", "fr", "he", "hi", "id", "it", "ja", "ka", "ko", "nl", "pt", "ru", "th", "tr", "vi", "zh"]; + +/// The address of the published pack for `locale`, if GeoLibre publishes one. +pub fn url(locale: &str) -> Option { + LOCALES.contains(&locale).then(|| format!("{BASE_URL}/v1/whitebox/{locale}.json")) +} + +/// One tool's strings in a pack. +#[derive(Debug, Default)] +struct ToolText { + name: Option, + description: Option, + /// By parameter name: its label and its description. + params: HashMap, Option)>, +} + +/// A language pack, checked. +#[derive(Debug)] +pub struct Pack { + /// The language it translates into (`pt`, `zh`…). + pub locale: String, + /// By tool id (without [`crate::PREFIX`]). + tools: HashMap, + /// Toolbox names by GeoLibre's key: `terrain`, `lidarAnalysisMetrics` (`LiDAR - …`). + categories: BTreeMap, + /// Subcategory names by GeoLibre's key: `sampling_gridding`. + subcategories: HashMap, +} + +impl Pack { + /// How many tools it names. + pub fn tools(&self) -> usize { + self.tools.values().filter(|t| t.name.is_some()).count() + } +} + +/// Whether `code` is a language code as GeoLibre accepts one: two or three lower-case letters, +/// then `-` and 2 to 8 letters or digits, any number of times (`pt`, `zh-Hant`). It also keeps a +/// pack's file name to these characters. +fn valid_locale(code: &str) -> bool { + let mut parts = code.split('-'); + let first = parts.next().unwrap_or(""); + (2..=3).contains(&first.len()) + && first.bytes().all(|b| b.is_ascii_lowercase()) + && parts.all(|p| (2..=8).contains(&p.len()) && p.bytes().all(|b| b.is_ascii_alphanumeric())) +} + +/// Counts the strings in `v`, which must hold only objects and strings; past [`MAX_LEAVES`] +/// in all (`count` carries the running total), it's an error. +fn count_strings(v: &Value, count: &mut usize) -> Result<(), String> { + match v { + Value::String(_) => { + *count += 1; + if *count > MAX_LEAVES { + return Err(format!("the language pack holds more than {MAX_LEAVES} strings")); + } + Ok(()) + } + Value::Object(m) => m.values().try_for_each(|c| count_strings(c, count)), + _ => Err("a language pack holds only objects and strings".into()), + } +} + +/// `v` as an object whose keys are all in `allowed`, else the error `what`. +fn object_of<'a>(v: &'a Value, allowed: &[&str], what: &str) -> Result<&'a Map, String> { + v.as_object().filter(|m| m.keys().all(|k| allowed.contains(&k.as_str()))).ok_or_else(|| what.to_string()) +} + +fn string_at(v: &Value, key: &str) -> Option { + v.get(key).and_then(Value::as_str).map(str::trim).filter(|s| !s.is_empty()).map(str::to_string) +} + +fn strings_of(v: Option<&Value>) -> impl Iterator + '_ { + v.and_then(Value::as_object) + .into_iter() + .flatten() + .filter_map(|(k, v)| Some((k.clone(), v.as_str()?.trim().to_string()))) + .filter(|(_, v)| !v.is_empty()) +} + +/// Reads and checks a pack, as GeoLibre does: the `geolibre-language-pack` format, version 1, +/// the `whitebox` scope, a valid language code, only the Processing translations (tool metadata, +/// toolbox and menu names), strings only, and at most [`MAX_BYTES`] and [`MAX_LEAVES`] strings. +pub fn parse(text: &str) -> Result { + if text.len() > MAX_BYTES { + return Err(format!("the language pack is larger than {} MB", MAX_BYTES / (1024 * 1024))); + } + let raw: Value = serde_json::from_str(text).map_err(|e| format!("the language pack isn't valid JSON: {e}"))?; + let root = raw.as_object().ok_or("the language pack isn't a JSON object")?; + if root.get("format").and_then(Value::as_str) != Some("geolibre-language-pack") + || root.get("scope").and_then(Value::as_str) != Some("whitebox") + { + return Err("this isn't a GeoLibre language pack for the Whitebox tools".into()); + } + if root.get("formatVersion").and_then(Value::as_u64) != Some(1) { + return Err(format!("language pack format {} isn't supported (only 1)", root.get("formatVersion").unwrap_or(&Value::Null))); + } + let locale = + root.get("locale").and_then(Value::as_str).filter(|l| valid_locale(l)).ok_or("the language pack has no valid language code")?; + let translations = object_of( + root.get("translations").unwrap_or(&Value::Null), + &["processing"], + "the language pack may hold only Processing translations", + )?; + let processing = object_of( + translations.get("processing").unwrap_or(&Value::Null), + &["toolMeta", "whitebox"], + "the language pack's Processing translations aren't valid", + )?; + let mut count = 0; + let meta = match processing.get("toolMeta") { + Some(m) => { + let m = object_of(m, &["whitebox"], "the language pack may hold only Whitebox tool metadata")?; + let w = m.get("whitebox").filter(|w| w.is_object()).ok_or("the language pack's tool metadata isn't valid")?; + count_strings(w, &mut count)?; + Some(w) + } + None => None, + }; + let menus = match processing.get("whitebox") { + Some(w) => { + object_of(w, &["categories", "menuTool", "menuSubcategory"], "the language pack's Whitebox menus hold keys it may not")?; + count_strings(w, &mut count)?; + Some(w) + } + None => None, + }; + if count == 0 { + return Err("the language pack holds no translations".into()); + } + let tools = meta + .and_then(Value::as_object) + .into_iter() + .flatten() + .map(|(id, t)| { + let params = t + .get("params") + .and_then(Value::as_object) + .into_iter() + .flatten() + .map(|(p, v)| (p.clone(), (string_at(v, "label"), string_at(v, "description")))) + .collect(); + (id.clone(), ToolText { name: string_at(t, "name"), description: string_at(t, "description"), params }) + }) + .collect(); + Ok(Pack { + locale: locale.to_string(), + tools, + categories: strings_of(menus.and_then(|m| m.get("categories"))).collect(), + subcategories: strings_of(menus.and_then(|m| m.get("menuSubcategory"))).collect(), + }) +} + +/// The words of `s`: its runs of ASCII letters and digits (`I/O & Management` → `I`, `O`, +/// `Management`), as GeoLibre's keys are made from its English names. +fn words(s: &str) -> impl Iterator + '_ { + s.split(|c: char| !c.is_ascii_alphanumeric()).filter(|w| !w.is_empty()).map(str::to_ascii_lowercase) +} + +/// `Remote Sensing` → `remoteSensing`, a category's key. +fn camel(s: &str) -> String { + words(s) + .enumerate() + .map(|(i, w)| if i == 0 { w } else { w.get(..1).unwrap_or("").to_ascii_uppercase() + w.get(1..).unwrap_or("") }) + .collect() +} + +/// `Sampling & Gridding` → `sampling_gridding`, a subcategory's key. +fn snake(s: &str) -> String { + words(s).collect::>().join("_") +} + +/// A top toolbox's name (`Terrain`, `LiDAR`): its own key's, else the first part of a +/// `LiDAR - …` category's (the pack names some tops only with their subcategory). +fn category_name<'a>(pack: &'a Pack, top: &str) -> Option<&'a str> { + let key = camel(top); + if key.is_empty() { + return None; + } + if let Some(name) = pack.categories.get(&key) { + return Some(name); + } + pack.categories + .range(key.clone()..) + .take_while(|(k, _)| k.starts_with(&key)) + .find(|(k, _)| k.get(key.len()..key.len() + 1).is_some_and(|c| c.bytes().all(|b| b.is_ascii_uppercase()))) + .and_then(|(_, v)| v.split(" - ").next()) + .map(str::trim) + .filter(|s| !s.is_empty()) +} + +/// English text → the pack's, for the strings `tools` show: names, summaries, parameter labels +/// and help, and toolbox names. The first translation of a string wins. +fn english_to_pack(pack: &Pack, tools: &[ToolSpec]) -> HashMap { + let mut map = HashMap::new(); + let mut put = |en: &str, local: Option<&str>| { + if let Some(local) = local + && !en.trim().is_empty() + && local != en + { + map.entry(en.to_string()).or_insert_with(|| local.to_string()); + } + }; + for spec in tools { + let mut levels = spec.toolbox.split(" › ").skip(1); + if let Some(top) = levels.next() { + put(top, category_name(pack, top)); + } + if let Some(sub) = levels.next() { + put(sub, pack.subcategories.get(&snake(sub)).map(String::as_str)); + } + let Some(text) = pack.tools.get(spec.id.trim_start_matches(crate::PREFIX)) else { continue }; + put(spec.name, text.name.as_deref()); + put(spec.summary, text.description.as_deref()); + for p in spec.params { + let Some((label, help)) = text.params.get(p.name) else { continue }; + put(p.label, label.as_deref()); + put(p.help, help.as_deref()); + } + } + map +} + +/// The pack in use, with its lookup for the tools listed when it was made. +struct Active { + pack: Pack, + map: HashMap, + /// The tool list the lookup was made for: its address and length. + tools: (usize, usize), +} + +static ACTIVE: Mutex> = Mutex::new(None); + +/// Counts changes to what [`translate`] gives: a pack put in use or removed, the tool list +/// reloaded. +static REVISION: AtomicU64 = AtomicU64::new(0); + +/// Languages whose pack was looked for in the data folder and isn't there. +static MISSING: Mutex>> = Mutex::new(None); + +fn published() -> &'static [ToolSpec] { + rustgis_analysis::published_external_tools_of(crate::SOURCE) +} + +fn tools_key(tools: &[ToolSpec]) -> (usize, usize) { + (tools.as_ptr() as usize, tools.len()) +} + +/// Puts `pack` in use (`None`: none), in place of the one in use. +pub fn activate(pack: Option) { + if let Ok(mut a) = ACTIVE.lock() { + *a = pack.map(|pack| { + let tools = published(); + Active { map: english_to_pack(&pack, tools), tools: tools_key(tools), pack } + }); + } + if let Ok(mut m) = MISSING.lock() { + *m = None; + } + REVISION.fetch_add(1, Ordering::Relaxed); +} + +/// The language of the pack in use. +pub fn active_locale() -> Option { + ACTIVE.lock().ok().and_then(|a| a.as_ref().map(|a| a.pack.locale.clone())) +} + +/// Changes whenever [`translate`] may give something else for the same text, so a cache of its +/// answers knows to start over. +pub fn revision() -> u64 { + if let Ok(mut a) = ACTIVE.lock() + && let Some(a) = a.as_mut() + { + let tools = published(); + if a.tools != tools_key(tools) { + a.map = english_to_pack(&a.pack, tools); + a.tools = tools_key(tools); + REVISION.fetch_add(1, Ordering::Relaxed); + } + } + REVISION.load(Ordering::Relaxed) +} + +/// Whether `locale`'s pack is in use: the one kept for it (natively, read the first time this +/// is asked) or one just downloaded or imported. +pub fn in_use(locale: &str) -> bool { + #[cfg(not(target_arch = "wasm32"))] + load_kept(locale); + active_locale().as_deref() == Some(locale) +} + +/// `english`, a GeoLibre tool's text, in `locale`, from that language's pack: the one in use, +/// else (natively) the one kept in the data folder, read the first time it is asked for. +pub fn translate(locale: &str, english: &str) -> Option { + #[cfg(not(target_arch = "wasm32"))] + load_kept(locale); + let a = ACTIVE.lock().ok()?; + let a = a.as_ref().filter(|a| a.pack.locale == locale)?; + a.map.get(english).cloned() +} + +/// Puts the pack kept for `locale` in use, unless one for it is already, or it was looked for +/// and wasn't there. One file read, of about 2 MB, once per language switch. +#[cfg(not(target_arch = "wasm32"))] +fn load_kept(locale: &str) { + if active_locale().as_deref() == Some(locale) || MISSING.lock().ok().is_some_and(|m| m.as_ref().is_some_and(|m| m.contains(locale))) { + return; + } + match load(locale) { + Ok(Some(pack)) => activate(Some(pack)), + Ok(None) => { + if let Ok(mut m) = MISSING.lock() { + m.get_or_insert_with(Default::default).insert(locale.to_string()); + } + } + Err(e) => { + log::warn!("GeoLibre tool translations ({locale}): {e}"); + if let Ok(mut m) = MISSING.lock() { + m.get_or_insert_with(Default::default).insert(locale.to_string()); + } + } + } +} + +/// Where packs are kept: a `geolibre-languages` folder in the app's data folder, one +/// `.json` each. +#[cfg(not(target_arch = "wasm32"))] +pub fn dir() -> Option { + rustgis_data::dirs::app_data_dir().map(|d| d.join("geolibre-languages")) +} + +#[cfg(not(target_arch = "wasm32"))] +fn file(locale: &str) -> Result { + if !valid_locale(locale) { + return Err(format!("'{locale}' isn't a language code")); + } + dir().map(|d| d.join(format!("{locale}.json"))).ok_or_else(|| "can't find the app's data folder".into()) +} + +/// The languages whose packs are kept, sorted. +#[cfg(not(target_arch = "wasm32"))] +pub fn kept() -> Vec { + let Some(dir) = dir() else { return Vec::new() }; + let mut out: Vec = std::fs::read_dir(dir) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| e.file_name().to_str()?.strip_suffix(".json").filter(|l| valid_locale(l)).map(str::to_string)) + .collect(); + out.sort(); + out +} + +/// The pack kept for `locale` (`None`: none is). +#[cfg(not(target_arch = "wasm32"))] +fn load(locale: &str) -> Result, String> { + let path = file(locale)?; + if !path.is_file() { + return Ok(None); + } + let text = read_limited(&path)?; + let pack = parse(&text)?; + if pack.locale != locale { + return Err(format!("{} holds the '{}' pack", path.display(), pack.locale)); + } + Ok(Some(pack)) +} + +#[cfg(not(target_arch = "wasm32"))] +fn read_limited(path: &std::path::Path) -> Result { + let len = std::fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?.len(); + if len > MAX_BYTES as u64 { + return Err(format!("the language pack is larger than {} MB", MAX_BYTES / (1024 * 1024))); + } + std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display())) +} + +/// Keeps `text`, a checked pack for `locale`, and puts it in use. +#[cfg(not(target_arch = "wasm32"))] +fn keep(pack: Pack, text: &str) -> Result { + let path = file(&pack.locale)?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?; + } + rustgis_data::io::write_atomic(&path, text.as_bytes()).map_err(|e| format!("{}: {e}", path.display()))?; + let n = pack.tools(); + activate(Some(pack)); + Ok(n) +} + +/// Downloads GeoLibre's pack for `locale`, keeps it and puts it in use; returns how many tools +/// it names. One request of about 2 MB, which gives up after a minute. +#[cfg(not(target_arch = "wasm32"))] +pub fn download(locale: &str) -> Result { + let url = url(locale).ok_or_else(|| format!("GeoLibre publishes no tool translations for '{locale}'"))?; + let agent: ureq::Agent = ureq::Agent::config_builder() + .timeout_global(Some(std::time::Duration::from_secs(60))) + .tls_config(rustgis_data::http::tls_config()) + .build() + .into(); + let mut resp = agent + .get(&url) + .header("User-Agent", concat!("RustGIS/", env!("CARGO_PKG_VERSION"))) + .header("Accept", "application/json") + .call() + .map_err(|e| rustgis_data::diagnostics::message_with_address(&url, &e.to_string()))?; + let text = resp + .body_mut() + .with_config() + .limit(MAX_BYTES as u64) + .read_to_string() + .map_err(|e| rustgis_data::diagnostics::message_with_address(&url, &e.to_string()))?; + let pack = parse(&text)?; + if pack.locale != locale { + return Err(format!("{url} holds the '{}' pack", pack.locale)); + } + keep(pack, &text) +} + +/// Reads a pack from `path`, keeps it for the language it names and puts it in use; returns +/// that language and how many tools it names. +#[cfg(not(target_arch = "wasm32"))] +pub fn import(path: &std::path::Path) -> Result<(String, usize), String> { + let text = read_limited(path)?; + let pack = parse(&text)?; + let locale = pack.locale.clone(); + keep(pack, &text).map(|n| (locale, n)) +} + +/// Deletes the pack kept for `locale`, and stops using it. +#[cfg(not(target_arch = "wasm32"))] +pub fn remove(locale: &str) -> Result<(), String> { + let path = file(locale)?; + if !path.is_file() { + return Err(format!("no tool translations are kept for '{locale}'")); + } + std::fs::remove_file(&path).map_err(|e| format!("{}: {e}", path.display()))?; + if active_locale().as_deref() == Some(locale) { + activate(None); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use rustgis_analysis::{ParamKind, ParamSpec}; + + fn pack_text(locale: &str) -> String { + serde_json::json!({ + "format": "geolibre-language-pack", "formatVersion": 1, "scope": "whitebox", "locale": locale, + "translations": {"processing": { + "toolMeta": {"whitebox": {"contours_from_raster": { + "name": "Curvas de nível a partir de raster", + "description": "Cria polilinhas de curvas de nível.", + "params": {"interval": {"label": "Intervalo", "description": "Equidistância."}} + }}}, + "whitebox": { + "categories": {"vector": "Vetor", "remoteSensingSar": "Sensoriamento remoto - SAR"}, + "menuSubcategory": {"sampling_gridding": "Amostragem e grades"}, + "menuTool": {"contours_from_raster": "Curvas de nível a partir de raster"} + } + }} + }) + .to_string() + } + + static PARAMS: [ParamSpec; 1] = [ParamSpec { + name: "interval", + label: "Interval", + kind: ParamKind::Number, + required: false, + default: "", + help: "Contour interval value.", + }]; + + fn no_run(_: &dyn rustgis_analysis::Inputs, _: &Value) -> Result { + Err("not run".into()) + } + + fn tools() -> Vec { + vec![ + ToolSpec { + id: "geolibre:contours_from_raster", + name: "Contours From Raster", + toolbox: "GeoLibre › Vector › Sampling & Gridding", + summary: "Creates contour polylines from a raster surface model.", + params: &PARAMS, + run: no_run, + }, + ToolSpec { + id: "geolibre:sar_info", + name: "SAR Info", + toolbox: "GeoLibre › Remote Sensing › SAR", + summary: "", + params: &[], + run: no_run, + }, + ] + } + + #[test] + fn a_pack_maps_the_tools_english_text_to_its_own() { + let pack = parse(&pack_text("pt")).unwrap(); + assert_eq!(pack.locale, "pt"); + assert_eq!(pack.tools(), 1); + let map = english_to_pack(&pack, &tools()); + assert_eq!(map.get("Contours From Raster").map(String::as_str), Some("Curvas de nível a partir de raster")); + assert_eq!( + map.get("Creates contour polylines from a raster surface model.").map(String::as_str), + Some("Cria polilinhas de curvas de nível.") + ); + assert_eq!(map.get("Interval").map(String::as_str), Some("Intervalo")); + assert_eq!(map.get("Contour interval value.").map(String::as_str), Some("Equidistância.")); + assert_eq!(map.get("Vector").map(String::as_str), Some("Vetor")); + assert_eq!(map.get("Sampling & Gridding").map(String::as_str), Some("Amostragem e grades")); + // A top named only with its subcategories: the first part of theirs. + assert_eq!(map.get("Remote Sensing").map(String::as_str), Some("Sensoriamento remoto")); + assert!(!map.contains_key("SAR"), "the same as the English, or missing: left out"); + assert!(!map.contains_key("SAR Info"), "a tool the pack doesn't name stays in English"); + } + + #[test] + fn keys_follow_geolibres_names() { + assert_eq!(camel("Remote Sensing"), "remoteSensing"); + assert_eq!(camel("LiDAR"), "lidar"); + assert_eq!(snake("I/O & Management"), "i_o_management"); + assert_eq!(snake("Streams - Network Extraction"), "streams_network_extraction"); + } + + #[test] + fn a_pack_is_checked_as_geolibre_checks_it() { + let good: Value = serde_json::from_str(&pack_text("pt")).unwrap(); + let with = |f: &dyn Fn(&mut Value)| { + let mut v = good.clone(); + f(&mut v); + parse(&v.to_string()) + }; + assert!(with(&|v| v["format"] = "other".into()).is_err()); + assert!(with(&|v| v["formatVersion"] = 2.into()).is_err()); + assert!(with(&|v| v["scope"] = "ui".into()).is_err()); + assert!(with(&|v| v["locale"] = "../x".into()).is_err()); + assert!(with(&|v| v["locale"] = "PT".into()).is_err()); + assert!(with(&|v| v["translations"]["menu"] = serde_json::json!({})).is_err(), "only Processing"); + assert!(with(&|v| v["translations"]["processing"]["whitebox"]["toolbar"] = serde_json::json!({})).is_err()); + assert!(with(&|v| v["translations"]["processing"]["whitebox"]["categories"]["vector"] = 3.into()).is_err(), "strings only"); + assert!(with(&|v| v["translations"]["processing"] = serde_json::json!({})).is_err(), "empty"); + assert!(parse("[").is_err()); + assert!(parse(&" ".repeat(MAX_BYTES + 1)).is_err()); + assert!(valid_locale("zh-Hant") && valid_locale("pt") && !valid_locale("p") && !valid_locale("pt-")); + } + + #[test] + fn only_published_languages_have_an_address() { + assert_eq!(url("pt").as_deref(), Some("https://languages.geolibre.app/v1/whitebox/pt.json")); + assert_eq!(url("en"), None); + assert_eq!(url("zh-tw"), None); + } +} diff --git a/crates/geolibre/src/lib.rs b/crates/geolibre/src/lib.rs index eea15dc34a..74068935c9 100644 --- a/crates/geolibre/src/lib.rs +++ b/crates/geolibre/src/lib.rs @@ -25,6 +25,7 @@ #[cfg(not(target_arch = "wasm32"))] pub mod install; +pub mod langpack; #[cfg(not(target_arch = "wasm32"))] mod native; #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/i18n/mod.rs b/crates/ui-egui/src/i18n/mod.rs index 71076ba986..a53351141c 100644 --- a/crates/ui-egui/src/i18n/mod.rs +++ b/crates/ui-egui/src/i18n/mod.rs @@ -403,9 +403,11 @@ thread_local! { /// The same, in logical order, for messages whose placeholders are filled /// ([`fmt`]) or whose segments are joined ([`location`]) before they are shown. static PLUGIN_RAW_STRINGS: std::cell::RefCell> = Default::default(); - /// Plugin lookups this frame and the ones before, misses included: (plugin revision, language, - /// logical and display results). `tr` runs for every label every frame. - static PLUGIN_CACHE_MAP: std::cell::RefCell<(u64, &'static str, std::collections::HashMap>)> = + /// Plugin lookups this frame and the ones before, misses included: (plugin and GeoLibre + /// translation revisions, language, logical and display results). `tr` runs for every label + /// every frame. + #[allow(clippy::type_complexity)] + static PLUGIN_CACHE_MAP: std::cell::RefCell<((u64, u64), &'static str, std::collections::HashMap>)> = Default::default(); } @@ -420,9 +422,10 @@ fn leak_once(seen: &mut std::collections::HashSet<&'static str>, s: String) -> & kept } -/// `s` translated by a loaded plugin's `i18n`, in logical order and in display form. +/// `s` translated by a loaded plugin's `i18n`, else by the GeoLibre tool translations kept for +/// `lang` (#2224), in logical order and in display form. fn plugin_tr_pair(lang: Lang, s: &str) -> Option<(&'static str, &'static str)> { - let revision = rustgis_engine::plugins::revision(); + let revision = (rustgis_engine::plugins::revision(), rustgis_engine::cmd::gp::geolibre_language_revision()); PLUGIN_CACHE_MAP.with_borrow_mut(|(rev, code, map)| { if *rev != revision || *code != lang.code() || map.len() > PLUGIN_CACHE { *rev = revision; @@ -432,7 +435,9 @@ fn plugin_tr_pair(lang: Lang, s: &str) -> Option<(&'static str, &'static str)> { if let Some(hit) = map.get(s) { return *hit; } - let found = rustgis_engine::plugins::translate(lang.code(), s).map(|t| { + let translated = + rustgis_engine::plugins::translate(lang.code(), s).or_else(|| rustgis_engine::cmd::gp::geolibre_translate(lang.code(), s)); + let found = translated.map(|t| { let shown = rtl::visual(&t); let raw = PLUGIN_RAW_STRINGS.with_borrow_mut(|seen| leak_once(seen, t)); let vis = PLUGIN_STRINGS.with_borrow_mut(|seen| leak_once(seen, shown)); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 49d469ff29..ac6a764e35 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2805,6 +2805,13 @@ pub struct GpForm { pub confirm_uninstall: bool, /// Why the last uninstall failed (apart from `last`, which a tool run sets too). pub uninstall_error: Option, + /// GeoLibre tool translations downloading (#2224): the language, and the tools they name + /// (or why they couldn't be had) once the download is done. + #[cfg(not(target_arch = "wasm32"))] + pub language_download: Option<(&'static str, crate::Slot>)>, + /// Why the last download or removal of tool translations failed. + #[cfg(not(target_arch = "wasm32"))] + pub language_error: Option, } /// A plugin tool running on a thread of its own. @@ -3381,6 +3388,61 @@ fn geolibre_uninstall(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } +/// The GeoLibre tools in the interface's language (#2224): **Translate tools** downloads +/// GeoLibre's pack for it, on a thread (one request of about 2 MB); **Remove translations** +/// deletes the kept one. Nothing for English, or a language GeoLibre has no pack for. +#[cfg(not(target_arch = "wasm32"))] +fn geolibre_language(app: &mut RustGisApp, ui: &mut Ui) { + let lang = crate::i18n::current().code(); + if let Some((for_lang, slot)) = &app.gp.language_download { + let done = slot.lock().ok().and_then(|mut r| r.take()); + match done { + Some(Ok(_)) => { + app.gp.language_download = None; + app.gp.language_error = None; + } + Some(Err(e)) => { + app.gp.language_download = None; + app.gp.language_error = Some(e); + } + None if *for_lang == lang => { + ui.spinner().on_hover_text(tl!("Downloading the tool translations…")); + return; + } + None => {} + } + } + if !rustgis_engine::cmd::gp::geolibre_language_available(lang) { + return; + } + if rustgis_engine::cmd::gp::geolibre_language_in_use(lang) { + let resp = ui + .small_button(egui::RichText::new(tl!("Remove translations")).size(11.0)) + .on_hover_text(tl!("Show these tools in English again")); + if resp.clicked() { + app.gp.language_error = app.run("gp.uninstallGeolibreLanguage", json!({"locale": lang})).err(); + } + return; + } + let tip = crate::i18n::fmt( + "Download GeoLibre's translations of these tools into {language} (about 2 MB, from languages.geolibre.app)", + &[("language", crate::i18n::current().name())], + ); + if ui.small_button(egui::RichText::new(tl!("Translate tools")).size(11.0)).on_hover_text(tip).clicked() { + let slot: crate::Slot> = Default::default(); + app.gp.language_download = Some((lang, slot.clone())); + app.gp.language_error = None; + let ctx = ui.ctx().clone(); + std::thread::spawn(move || { + let result = rustgis_engine::cmd::gp::install_geolibre_language(lang); + if let Ok(mut s) = slot.lock() { + *s = Some(result); + } + ctx.request_repaint(); + }); + } +} + /// The browser's version of the card: load the WASI runner's tools. fn geolibre_web_card(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { ui.add_space(6.0); @@ -3548,6 +3610,22 @@ pub(crate) fn input_table<'a>(session: &'a Session, name: &str) -> Option<&'a ru rustgis_engine::project::find_table(tables, name).and_then(|i| tables.get(i)) } +/// Whether Find Tools shows `tool` for `q`, the search as [`TreeNode::fold`] folds it (lower case, +/// no accents): its name or summary as shown (translated) or in English, or its id, holds it. +/// `curvas de nivel` finds `Curvas de nível a partir de raster`, `dissolver` the built-in +/// Dissolve shown as `Dissolver` (#2224). +/// +/// [`TreeNode::fold`]: rustgis_engine::plugins::TreeNode::fold +fn tool_matches(q: &str, tool: &rustgis_engine::analysis::ToolSpec) -> bool { + let fold = rustgis_engine::plugins::TreeNode::fold; + q.is_empty() + || tool.id.to_lowercase().contains(q) + || [tool.name, tool.summary].into_iter().any(|en| { + let shown = crate::i18n::t_raw(en); + fold(en).contains(q) || (shown != en && fold(shown).contains(q)) + }) +} + fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { let Some(spec) = app.gp.tool.and_then(rustgis_engine::analysis::tool) else { // Tool finder, under the tool running, if one is. @@ -3561,10 +3639,8 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { crate::widgets::search_field(ui, &mut app.gp.search, tl!("Find Tools")); ui.separator(); ui.add_space(4.0); - let q = app.gp.search.to_lowercase(); - let matches = |x: &rustgis_engine::analysis::ToolSpec| { - q.is_empty() || x.name.to_lowercase().contains(&q) || x.summary.to_lowercase().contains(&q) || x.id.to_lowercase().contains(&q) - }; + let q = rustgis_engine::plugins::TreeNode::fold(&app.gp.search); + let matches = |x: &rustgis_engine::analysis::ToolSpec| tool_matches(&q, x); let row = |app: &mut RustGisApp, ui: &mut Ui, tool: &'static rustgis_engine::analysis::ToolSpec| { let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 42.0), Sense::click()); resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!(tool.name))); @@ -3600,11 +3676,19 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { if q.is_empty() { ui.horizontal(|ui| { ui.label(egui::RichText::new(tl!("GeoLibre")).size(11.0).color(t.text_dim)); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| geolibre_uninstall(app, ui, t)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + geolibre_uninstall(app, ui, t); + #[cfg(not(target_arch = "wasm32"))] + geolibre_language(app, ui); + }); }); if let Some(e) = &app.gp.uninstall_error { ui.label(egui::RichText::new(e).size(11.0).color(t.red)); } + #[cfg(not(target_arch = "wasm32"))] + if let Some(e) = &app.gp.language_error { + ui.label(egui::RichText::new(e).size(11.0).color(t.red)); + } geolibre_passed_over(app, ui, t, false); // As in GeoLibre's Processing menu: category › subcategory › tools // (`GeoLibre › Terrain › Derivatives`; a tool without a subcategory sits @@ -3763,7 +3847,7 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } let label = ui.label(egui::RichText::new(format!("{}{}", tl!(p.label), if p.required { " *" } else { "" })).size(12.0)); if !p.help.is_empty() { - label.on_hover_text(p.help); + label.on_hover_text(tl!(p.help)); } let mut cur = app.gp.values.get(p.name).cloned().unwrap_or_default(); if let Some(input) = crate::fieldlists::input(p.kind) { @@ -7199,6 +7283,30 @@ pub(crate) fn layout_menu(app: &mut RustGisApp, ui: &mut Ui, n: &str) { mod tests { use super::*; + /// Find Tools finds a tool by what it shows in the interface's language as well as by its + /// English name and id, without minding case or accents (#2224). + #[test] + fn find_tools_matches_the_shown_name_without_accents() { + let dissolve = rustgis_engine::analysis::tool("dissolve").expect("the built-in Dissolve"); + let kernel = rustgis_engine::analysis::tool("kernelDensity").expect("the built-in Kernel Density"); + let fold = rustgis_engine::plugins::TreeNode::fold; + crate::i18n::set_current(crate::i18n::Lang::from_code("pt").expect("Portuguese")); + let shown = crate::i18n::t_raw(dissolve.name); + assert_ne!(shown, dissolve.name, "the test needs Dissolve translated into Portuguese"); + assert!(tool_matches(&fold(shown), dissolve), "{shown}"); + assert!(tool_matches(&fold("DISSOLVE"), dissolve), "the English name still finds it"); + assert!(tool_matches(&fold("dissolve"), dissolve) && tool_matches("", dissolve)); + let shown = crate::i18n::t_raw(kernel.name); + let plain: String = fold(shown); + assert!(tool_matches(&plain, kernel), "{shown} without its accents or case"); + assert!(!tool_matches(&fold("curvas de nivel"), dissolve)); + crate::i18n::set_current(crate::i18n::Lang::EN); + assert!(!tool_matches( + &fold(crate::i18n::Lang::from_code("pt").map(|pt| crate::i18n::tr_raw(pt, dissolve.name)).unwrap_or("")), + dissolve + )); + } + /// An external tool's optional layer inputs stay empty until they're picked, and its required /// ones start at the active layer; a built-in tool's optional one still follows it (#1517). #[cfg(not(target_arch = "wasm32"))] diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 1db2b4b912..92f5c6c045 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1323,6 +1323,22 @@ you located yourself, and one on your `PATH` are left alone; RustGIS looks for a You can install the tools again at any time. In the browser, **Remove** unloads the tools and stops loading them when RustGIS starts. +**In your language.** The tools' names, descriptions, parameters and toolboxes come in English. +GeoLibre translates them into some twenty languages, in +[language packs](https://github.com/opengeos/geolibre-language-packs) of about 2 MB each that it +keeps out of its builds, and RustGIS reads the same packs. With the interface in a language GeoLibre +has a pack for, click **Translate tools** beside *GeoLibre* at the top of the Tools pane: RustGIS +downloads that language's pack from `languages.geolibre.app`, checks it as GeoLibre does (its format +and version, text only, 5 MB at most) and keeps it in a `geolibre-languages` folder in its data +folder, so it works offline from then on, and in later sessions, whenever the interface is in that +language. **Remove translations** deletes it. Find Tools then finds a tool by its translated name as +well as its English one, with or without accents (`curvas de nivel` finds *Curvas de nível a partir +de raster*). Only what the tools show is translated: scripts, the command line and the layers a +tool adds keep the English names. From the command line, `gp.installGeolibreLanguage` +(`{"locale": "pt"}`) downloads a pack, `gp.importGeolibreLanguage` (`{"path": …}`) reads one from a +file (one you made or edited, in the same format), `gp.uninstallGeolibreLanguage` deletes one, and +`gp.geolibre` lists them under `languages`. The browser shows the tools in English. + To install it yourself instead, download the archive for your platform (`geolibre--.tar.gz`, or `.zip` on Windows), unpack it anywhere, and click **Locate…** to pick the `geolibre` program; RustGIS remembers it. From 297f64092ab0f6a84f7fc726d20f260a7600a54c Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sun, 15 Nov 2026 20:54:01 -0300 Subject: [PATCH 2/4] fix(gp): review round 1 for the GeoLibre language packs (#2224) - Bound the pack read itself (take(MAX_BYTES + 1)), not its reported size: a pipe or /dev/zero reports none. - Say a kept pack that can't be read: Tools pane and gp.geolibre languages.errors. - Report how many of the listed tools a pack names. - Start the download and the runner install with thread::Builder and show a refused thread instead of panicking. - Document why gp.installGeolibreLanguage waits on its one request, and the first-wins rule of the English-keyed lookup (1 of 7,068 strings in pt). - Test a kept pack's life: imported, read back, damaged and said, removed. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 29 +++++++-- crates/geolibre/src/langpack.rs | 100 +++++++++++++++++++++++++++++--- crates/ui-egui/src/panes.rs | 15 ++++- 3 files changed, 129 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 1cf6c88ae7..1c39c5ada5 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -156,12 +156,14 @@ pub fn geolibre_passed_over() -> Option { fn geolibre_languages() -> Value { #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] { + let errors: serde_json::Map = + rustgis_geolibre::langpack::load_errors().into_iter().map(|(locale, e)| (locale, Value::String(e))).collect(); json!({"available": rustgis_geolibre::langpack::LOCALES, "installed": rustgis_geolibre::langpack::kept(), - "active": rustgis_geolibre::langpack::active_locale()}) + "active": rustgis_geolibre::langpack::active_locale(), "errors": errors}) } #[cfg(not(all(feature = "geolibre", not(target_arch = "wasm32"))))] { - json!({"available": [], "installed": [], "active": null}) + json!({"available": [], "installed": [], "active": null, "errors": {}}) } } @@ -207,6 +209,20 @@ pub fn geolibre_translate(locale: &str, english: &str) -> Option { } } +/// Why the tool translations kept for `locale` can't be used (a damaged file, another format +/// version), once they were tried: the Tools pane shows it beside **Translate tools**. +pub fn geolibre_language_error(locale: &str) -> Option { + #[cfg(feature = "geolibre")] + { + rustgis_geolibre::langpack::load_error(locale) + } + #[cfg(not(feature = "geolibre"))] + { + let _ = locale; + None + } +} + /// Changes whenever [`geolibre_translate`] may answer differently (translations installed or /// removed, the tools reloaded), so a cache of its answers starts over. pub fn geolibre_language_revision() -> u64 { @@ -219,7 +235,7 @@ pub fn geolibre_language_revision() -> u64 { } /// Downloads GeoLibre's tool translations for `locale` (about 2 MB), keeps them and puts them in -/// use; returns how many tools they name. Blocks on one request: the app calls it from a +/// use; returns how many of the listed tools they name. Blocks on one request: the app calls it from a /// background thread. Native only. pub fn install_geolibre_language(locale: &str) -> Result { #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] @@ -233,8 +249,11 @@ pub fn install_geolibre_language(locale: &str) -> Result { } } -/// `gp.installGeolibreLanguage`: one request of about 2 MB, which gives up after a minute, run -/// where it is called (like `gp.installGeolibre`); the app's button runs it on a thread. +/// `gp.installGeolibreLanguage`. It runs where it is called, without Cancel, because its work +/// doesn't grow with anything the user has: one request for one file, read up to 5 MB and given +/// up after 60 s, then one parse, the same for every project. The Tools pane's button, the way +/// the app downloads a pack, runs it on a thread of its own; called from a script, the command line +/// or Python, it waits for that one request like `gp.installGeolibre` waits for the runner's. fn install_geolibre_language_cmd(_s: &mut Session, p: &Value) -> CmdResult { let locale = ps(p, "locale").map(str::trim).filter(|l| !l.is_empty()).ok_or("give the language's \"locale\", such as \"pt\"")?; let tools = install_geolibre_language(locale)?; diff --git a/crates/geolibre/src/langpack.rs b/crates/geolibre/src/langpack.rs index 9833fee9ea..7db5fbbc81 100644 --- a/crates/geolibre/src/langpack.rs +++ b/crates/geolibre/src/langpack.rs @@ -12,7 +12,10 @@ //! [`translate`] maps the English text the tools show (the [`ToolSpec`] strings) to the pack's, //! by tool id and parameter name, so the interface's catalogue lookup can fall back to it like //! a plugin's `i18n`. The English names stay the tools' names everywhere else (scripts, the -//! command line, layer names, search by the English words). +//! command line, layer names, search by the English words). Because the lookup is keyed by the +//! English text, two tools that share an English string get one translation of it: the first +//! tool's, in the tool list's order. The packs rarely disagree on one: the Portuguese pack has two +//! translations for one of its 7,068 English strings (`Ripley's K Function`). use std::collections::{BTreeMap, HashMap, HashSet}; use std::sync::Mutex; @@ -273,6 +276,22 @@ static REVISION: AtomicU64 = AtomicU64::new(0); /// Languages whose pack was looked for in the data folder and isn't there. static MISSING: Mutex>> = Mutex::new(None); +/// Why the pack kept for a language couldn't be read, by language. +static LOAD_ERRORS: Mutex>> = Mutex::new(None); + +/// Why the pack kept for `locale` can't be used (a damaged file, another format version), if +/// it was tried and failed: the Tools pane and `gp.geolibre` say it. +pub fn load_error(locale: &str) -> Option { + LOAD_ERRORS.lock().ok()?.as_ref()?.get(locale).cloned() +} + +/// Every language whose kept pack couldn't be read, with the reason. +pub fn load_errors() -> Vec<(String, String)> { + let mut v: Vec<_> = LOAD_ERRORS.lock().ok().and_then(|e| e.clone()).unwrap_or_default().into_iter().collect(); + v.sort(); + v +} + fn published() -> &'static [ToolSpec] { rustgis_analysis::published_external_tools_of(crate::SOURCE) } @@ -292,6 +311,9 @@ pub fn activate(pack: Option) { if let Ok(mut m) = MISSING.lock() { *m = None; } + if let Ok(mut e) = LOAD_ERRORS.lock() { + *e = None; + } REVISION.fetch_add(1, Ordering::Relaxed); } @@ -353,6 +375,9 @@ fn load_kept(locale: &str) { if let Ok(mut m) = MISSING.lock() { m.get_or_insert_with(Default::default).insert(locale.to_string()); } + if let Ok(mut errors) = LOAD_ERRORS.lock() { + errors.get_or_insert_with(Default::default).insert(locale.to_string(), e); + } } } } @@ -361,6 +386,10 @@ fn load_kept(locale: &str) { /// `.json` each. #[cfg(not(target_arch = "wasm32"))] pub fn dir() -> Option { + #[cfg(test)] + if let Some(d) = tests::DIR.lock().ok().and_then(|d| d.clone()) { + return Some(d); + } rustgis_data::dirs::app_data_dir().map(|d| d.join("geolibre-languages")) } @@ -401,13 +430,25 @@ fn load(locale: &str) -> Result, String> { Ok(Some(pack)) } +/// Reads at most [`MAX_BYTES`] of `path` (one byte more tells a larger file), whatever its size +/// says: a pipe or a device (`/dev/zero`) reports none. #[cfg(not(target_arch = "wasm32"))] fn read_limited(path: &std::path::Path) -> Result { - let len = std::fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?.len(); - if len > MAX_BYTES as u64 { + use std::io::Read; + let err = |e: std::io::Error| format!("{}: {e}", path.display()); + let mut bytes = Vec::new(); + std::fs::File::open(path).map_err(err)?.take(MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map_err(err)?; + if bytes.len() > MAX_BYTES { return Err(format!("the language pack is larger than {} MB", MAX_BYTES / (1024 * 1024))); } - std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display())) + String::from_utf8(bytes).map_err(|_| format!("{}: the language pack isn't UTF-8 text", path.display())) +} + +/// How many of the listed GeoLibre tools `pack` names: what a download or an import reports, so +/// a pack made for another runner's tools shows as translating few. +#[cfg(not(target_arch = "wasm32"))] +fn matched(pack: &Pack) -> usize { + published().iter().filter(|t| pack.tools.get(t.id.trim_start_matches(crate::PREFIX)).is_some_and(|x| x.name.is_some())).count() } /// Keeps `text`, a checked pack for `locale`, and puts it in use. @@ -418,13 +459,13 @@ fn keep(pack: Pack, text: &str) -> Result { std::fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?; } rustgis_data::io::write_atomic(&path, text.as_bytes()).map_err(|e| format!("{}: {e}", path.display()))?; - let n = pack.tools(); + let n = matched(&pack); activate(Some(pack)); Ok(n) } -/// Downloads GeoLibre's pack for `locale`, keeps it and puts it in use; returns how many tools -/// it names. One request of about 2 MB, which gives up after a minute. +/// Downloads GeoLibre's pack for `locale`, keeps it and puts it in use; returns how many of the +/// listed tools it names. One request of about 2 MB, which gives up after a minute. #[cfg(not(target_arch = "wasm32"))] pub fn download(locale: &str) -> Result { let url = url(locale).ok_or_else(|| format!("GeoLibre publishes no tool translations for '{locale}'"))?; @@ -453,7 +494,7 @@ pub fn download(locale: &str) -> Result { } /// Reads a pack from `path`, keeps it for the language it names and puts it in use; returns -/// that language and how many tools it names. +/// that language and how many of the listed tools it names. #[cfg(not(target_arch = "wasm32"))] pub fn import(path: &std::path::Path) -> Result<(String, usize), String> { let text = read_limited(path)?; @@ -473,6 +514,11 @@ pub fn remove(locale: &str) -> Result<(), String> { if active_locale().as_deref() == Some(locale) { activate(None); } + if let Ok(mut errors) = LOAD_ERRORS.lock() + && let Some(errors) = errors.as_mut() + { + errors.remove(locale); + } Ok(()) } @@ -481,6 +527,9 @@ mod tests { use super::*; use rustgis_analysis::{ParamKind, ParamSpec}; + /// Where [`dir`] keeps packs in these tests, instead of the app's data folder. + pub(super) static DIR: Mutex> = Mutex::new(None); + fn pack_text(locale: &str) -> String { serde_json::json!({ "format": "geolibre-language-pack", "formatVersion": 1, "scope": "whitebox", "locale": locale, @@ -585,6 +634,41 @@ mod tests { assert!(valid_locale("zh-Hant") && valid_locale("pt") && !valid_locale("p") && !valid_locale("pt-")); } + /// A pack kept, read again in a later session, damaged (and said so), then removed. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_kept_pack_is_read_back_said_when_damaged_and_removed() { + let dir = std::env::temp_dir().join(format!("rustgis-langpack-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + *DIR.lock().unwrap() = Some(dir.clone()); + let src = dir.with_extension("json"); + std::fs::write(&src, pack_text("pt")).unwrap(); + let (locale, _) = import(&src).unwrap(); + assert_eq!(locale, "pt"); + assert!(dir.join("pt.json").is_file() && kept() == ["pt"]); + assert!(in_use("pt")); + // A later session: nothing in use, the kept file read the first time it's asked for. + let before = revision(); + activate(None); + assert!(revision() > before, "a change of pack starts a cache over"); + assert!(in_use("pt") && load_error("pt").is_none()); + // Damaged: said, and not in use. + activate(None); + std::fs::write(dir.join("pt.json"), "{").unwrap(); + assert!(!in_use("pt")); + assert!(load_error("pt").is_some_and(|e| e.contains("JSON")), "{:?}", load_error("pt")); + assert_eq!(load_errors().len(), 1); + remove("pt").unwrap(); + assert!(kept().is_empty() && load_error("pt").is_none() && !in_use("pt")); + assert!(remove("pt").is_err(), "nothing left to remove"); + // A pipe or a device reports no size: the read is bounded all the same. + #[cfg(unix)] + assert!(import(std::path::Path::new("/dev/zero")).is_err()); + *DIR.lock().unwrap() = None; + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_file(&src); + } + #[test] fn only_published_languages_have_an_address() { assert_eq!(url("pt").as_deref(), Some("https://languages.geolibre.app/v1/whitebox/pt.json")); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index ac6a764e35..436962397a 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -3281,7 +3281,7 @@ fn geolibre_card(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { app.gp.install = Some(st.clone()); app.gp.last = None; let ctx = ui.ctx().clone(); - std::thread::spawn(move || { + let spawned = std::thread::Builder::new().name("geolibre-install".into()).spawn(move || { let result = rustgis_engine::cmd::gp::install_geolibre(&|done, total| { if let Ok(mut s) = st.lock() { s.done = done; @@ -3293,6 +3293,10 @@ fn geolibre_card(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } ctx.request_repaint(); }); + if let Err(e) = spawned { + app.gp.install = None; + app.gp.last = Some((false, vec![e.to_string()])); + } } ui.add_space(2.0); ui.horizontal_wrapped(|ui| { @@ -3433,13 +3437,17 @@ fn geolibre_language(app: &mut RustGisApp, ui: &mut Ui) { app.gp.language_download = Some((lang, slot.clone())); app.gp.language_error = None; let ctx = ui.ctx().clone(); - std::thread::spawn(move || { + let spawned = std::thread::Builder::new().name("geolibre-language".into()).spawn(move || { let result = rustgis_engine::cmd::gp::install_geolibre_language(lang); if let Ok(mut s) = slot.lock() { *s = Some(result); } ctx.request_repaint(); }); + if let Err(e) = spawned { + app.gp.language_download = None; + app.gp.language_error = Some(e.to_string()); + } } } @@ -3688,6 +3696,9 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { #[cfg(not(target_arch = "wasm32"))] if let Some(e) = &app.gp.language_error { ui.label(egui::RichText::new(e).size(11.0).color(t.red)); + } else if let Some(e) = rustgis_engine::cmd::gp::geolibre_language_error(crate::i18n::current().code()) { + let e = crate::i18n::fmt("The kept tool translations can't be read: {error}", &[("error", &e)]); + ui.label(egui::RichText::new(e).size(11.0).color(t.red)); } geolibre_passed_over(app, ui, t, false); // As in GeoLibre's Processing menu: category › subcategory › tools From cd7c24b21a5d481cb15c98781c16b48510bcb59f Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 16 Nov 2026 01:09:51 -0300 Subject: [PATCH 3/4] fix(gp): confirmed review findings for the GeoLibre language packs (#2224) - One tool-translation download at a time: a second one, for another language, replaced the first's slot and its result was never shown. - Say when a downloaded pack names none of the listed tools. - Hold one lock across reading a kept pack and putting it in use, and across keeping a new one and putting it in use, so an older read can't replace a newer download. - The plugin off-limits test passes a valid locale and checks the policy's refusal for the two language commands. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests.rs | 18 +++++++----------- crates/geolibre/src/langpack.rs | 19 +++++++++++++++++-- crates/ui-egui/src/panes.rs | 15 ++++++++++++--- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 34ee01ec19..265c917e61 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -14348,19 +14348,15 @@ fn plugin_edits_are_one_undo_step() { assert!(forward(&mut s, "eng-edit", "layer.remove", json!({"layer": "Nope"})).is_err()); assert_eq!(s.map().layers.len(), n); // Some things are off limits whatever the permissions. - for op in [ - "script.run", - "project.new", - "edit.undo", - "plugin.uninstall", - "plugin:eng-edit/h", - "gp.setGeolibre", - "gp.uninstallGeolibre", - "gp.installGeolibreLanguage", - "gp.uninstallGeolibreLanguage", - ] { + for op in ["script.run", "project.new", "edit.undo", "plugin.uninstall", "plugin:eng-edit/h", "gp.setGeolibre", "gp.uninstallGeolibre"] + { assert!(forward(&mut s, "eng-edit", op, json!({})).is_err(), "{op}"); } + // With a valid locale, so the refusal is the policy's, not a missing parameter. + for op in ["gp.installGeolibreLanguage", "gp.uninstallGeolibreLanguage"] { + let e = forward(&mut s, "eng-edit", op, json!({"locale": "pt"})).unwrap_err(); + assert!(e.contains("reserved actions"), "{op}: {e}"); + } // Files need their own permission. let e = forward(&mut s, "eng-edit", "layer.addData", json!({"path": "/etc/passwd"})).unwrap_err(); assert!(e.contains("'files' permission"), "{e}"); diff --git a/crates/geolibre/src/langpack.rs b/crates/geolibre/src/langpack.rs index 7db5fbbc81..8a82a3acd2 100644 --- a/crates/geolibre/src/langpack.rs +++ b/crates/geolibre/src/langpack.rs @@ -276,6 +276,12 @@ static REVISION: AtomicU64 = AtomicU64::new(0); /// Languages whose pack was looked for in the data folder and isn't there. static MISSING: Mutex>> = Mutex::new(None); +/// Held across reading a kept pack and putting it in use, and across keeping a new one and putting +/// it in use, so a read that started before a download or an import kept a newer pack can't put +/// the older one back in use after it. +#[cfg(not(target_arch = "wasm32"))] +static LIFECYCLE: Mutex<()> = Mutex::new(()); + /// Why the pack kept for a language couldn't be read, by language. static LOAD_ERRORS: Mutex>> = Mutex::new(None); @@ -360,7 +366,15 @@ pub fn translate(locale: &str, english: &str) -> Option { /// and wasn't there. One file read, of about 2 MB, once per language switch. #[cfg(not(target_arch = "wasm32"))] fn load_kept(locale: &str) { - if active_locale().as_deref() == Some(locale) || MISSING.lock().ok().is_some_and(|m| m.as_ref().is_some_and(|m| m.contains(locale))) { + let settled = || { + active_locale().as_deref() == Some(locale) || MISSING.lock().ok().is_some_and(|m| m.as_ref().is_some_and(|m| m.contains(locale))) + }; + if settled() { + return; + } + let Ok(_lifecycle) = LIFECYCLE.lock() else { return }; + // A download or an import may have kept and put in use a pack for it while this waited. + if settled() { return; } match load(locale) { @@ -458,8 +472,9 @@ fn keep(pack: Pack, text: &str) -> Result { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?; } - rustgis_data::io::write_atomic(&path, text.as_bytes()).map_err(|e| format!("{}: {e}", path.display()))?; let n = matched(&pack); + let _lifecycle = LIFECYCLE.lock().map_err(|_| "the tool translations are in an unusable state".to_string())?; + rustgis_data::io::write_atomic(&path, text.as_bytes()).map_err(|e| format!("{}: {e}", path.display()))?; activate(Some(pack)); Ok(n) } diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 436962397a..9ad83c80c5 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -3398,9 +3398,19 @@ fn geolibre_uninstall(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { #[cfg(not(target_arch = "wasm32"))] fn geolibre_language(app: &mut RustGisApp, ui: &mut Ui) { let lang = crate::i18n::current().code(); - if let Some((for_lang, slot)) = &app.gp.language_download { + // One download at a time, whatever the language now: a second one would take the first's + // place, and the first's result (or failure) would never be shown. + if let Some((_, slot)) = &app.gp.language_download { let done = slot.lock().ok().and_then(|mut r| r.take()); match done { + // A pack that names none of these tools (made for another runner version) would leave + // them in English under Remove translations, without a word. + Some(Ok(0)) => { + app.gp.language_download = None; + app.gp.language_error = Some( + tl!("The downloaded translations name none of these tools: they may be for another GeoLibre version.").to_string(), + ); + } Some(Ok(_)) => { app.gp.language_download = None; app.gp.language_error = None; @@ -3409,11 +3419,10 @@ fn geolibre_language(app: &mut RustGisApp, ui: &mut Ui) { app.gp.language_download = None; app.gp.language_error = Some(e); } - None if *for_lang == lang => { + None => { ui.spinner().on_hover_text(tl!("Downloading the tool translations…")); return; } - None => {} } } if !rustgis_engine::cmd::gp::geolibre_language_available(lang) { From c4760b52189918e69efb41b1c86965c31ff06239 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 16 Nov 2026 11:22:31 -0300 Subject: [PATCH 4/4] fix(gp): find GeoLibre's zh-TW tool translations for Traditional Chinese (#2224) GeoLibre now publishes zh-TW (opengeos/geolibre-language-packs#6). RustGIS's Traditional Chinese is zh-tw: list it, ask for zh-TW.json (the address is case-sensitive) and read a pack's language code in lower case. Co-Authored-By: Claude Opus 5.5 --- crates/geolibre/src/langpack.rs | 35 +++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/geolibre/src/langpack.rs b/crates/geolibre/src/langpack.rs index 8a82a3acd2..145b19dc48 100644 --- a/crates/geolibre/src/langpack.rs +++ b/crates/geolibre/src/langpack.rs @@ -34,13 +34,22 @@ pub const MAX_BYTES: usize = 5 * 1024 * 1024; const MAX_LEAVES: usize = 100_000; /// The languages GeoLibre publishes a pack for (its `v1/index.json`, the English template left -/// out), by the codes RustGIS's languages use. +/// out, and `zh-HK`/`zh-MO`, which RustGIS has no language for), by the codes RustGIS's languages +/// use: lower case (`zh-tw` for GeoLibre's `zh-TW`). pub const LOCALES: &[&str] = - &["ar", "az", "de", "es", "fa", "fr", "he", "hi", "id", "it", "ja", "ka", "ko", "nl", "pt", "ru", "th", "tr", "vi", "zh"]; + &["ar", "az", "de", "es", "fa", "fr", "he", "hi", "id", "it", "ja", "ka", "ko", "nl", "pt", "ru", "th", "tr", "vi", "zh", "zh-tw"]; -/// The address of the published pack for `locale`, if GeoLibre publishes one. +/// The address of the published pack for `locale`, if GeoLibre publishes one. GeoLibre names a +/// pack by its code with the region in capitals (`zh-TW.json`), and the address is case-sensitive. pub fn url(locale: &str) -> Option { - LOCALES.contains(&locale).then(|| format!("{BASE_URL}/v1/whitebox/{locale}.json")) + LOCALES.contains(&locale).then(|| { + let published: Vec = locale + .split('-') + .enumerate() + .map(|(i, part)| if i > 0 && part.len() == 2 { part.to_ascii_uppercase() } else { part.to_string() }) + .collect(); + format!("{BASE_URL}/v1/whitebox/{}.json", published.join("-")) + }) } /// One tool's strings in a pack. @@ -55,7 +64,8 @@ struct ToolText { /// A language pack, checked. #[derive(Debug)] pub struct Pack { - /// The language it translates into (`pt`, `zh`…). + /// The language it translates into, as RustGIS writes it: lower case (`pt`, `zh-tw` for a + /// `zh-TW` pack). pub locale: String, /// By tool id (without [`crate::PREFIX`]). tools: HashMap, @@ -133,8 +143,13 @@ pub fn parse(text: &str) -> Result { if root.get("formatVersion").and_then(Value::as_u64) != Some(1) { return Err(format!("language pack format {} isn't supported (only 1)", root.get("formatVersion").unwrap_or(&Value::Null))); } - let locale = - root.get("locale").and_then(Value::as_str).filter(|l| valid_locale(l)).ok_or("the language pack has no valid language code")?; + // Language codes ignore case; RustGIS's are lower case, so a `zh-TW` pack serves `zh-tw`. + let locale = root + .get("locale") + .and_then(Value::as_str) + .filter(|l| valid_locale(l)) + .map(str::to_ascii_lowercase) + .ok_or("the language pack has no valid language code")?; let translations = object_of( root.get("translations").unwrap_or(&Value::Null), &["processing"], @@ -688,6 +703,10 @@ mod tests { fn only_published_languages_have_an_address() { assert_eq!(url("pt").as_deref(), Some("https://languages.geolibre.app/v1/whitebox/pt.json")); assert_eq!(url("en"), None); - assert_eq!(url("zh-tw"), None); + assert_eq!(url("zh-tw").as_deref(), Some("https://languages.geolibre.app/v1/whitebox/zh-TW.json")); + assert_eq!(url("zh-hk"), None); + assert_eq!(url("zh-TW"), None, "RustGIS's own codes only"); + // GeoLibre's `zh-TW` pack serves RustGIS's `zh-tw`. + assert_eq!(parse(&pack_text("zh-TW")).unwrap().locale, "zh-tw"); } }