From 0c628208b5dff362b31b23be4480ed69374de669 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Sun, 8 Nov 2026 15:08:07 +0000 Subject: [PATCH] build(i18n): embed the craft-i18n catalogs pinned in craft-i18n.rev - `craft-i18n` crate (craft-libs c5a1cbfb, PhotoCraft's existing pin; already in the tree through craft-ui) as a ui-egui dependency. - build.rs writes `CRAFT_I18N` from `CRAFT_I18N_DIR`: one (locale, common.tsv, photocraft.tsv) entry per locale with a PhotoCraft catalog, paths written with `{:?}` and never canonicalized. Unset: empty, English only; `CRAFT_I18N_REQUIRED=1` makes a missing checkout an error. - `craft-i18n.rev` pins storytold/craft-i18n 4bafe3ed (the cutover sync of photocraft@848fa30), LF through `.gitattributes`. - Both flatpak manifests fetch craft-i18n at that commit; the source build sets `CRAFT_I18N_DIR` and `CRAFT_I18N_REQUIRED=1`. A test keeps both manifests equal to the pin. - ci.yml: the Windows, without-heif and web jobs check out craft-i18n at the pin, as the shared gate and release workflows do; a pin bump selects the web and Windows jobs. Co-Authored-By: Claude Opus 5.5 --- .gitattributes | 2 + .github/workflows/ci.yml | 23 +++++- Cargo.lock | 1 + Cargo.toml | 4 + apps/photocraft/src/main.rs | 20 +++++ craft-i18n.rev | 1 + crates/ui-egui/Cargo.toml | 2 + crates/ui-egui/build.rs | 82 +++++++++++++++++++ .../ai.storyteller.photocraft.bundle.yml | 11 +++ .../flatpak/ai.storyteller.photocraft.yml | 9 ++ 10 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 .gitattributes create mode 100644 craft-i18n.rev diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..5ec2f05cef --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# The craft-i18n pin: CI and the build read it as one SHA, so it stays LF everywhere (readers trim it too). +craft-i18n.rev text eol=lf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0f8c9770d..800f464bae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -130,13 +130,14 @@ jobs: fi # The browser build (a fat-LTO wasm build) runs only when something it compiles or # packages changed: the web app and its packaging, crate sources and manifests (the web - # build compiles nearly every crate), the workspace manifests and this workflow. - if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/photocraft-web(-boot)?/|packaging/web/|packaging/env\.sh$|crates/[^/]+/(src/|Cargo\.toml$|build\.rs$)|Cargo\.(toml|lock)$|rust-toolchain(\.toml)?$|\.github/workflows/ci\.yml$)'; then + # build compiles nearly every crate), the workspace manifests, the craft-i18n pin and this + # workflow. + if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/photocraft-web(-boot)?/|packaging/web/|packaging/env\.sh$|crates/[^/]+/(src/|Cargo\.toml$|build\.rs$)|Cargo\.(toml|lock)$|craft-i18n\.rev$|rust-toolchain(\.toml)?$|\.github/workflows/ci\.yml$)'; then echo "web=true" >> "$GITHUB_OUTPUT" else echo "web=false" >> "$GITHUB_OUTPUT" fi - if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/|crates/(ui-egui|text|gpu|tablet|heif|format)/|Cargo\.lock$|(.*/)?build\.rs$|\.github/workflows/ci\.yml$)'; then + if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/|crates/(ui-egui|text|gpu|tablet|heif|format)/|Cargo\.lock$|craft-i18n\.rev$|(.*/)?build\.rs$|\.github/workflows/ci\.yml$)'; then echo "windows=true" >> "$GITHUB_OUTPUT" else echo "windows=false" >> "$GITHUB_OUTPUT" @@ -208,6 +209,11 @@ jobs: path: craft-fonts persist-credentials: false + # The translations at the commit in craft-i18n.rev, as the gate and release.yml embed them + # (sets CRAFT_I18N_DIR and CRAFT_I18N_REQUIRED=1). + - name: Check out craft-i18n + uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable with: components: clippy @@ -288,6 +294,11 @@ jobs: with: persist-credentials: false + # The translations at the commit in craft-i18n.rev, as the gate and release.yml embed them + # (sets CRAFT_I18N_DIR and CRAFT_I18N_REQUIRED=1). + - name: Check out craft-i18n + uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main + - name: Install Linux build dependencies uses: storytold/craft-common-actions/actions/install-apt-packages@main with: @@ -361,6 +372,12 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false + + # The translations at the commit in craft-i18n.rev, as the gate and release.yml embed them + # (sets CRAFT_I18N_DIR and CRAFT_I18N_REQUIRED=1). + - name: Check out craft-i18n + uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable with: targets: wasm32-unknown-unknown diff --git a/Cargo.lock b/Cargo.lock index 434e4b6aa8..f528b8cc38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5775,6 +5775,7 @@ version = "0.20.1" dependencies = [ "base64 0.23.1", "chrono", + "craft-i18n", "craft-print", "craft-ui", "craft-update", diff --git a/Cargo.toml b/Cargo.toml index f5fafa868d..45159b8ec0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,10 @@ craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" } craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" } craft-pcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "b8f7d93b00dfb8f172938f3d2d796ae62dcfe123" } +# Interface translations: lookup, fallback, CLDR plurals and language names over the craft-i18n +# catalogs that `crates/ui-egui/build.rs` embeds from `CRAFT_I18N_DIR` (storytold/craft-i18n, at +# the commit in `craft-i18n.rev`). Already in the tree through craft-ui. +craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" } # craft-update: app-specific pretend versions apply to startup and manual checks. craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] } thiserror = "2" diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs index 5461c4cc19..b24daa7538 100644 --- a/apps/photocraft/src/main.rs +++ b/apps/photocraft/src/main.rs @@ -1066,4 +1066,24 @@ mod tests { let class = entry.lines().find_map(|line| line.strip_prefix("StartupWMClass=")); assert_eq!(class, Some(super::APP_ID)); } + + /// Both flatpak manifests build with the craft-i18n commit `craft-i18n.rev` pins: CI and + /// releases read the file, and the manifests have to name the commit themselves. The pin may + /// have a trailing newline or CRLF (a Windows checkout), so it is trimmed, as every reader does. + #[test] + fn the_flatpak_manifests_pin_the_craft_i18n_rev() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let rev = std::fs::read_to_string(root.join("craft-i18n.rev")).unwrap(); + let rev = rev.trim(); + assert!(rev.len() == 40 && rev.bytes().all(|b| b.is_ascii_hexdigit()), "craft-i18n.rev holds one full SHA: {rev:?}"); + for manifest in ["ai.storyteller.photocraft.yml", "ai.storyteller.photocraft.bundle.yml"] { + let text = std::fs::read_to_string(root.join("packaging/linux/flatpak").join(manifest)).unwrap(); + let source = text.split("url: https://github.com/storytold/craft-i18n").nth(1).unwrap_or_else(|| panic!("{manifest}: a craft-i18n git source")); + let source = source.split("- type:").next().unwrap_or(source); + let commit = + source.lines().find_map(|l| l.trim().strip_prefix("commit:")).unwrap_or_else(|| panic!("{manifest}: the craft-i18n source pins a commit")); + assert_eq!(commit.trim().trim_matches(['\'', '"']), rev, "{manifest}"); + assert!(text.contains("CRAFT_I18N_DIR: /run/build/photocraft/craft-i18n"), "{manifest}: the build names the catalogs"); + } + } } diff --git a/craft-i18n.rev b/craft-i18n.rev new file mode 100644 index 0000000000..b1e8633d24 --- /dev/null +++ b/craft-i18n.rev @@ -0,0 +1 @@ +4bafe3edb74ea29d203602209aac3c6cafc5c3a1 diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index e0111e9ca5..7f26741dae 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -11,6 +11,8 @@ workspace = true chrono = { workspace = true } craft-print = { workspace = true } craft-ui = { workspace = true } +# Interface translations over the embedded craft-i18n catalogs (`build.rs`, `src/i18n.rs`). +craft-i18n = { workspace = true } photocraft-geom = { workspace = true } photocraft-color = { workspace = true } photocraft-cms = { workspace = true } diff --git a/crates/ui-egui/build.rs b/crates/ui-egui/build.rs index a90d4903ad..bcefa13855 100644 --- a/crates/ui-egui/build.rs +++ b/crates/ui-egui/build.rs @@ -7,6 +7,12 @@ //! //! A missing or malformed file gives empty tables (or no names) and a warning. //! +//! - `CRAFT_I18N_DIR`, a craft-i18n checkout (storytold/craft-i18n, at the SHA in +//! `craft-i18n.rev`): `craft_i18n.rs` holds `CRAFT_I18N`, one (locale, shared catalog, PhotoCraft +//! catalog) entry per locale that has a `photocraft.tsv`, embedded raw. Unset, or not a checkout +//! (a warning), it is empty and the interface is English only; with `CRAFT_I18N_REQUIRED=1` (CI +//! and releases) that is an error instead. Recipe: craft-i18n's `docs/integration.md`. +//! //! Also packs the translation catalogs (`src/i18n/*.tsv`) into `OUT_DIR/i18n/` for //! `i18n::LANGUAGES` (see [`pack_catalogs`]): stored that way they take ~0.5 MB of the binary //! instead of ~2.2 MB, which keeps the web build under its 24 MiB size gate @@ -26,10 +32,86 @@ fn main() { if let Err(e) = std::fs::write(&out, generate(&json, &people)) { println!("cargo::warning=writing {}: {e}", out.display()); } + let out = out.with_file_name("craft_i18n.rs"); + if let Err(e) = std::fs::write(&out, craft_i18n()) { + println!("cargo::error=writing {}: {e}", out.display()); + } pack_catalogs(); deflate_egui_fonts(); } +/// The catalog this app reads in each locale: `locales//photocraft.tsv`. +const APP_CATALOG: &str = "photocraft.tsv"; + +/// `CRAFT_I18N` from `CRAFT_I18N_DIR`: the `Embedded` list the craft-i18n crate reads. +fn craft_i18n() -> String { + println!("cargo::rerun-if-env-changed=CRAFT_I18N_DIR"); + println!("cargo::rerun-if-env-changed=CRAFT_I18N_REQUIRED"); + let required = std::env::var("CRAFT_I18N_REQUIRED").is_ok_and(|v| v.trim() == "1"); + let entries = match std::env::var_os("CRAFT_I18N_DIR").filter(|d| !d.is_empty()) { + // Not canonicalized: on Windows that gives `\\?\C:\…`, which `include_str!` can't read. + Some(dir) => match locales(&std::path::PathBuf::from(dir).join("locales")) { + Ok(entries) => entries, + Err(e) => { + let level = if required { "error" } else { "warning" }; + println!("cargo::{level}=CRAFT_I18N_DIR: {e}; the interface is English only"); + Vec::new() + } + }, + None => { + if required { + println!("cargo::error=CRAFT_I18N_REQUIRED=1 but CRAFT_I18N_DIR is unset: a release must embed the translations"); + } + Vec::new() + } + }; + let mut o = String::from("/// (locale, shared catalog, PhotoCraft's catalog), one entry per locale; `\"\"` for a missing file.\n"); + o.push_str("pub static CRAFT_I18N: &craft_i18n::Embedded = &[\n"); + for (code, shared, app) in entries { + // `{:?}` escapes the path (Windows backslashes) as a Rust string literal. + let shared = shared.map_or_else(|| "\"\"".to_string(), |p| format!("include_str!({p:?})")); + let _ = writeln!(o, " ({code:?}, {shared}, include_str!({app:?})),"); + } + o.push_str("];\n"); + o +} + +/// One embedded locale: its code, its `common.tsv` if it has one, and its PhotoCraft catalog. +type Locale = (String, Option, std::path::PathBuf); + +/// Every locale under `dir` with PhotoCraft's catalog, by code. An error when `dir` isn't a +/// craft-i18n `locales/` directory. +fn locales(dir: &std::path::Path) -> Result, String> { + // Build scripts run in the crate's directory and `include_str!` resolves against the + // generated file, so a relative path would mean two different places. + if !dir.is_absolute() { + return Err(format!("{} is not an absolute path", dir.display())); + } + // Before read_dir, so a checkout that appears later reruns the script. + println!("cargo::rerun-if-changed={}", dir.display()); + let list = std::fs::read_dir(dir).map_err(|e| format!("{} is not a craft-i18n checkout ({e})", dir.display()))?; + let mut out = Vec::new(); + for entry in list.flatten() { + let path = entry.path(); + let Some(code) = path.file_name().and_then(|n| n.to_str()).map(str::to_string) else { continue }; + let app = path.join(APP_CATALOG); + if !app.is_file() { + continue; + } + println!("cargo::rerun-if-changed={}", app.display()); + let shared = Some(path.join("common.tsv")).filter(|p| p.is_file()); + if let Some(p) = &shared { + println!("cargo::rerun-if-changed={}", p.display()); + } + out.push((code, shared, app)); + } + if out.is_empty() { + return Err(format!("{} has no locale with a {APP_CATALOG}", dir.display())); + } + out.sort(); + Ok(out) +} + /// Deflate egui's default fonts (the `epaint_default_fonts` crate) into `OUT_DIR/egui_fonts/` for /// `egui_fonts.rs`. The app builds egui without `default_fonts`, which would embed the same 1.4 MB /// uncompressed; deflated they take 0.78 MB (the web build's 24 MiB size gate). diff --git a/packaging/linux/flatpak/ai.storyteller.photocraft.bundle.yml b/packaging/linux/flatpak/ai.storyteller.photocraft.bundle.yml index 49d441ed2f..e88c2ecbc7 100644 --- a/packaging/linux/flatpak/ai.storyteller.photocraft.bundle.yml +++ b/packaging/linux/flatpak/ai.storyteller.photocraft.bundle.yml @@ -95,6 +95,13 @@ modules: - 'exec "/app/libexec/cups/${0##*/}" "$@"' - name: photocraft buildsystem: simple + # The release binaries were built with the interface translations at the commit in + # craft-i18n.rev (the craft-i18n source below); nothing here compiles, so this records which + # translations the bundle carries and keeps it in step with the from-source manifest (a test + # keeps both equal to the pin). + build-options: + env: + CRAFT_I18N_DIR: /run/build/photocraft/craft-i18n build-commands: - install -Dm755 bin/photocraft /app/bin/photocraft - install -Dm755 bin/photocraft-cli /app/bin/photocraft-cli @@ -107,3 +114,7 @@ modules: sources: - type: dir path: stage + - type: git + url: https://github.com/storytold/craft-i18n + commit: 4bafe3edb74ea29d203602209aac3c6cafc5c3a1 + dest: craft-i18n diff --git a/packaging/linux/flatpak/ai.storyteller.photocraft.yml b/packaging/linux/flatpak/ai.storyteller.photocraft.yml index 3546d873ab..d3849f98c9 100644 --- a/packaging/linux/flatpak/ai.storyteller.photocraft.yml +++ b/packaging/linux/flatpak/ai.storyteller.photocraft.yml @@ -97,6 +97,9 @@ modules: env: CARGO_HOME: /run/build/photocraft/cargo CARGO_NET_OFFLINE: 'true' + # The interface translations (the craft-i18n source below), embedded by build.rs. + CRAFT_I18N_DIR: /run/build/photocraft/craft-i18n + CRAFT_I18N_REQUIRED: '1' build-commands: - cargo --offline build --release --locked -p photocraft --features heif - install -Dm755 target/release/photocraft /app/bin/photocraft @@ -119,4 +122,10 @@ modules: - corpus - build-flatpak - .flatpak-builder + - craft-i18n - cargo-sources.json + # The interface translations at the commit in craft-i18n.rev (a test keeps the two equal). + - type: git + url: https://github.com/storytold/craft-i18n + commit: 4bafe3edb74ea29d203602209aac3c6cafc5c3a1 + dest: craft-i18n