From c1af8dcf8331316f7172b105480b14da1a9dda89 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 30 Nov 2026 05:10:56 +0100 Subject: [PATCH] i18n: load the shared Construction Geometry label (#3414) * Load Construction Geometry from the shared Chinese catalog * Format the shared Chinese catalog validation --------- Co-authored-by: FredApps --- ATTRIBUTION.md | 1 + craft-i18n.rev | 1 + crates/ui-egui/build.rs | 52 ++++++++++++++++++++++++++- crates/ui-egui/src/i18n.rs | 18 +++++++++- crates/ui-egui/src/i18n/tests.rs | 29 +++++++++++++++ crates/ui-egui/src/i18n/zh_shared.tsv | 3 ++ 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 craft-i18n.rev create mode 100644 crates/ui-egui/src/i18n/zh_shared.tsv diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index aa67f69f6..eb4c72be9 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -25,6 +25,7 @@ Generated-in-code assets are original and have no file to list: | Asset | Author | Source | Licence | Notes | |---|---|---|---|---| +| `crates/ui-egui/src/i18n/zh_shared.tsv` | @V1dalTheAbidal | [craft-i18n #444](https://github.com/storytold/craft-i18n/pull/444), [pinned Simplified Chinese CADCraft catalog](https://github.com/storytold/craft-i18n/blob/9fa32aecf2759cd61bb216a958df68da619798f3/locales/zh-hans/cadcraft.tsv#L201) | MIT OR Apache-2.0 | Exact generated one-row projection for offline builds; AI-assisted, not native-speaker reviewed. | | `licenses/jpeg-encoder-0.6.1/` | jpeg-encoder contributors and Independent JPEG Group | [jpeg-encoder 0.6.1](https://crates.io/crates/jpeg-encoder/0.6.1) | (MIT OR Apache-2.0) AND IJG | Dependency license and SIMD notices; source hashes in SOURCE.json. | | `epaint_default_fonts::EGUI_ICONS`, `epaint_default_fonts::EMOJI_ICON` (dependency font inputs) | John Slegers | [egui 173cc737](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [emoji-icon-font](https://github.com/jslegers/emoji-icon-font) | MIT (Copyright (c) 2014 John Slegers; full upstream text in `NOTICE`) | egui-icons.ttf is the five-glyph subset; emoji-icon-font.ttf is the full monochrome fallback. Registered by the production default font definitions; dependency assets unmodified here, no font file copied into this repository. | | `krilla 0.8.2 / icc/sGrey-v2-magic.icc` (dependency-embedded) | Clinton Ingram | [craft-libs 9f89fe8 vendor/krilla/icc/sGrey-v2-magic.icc](https://github.com/storytold/craft-libs/blob/9f89fe8f6d72ab4db4cc06f186381a6a254463ab/vendor/krilla/icc/sGrey-v2-magic.icc) (Krilla 0.8.2 upstream 3ffdf0588cf98050aad6edba51ca70162e1fb5b5) | CC0-1.0 | Embedded by the pinned PDF writer (`craft-pdf` 0.1.0, Krilla 0.8.2); unmodified dependency asset, no file copied into this repository. SHA-256 `4110169287e5f1b5149ca9f1da1814d32943905bba8030e849d1a3b729a19481`. | diff --git a/craft-i18n.rev b/craft-i18n.rev new file mode 100644 index 000000000..174050625 --- /dev/null +++ b/craft-i18n.rev @@ -0,0 +1 @@ +9fa32aecf2759cd61bb216a958df68da619798f3 diff --git a/crates/ui-egui/build.rs b/crates/ui-egui/build.rs index 3116b01db..95e3a15ae 100644 --- a/crates/ui-egui/build.rs +++ b/crates/ui-egui/build.rs @@ -1,4 +1,4 @@ -//! Bake the contributor credits into the binary as static Rust tables. Nothing is read at run time. +//! Bake the contributor credits and validate the shared Chinese overlay at build time. //! //! - contributors/contributors.json: commit stats per GitHub username, with a mark for whose people.toml //! entry is verified. @@ -11,7 +11,11 @@ use std::fmt::Write as _; use serde_json::Value; +const SHARED_ZH: &str = "src/i18n/zh_shared.tsv"; +const SHARED_ZH_KEY: &str = "Construction Geometry"; + fn main() { + verify_shared_zh(); let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../contributors"); let json = read(&dir.join("contributors.json"), "the About window shows no contributors", |t| { serde_json::from_str::(t).map_err(|e| e.to_string()) @@ -25,6 +29,52 @@ fn main() { } } +// Local/offline builds use the checked-in projection. Hosted and release builds with +// craft-i18n.rev also check it against their canonical craft-i18n checkout. +fn verify_shared_zh() { + println!("cargo::rerun-if-env-changed=CRAFT_I18N_DIR"); + println!("cargo::rerun-if-env-changed=CRAFT_I18N_REQUIRED"); + println!("cargo::rerun-if-changed={SHARED_ZH}"); + let result = projected_row(include_str!("src/i18n/zh_shared.tsv")).and_then(verify_checkout); + if let Err(e) = result { + println!("cargo::error=shared Simplified Chinese catalog: {e}"); + } +} + +fn projected_row(projection: &str) -> Result<&str, String> { + let mut rows = projection.lines().filter(|line| !line.is_empty() && !line.starts_with('#')); + let Some(row) = rows.next() else { + return Err(format!("missing projected key {SHARED_ZH_KEY:?}")); + }; + let valid = row + .strip_prefix('\t') + .and_then(|rest| rest.split_once('\t')) + .is_some_and(|(key, value)| key == SHARED_ZH_KEY && !value.trim().is_empty() && !value.contains(['\t', '\\', '\r'])); + if rows.next().is_some() || !valid { + return Err(format!("expected exactly one ordinary literal row for {SHARED_ZH_KEY:?}")); + } + Ok(row) +} + +fn verify_checkout(projected: &str) -> Result<(), String> { + let required = std::env::var("CRAFT_I18N_REQUIRED").is_ok_and(|v| v.trim() == "1"); + let Some(dir) = std::env::var_os("CRAFT_I18N_DIR").filter(|dir| !dir.is_empty()) else { + return if required { Err("CRAFT_I18N_REQUIRED=1 but CRAFT_I18N_DIR is unset".to_string()) } else { Ok(()) }; + }; + let dir = std::path::PathBuf::from(dir); + if !dir.is_absolute() { + return Err("CRAFT_I18N_DIR must be an absolute path".to_string()); + } + let catalog = dir.join("locales/zh-hans/cadcraft.tsv"); + println!("cargo::rerun-if-changed={}", catalog.display()); + let source = std::fs::read_to_string(&catalog).map_err(|e| format!("reading {}: {e}", catalog.display()))?; + let mut rows = source.lines().filter(|row| row.strip_prefix('\t').is_some_and(|rest| rest.split('\t').next() == Some(SHARED_ZH_KEY))); + if rows.next() != Some(projected) || rows.next().is_some() { + return Err(format!("projected key {SHARED_ZH_KEY:?} is missing, duplicated or differs from the checked-out source")); + } + Ok(()) +} + fn read(path: &std::path::Path, fallback: &str, parse: impl Fn(&str) -> Result) -> Option { println!("cargo::rerun-if-changed={}", path.display()); let r = std::fs::read_to_string(path).map_err(|e| e.to_string()).and_then(|t| parse(&t)); diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index c08357995..c3ffc4631 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -352,8 +352,24 @@ fn parse(source: &str) -> (Catalog<'_>, Vec) { (entries, errors) } +fn merge_ordinary_zh<'a>(catalog: &mut Catalog<'a>, supplemental: &'a str) { + for ((context, key), value) in parse(supplemental).0 { + if context.is_empty() { + catalog.entry((context, key)).or_insert(value); + } + } +} + fn catalog(language: &str) -> Option<&'static Catalog<'static>> { - LANGUAGES.iter().find(|l| l.code == language && !l.source.is_empty()).map(|l| l.catalog.get_or_init(|| parse(l.source).0)) + LANGUAGES.iter().find(|l| l.code == language && !l.source.is_empty()).map(|l| { + l.catalog.get_or_init(|| { + let mut entries = parse(l.source).0; + if l.code == "zh" { + merge_ordinary_zh(&mut entries, include_str!("i18n/zh_shared.tsv")); + } + entries + }) + }) } pub fn tr<'a>(language: &str, source: &'a str) -> &'a str { diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index b0c684235..e83ee1574 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -40,6 +40,35 @@ fn catalog_is_valid_and_preserves_placeholders_and_ellipses() { assert_eq!(tr("unknown", "Open"), "Open"); } +#[test] +fn chinese_supplement_fills_only_missing_ordinary_labels() { + let (mut entries, _) = parse("\tExisting label\tlocal\nmenu\tExisting label\tlocal menu\n"); + merge_ordinary_zh(&mut entries, "\tExisting label\tshared\n\tMissing label\tshared\nmenu\tIgnored label\tshared menu\n"); + assert_eq!(entries.get(&("", "Existing label")).copied(), Some("local")); + assert_eq!(entries.get(&("menu", "Existing label")).copied(), Some("local menu")); + assert_eq!(entries.get(&("", "Missing label")).copied(), Some("shared")); + assert!(!entries.contains_key(&("menu", "Ignored label"))); +} + +#[test] +fn installed_chinese_supplement_serves_ordinary_and_menu_lookups() { + const KEY: &str = "Construction Geometry"; + let (projection, errors) = parse(include_str!("zh_shared.tsv")); + assert!(errors.is_empty(), "{errors:?}"); + assert_eq!(projection.len(), 1); + let translated = projection[&("", KEY)]; + assert_ne!(translated, KEY); + assert_eq!(tr("zh", KEY), translated); + assert_eq!(tr_menu("zh", KEY), translated); + with_language("zh", || { + assert_eq!(t(KEY), translated); + assert_eq!(menu(KEY), translated); + assert_eq!(t("Construction Line"), "构造线"); + assert_eq!(t("a future untranslated label"), "a future untranslated label"); + }); + assert_eq!(tr("en", KEY), KEY); +} + /// A prompt with options is translated without its own "or": the command line adds the /// translated " or [" before the options ("Select polyline or [Multiple]"). #[test] diff --git a/crates/ui-egui/src/i18n/zh_shared.tsv b/crates/ui-egui/src/i18n/zh_shared.tsv new file mode 100644 index 000000000..2fcef4b09 --- /dev/null +++ b/crates/ui-egui/src/i18n/zh_shared.tsv @@ -0,0 +1,3 @@ +# Generated from locales/zh-hans/cadcraft.tsv at the root craft-i18n.rev. +# Keep only the ordinary Construction Geometry row, byte-for-byte. Do not translate here. + Construction Geometry 构造几何