From 02575684105d82791a4478cbe46371f2e7b82ed3 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Thu, 8 Aug 2024 17:12:04 +0200 Subject: [PATCH] egui: load locale language on startup --- Cargo.toml | 16 ++++++++++++++-- locales/core.ftl | 1 - src/bin/topola-egui/app.rs | 17 ++++++++++++++--- src/bin/topola-egui/main.rs | 13 ++++++++++--- src/bin/topola/main.rs | 8 -------- 5 files changed, 38 insertions(+), 17 deletions(-) delete mode 100644 locales/core.ftl diff --git a/Cargo.toml b/Cargo.toml index c49250e..364457f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ required-features = ["egui"] [features] default = ["disable_contracts"] cli = ["dep:clap"] -egui = ["dep:eframe", "dep:egui", "dep:rfd", "dep:futures"] +egui = ["dep:sys-locale", "dep:unic-langid", "dep:fluent-templates", "dep:eframe", "dep:egui", "dep:rfd", "dep:futures"] disable_contracts = ["contracts/disable_contracts"] [dependencies] @@ -35,7 +35,6 @@ contracts = "0.6.3" bimap = "0.6.3" log = "0.4" utf8-chars = "3.0.2" -fluent-templates = "0.9.4" [dependencies.specctra_derive] path = "macro/specctra_derive" @@ -53,6 +52,19 @@ optional = true version = "4.5.8" features = ["derive"] +[dependencies.sys-locale] +optional = true +version = "0.3.1" + +[dependencies.unic-langid] +optional = true +version = "0.9.5" +features = ["macros", "serde"] + +[dependencies.fluent-templates] +optional = true +version = "0.9.4" + [dependencies.eframe] optional = true version = "0.26.0" diff --git a/locales/core.ftl b/locales/core.ftl deleted file mode 100644 index f169eca..0000000 --- a/locales/core.ftl +++ /dev/null @@ -1 +0,0 @@ --something = foo diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index 89900c0..1b08b89 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -10,6 +10,7 @@ use std::{ Arc, Mutex, }, }; +use unic_langid::{langid, LanguageIdentifier}; use topola::{ autorouter::{ @@ -49,6 +50,8 @@ use crate::{ #[derive(Serialize, Deserialize)] #[serde(default)] pub struct App { + langid: LanguageIdentifier, + #[serde(skip)] maybe_overlay: Option, @@ -86,6 +89,7 @@ pub struct App { impl Default for App { fn default() -> Self { Self { + langid: langid!("en-US"), maybe_overlay: None, arc_mutex_maybe_invoker: Arc::new(Mutex::new(None)), maybe_execute: None, @@ -103,13 +107,20 @@ impl Default for App { impl App { /// Called once on start. - pub fn new(cc: &eframe::CreationContext<'_>) -> Self { + pub fn new(cc: &eframe::CreationContext<'_>, langid: LanguageIdentifier) -> Self { // Load previous app state if one exists. if let Some(storage) = cc.storage { - return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default(); + let this = Self { + langid, + ..eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default() + }; + return this; } - Default::default() + Self { + langid, + ..Default::default() + } } fn update_state(&mut self, dt: f32) { diff --git a/src/bin/topola-egui/main.rs b/src/bin/topola-egui/main.rs index e87f150..a5d6cf7 100644 --- a/src/bin/topola-egui/main.rs +++ b/src/bin/topola-egui/main.rs @@ -12,12 +12,13 @@ mod top; mod viewport; use app::App; use fluent_templates::static_loader; +use sys_locale::get_locale; +use unic_langid::{langid, LanguageIdentifier}; static_loader! { static LOCALES = { locales: "./locales", fallback_language: "en-US", - core_locales: "./locales/core.ftl", }; } @@ -25,6 +26,11 @@ static_loader! { #[cfg(not(target_arch = "wasm32"))] fn main() -> eframe::Result<()> { env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). + let langid = if let Some(langname) = get_locale() { + langname.parse().unwrap_or(langid!("en-US")) + } else { + langid!("en-US") + }; let native_options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() @@ -35,7 +41,7 @@ fn main() -> eframe::Result<()> { eframe::run_native( "topola-egui", native_options, - Box::new(|cc| Box::new(App::new(cc))), + Box::new(|cc| Box::new(App::new(cc, langid))), ) } @@ -46,13 +52,14 @@ fn main() { eframe::WebLogger::init(log::LevelFilter::Debug).ok(); let web_options = eframe::WebOptions::default(); + let langid: LanguageIdentifier = "en-US".parse(); wasm_bindgen_futures::spawn_local(async { eframe::WebRunner::new() .start( "topola-egui", web_options, - Box::new(|cc| Box::new(App::new(cc))), + Box::new(|cc| Box::new(App::new(cc, langid))), ) .await .expect("failed to start eframe"); diff --git a/src/bin/topola/main.rs b/src/bin/topola/main.rs index 3d7aadb..3a273f3 100644 --- a/src/bin/topola/main.rs +++ b/src/bin/topola/main.rs @@ -11,14 +11,6 @@ use topola::autorouter::selection::PinSelection; use topola::autorouter::Autorouter; use topola::specctra::design::SpecctraDesign; -static_loader! { - static LOCALES = { - locales: "./locales", - fallback_language: "en-US", - core_locales: "./locales/core.ftl", - }; -} - #[derive(Parser, Debug, Default)] #[command(about, version)] struct Cli {