Files
capri-core-rust/crates/capri-core/src/i18n.rs
T
nocodeandClaude Opus 5.5 49349d6ecd
CI / test (push) Has been cancelled
Linux port: first run, packaging, single instance, privacy and wiring
First run and backend
- The companion opens without Ollama or a model (OFFLINE / SETUP REQUIRED),
  opens Settings > Language model and picks the model up once downloaded.
- Model, provider and GPU changes apply without restarting (Ollama is
  restarted when launch settings change); no silent fallback from API mode.
- Fatal startup errors are shown in a window; the fallback avatar loads
  again; the shipped Capri artwork is used when nothing was imported.

Packaging and tooling
- packaging/arch/PKGBUILD, capri.desktop (KWin ScreenShot2 key), icons,
  artwork under /usr/share/capri/assets, Gitea Actions CI.
- xtask: `icons` and `release-check` (no private data or credentials).

Runtime
- One instance per data folder (flock + control socket): second launch
  shows the window; capri stop / restart / toggle / status (JSON, starts
  nothing) / doctor / setup / diagnostics export / purge.
- A foreign program on the Ollama port is refused; an Ollama left behind
  by Capri is replaced.

Privacy and security
- No raw Ollama log on disk; API keys are never saved in plaintext (old
  ones move to the keyring); refused credential notes are stored as
  [Credentials omitted]; launched apps get an allow-listed environment.

Fixes
- Stop/Escape silence the voice; saved voice effects apply to replies;
  settings_control glitch settings apply; distinct KWin cursor scripts;
  KDE window capture authorized for source builds; Linux wording.
- Voice pack references zipped with Windows separators are found.

Wired features
- Initiatives, request router, resource monitor, "clip that", questions
  about a snapshot, sleep mode, tap reactions, stopwatch, attachments,
  Internet mode button, voice-synced subtitles, wake visuals, dictation
  draft, tray switches, Spotify seek/shuffle/repeat; computer-control
  planner and classifier follow model switches and API mode.
- Approval cards: one shown, 8 queued, deduplicated, cancelled on stop,
  failure, lock or pause; one Application access switch.

Desktop
- Compositor detection (no black overlay on i3 without picom), i3lock
  detection, unsupported desktops reported at startup, screenshot-only
  control without AT-SPI, fontconfig fallbacks, translatable feature
  pages with French translations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:21:40 +01:00

131 lines
4.1 KiB
Rust

//! Interface translations: English source strings are the keys of
//! `assets/i18n/interface-locales.json`; a missing locale or key falls back to English.
use std::collections::HashMap;
use std::sync::{Arc, OnceLock};
use parking_lot::RwLock;
use serde::Deserialize;
use serde_json::value::RawValue;
static BUNDLE_JSON: &[u8] = include_bytes!("../../../assets/i18n/interface-locales.json");
/// Bundle order; the JSON object order is lost by the map below.
pub const LOCALES: [&str; 50] = [
"en", "ru", "de", "fr", "es", "it", "pt", "nl", "pl", "uk", "cs", "sk", "bg", "hr", "sr", "sl", "ro", "hu", "el", "da", "no",
"sv", "fi", "et", "lv", "lt", "is", "ca", "ar", "he", "fa", "ur", "hi", "bn", "ta", "te", "mr", "gu", "pa", "zh", "ja", "ko",
"vi", "th", "id", "ms", "tl", "tr", "sw", "af",
];
#[derive(Deserialize)]
struct Bundle {
locales: HashMap<String, Locale>,
#[serde(rename = "languageNames")]
language_names: HashMap<String, Box<RawValue>>,
}
#[derive(Deserialize)]
struct Locale {
#[serde(rename = "nativeName")]
native_name: String,
direction: String,
messages: Box<RawValue>,
}
fn bundle() -> &'static Bundle {
static BUNDLE: OnceLock<Bundle> = OnceLock::new();
BUNDLE.get_or_init(|| serde_json::from_slice(BUNDLE_JSON).expect("bundled interface locales"))
}
struct Active {
id: String,
rtl: bool,
messages: HashMap<String, String>,
names: HashMap<String, String>,
}
static ACTIVE: RwLock<Option<Arc<Active>>> = RwLock::new(None);
fn load(id: &str) -> Active {
let bundle = bundle();
let id = if bundle.locales.contains_key(id) { id } else { "en" };
let locale = &bundle.locales[id];
let names = bundle.language_names.get(id).and_then(|raw| serde_json::from_str(raw.get()).ok()).unwrap_or_default();
Active {
id: id.to_string(),
rtl: locale.direction == "rtl",
messages: serde_json::from_str(locale.messages.get()).unwrap_or_default(),
names,
}
}
fn active() -> Arc<Active> {
if let Some(a) = ACTIVE.read().clone() {
return a;
}
let a = Arc::new(load("en"));
*ACTIVE.write() = Some(a.clone());
a
}
/// Switches the interface language; unknown ids mean English.
pub fn set_locale(id: &str) {
if ACTIVE.read().as_ref().is_some_and(|a| a.id == id) {
return;
}
*ACTIVE.write() = Some(Arc::new(load(id)));
}
pub fn locale() -> String {
active().id.clone()
}
/// Arabic, Hebrew, Persian and Urdu. egui has no bidi shaping: text is right-aligned only.
pub fn rtl() -> bool {
active().rtl
}
/// Translation of an English UI key; unknown keys (owner data) pass through unchanged.
pub fn t(key: &str) -> String {
active().messages.get(key).cloned().unwrap_or_else(|| key.to_string())
}
/// `t` with `{name}` placeholders replaced.
pub fn tf(key: &str, values: &[(&str, &str)]) -> String {
values.iter().fold(t(key), |text, (name, value)| text.replace(&format!("{{{name}}}"), value))
}
/// Native name of an interface locale, for the language picker.
pub fn native_name(id: &str) -> String {
bundle().locales.get(id).map(|l| l.native_name.clone()).unwrap_or_else(|| id.to_string())
}
/// Conversation language name in the interface language.
pub fn language_name(id: &str) -> String {
if let Some(name) = active().names.get(id) {
return name.clone();
}
crate::persona::languages().iter().find(|l| l.id == id).map(|l| l.label.clone()).unwrap_or_else(|| id.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lookup_and_fallback() {
// One test: the active locale is process-global.
set_locale("fr");
assert_ne!(t("Settings"), "Settings");
assert_eq!(t("zz untranslated key"), "zz untranslated key");
assert!(!rtl());
assert_eq!(language_name("de"), "allemand");
set_locale("he");
assert!(rtl());
set_locale("xx");
assert_eq!(locale(), "en");
assert_eq!(t("VOICE PENDING"), "PREPARING VOICE");
assert_eq!(tf("{seconds} s", &[("seconds", "3")]), "3 s");
assert_eq!(native_name("ru"), "Русский");
}
}