egui: load locale language on startup

This commit is contained in:
Mikolaj Wielgus 2024-08-08 17:12:04 +02:00
parent 41bd60ea59
commit 0257568410
5 changed files with 38 additions and 17 deletions

View File

@ -19,7 +19,7 @@ required-features = ["egui"]
[features] [features]
default = ["disable_contracts"] default = ["disable_contracts"]
cli = ["dep:clap"] 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"] disable_contracts = ["contracts/disable_contracts"]
[dependencies] [dependencies]
@ -35,7 +35,6 @@ contracts = "0.6.3"
bimap = "0.6.3" bimap = "0.6.3"
log = "0.4" log = "0.4"
utf8-chars = "3.0.2" utf8-chars = "3.0.2"
fluent-templates = "0.9.4"
[dependencies.specctra_derive] [dependencies.specctra_derive]
path = "macro/specctra_derive" path = "macro/specctra_derive"
@ -53,6 +52,19 @@ optional = true
version = "4.5.8" version = "4.5.8"
features = ["derive"] 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] [dependencies.eframe]
optional = true optional = true
version = "0.26.0" version = "0.26.0"

View File

@ -1 +0,0 @@
-something = foo

View File

@ -10,6 +10,7 @@ use std::{
Arc, Mutex, Arc, Mutex,
}, },
}; };
use unic_langid::{langid, LanguageIdentifier};
use topola::{ use topola::{
autorouter::{ autorouter::{
@ -49,6 +50,8 @@ use crate::{
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct App { pub struct App {
langid: LanguageIdentifier,
#[serde(skip)] #[serde(skip)]
maybe_overlay: Option<Overlay>, maybe_overlay: Option<Overlay>,
@ -86,6 +89,7 @@ pub struct App {
impl Default for App { impl Default for App {
fn default() -> Self { fn default() -> Self {
Self { Self {
langid: langid!("en-US"),
maybe_overlay: None, maybe_overlay: None,
arc_mutex_maybe_invoker: Arc::new(Mutex::new(None)), arc_mutex_maybe_invoker: Arc::new(Mutex::new(None)),
maybe_execute: None, maybe_execute: None,
@ -103,13 +107,20 @@ impl Default for App {
impl App { impl App {
/// Called once on start. /// 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. // Load previous app state if one exists.
if let Some(storage) = cc.storage { 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) { fn update_state(&mut self, dt: f32) {

View File

@ -12,12 +12,13 @@ mod top;
mod viewport; mod viewport;
use app::App; use app::App;
use fluent_templates::static_loader; use fluent_templates::static_loader;
use sys_locale::get_locale;
use unic_langid::{langid, LanguageIdentifier};
static_loader! { static_loader! {
static LOCALES = { static LOCALES = {
locales: "./locales", locales: "./locales",
fallback_language: "en-US", fallback_language: "en-US",
core_locales: "./locales/core.ftl",
}; };
} }
@ -25,6 +26,11 @@ static_loader! {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result<()> { fn main() -> eframe::Result<()> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). 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 { let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default() viewport: egui::ViewportBuilder::default()
@ -35,7 +41,7 @@ fn main() -> eframe::Result<()> {
eframe::run_native( eframe::run_native(
"topola-egui", "topola-egui",
native_options, 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(); eframe::WebLogger::init(log::LevelFilter::Debug).ok();
let web_options = eframe::WebOptions::default(); let web_options = eframe::WebOptions::default();
let langid: LanguageIdentifier = "en-US".parse();
wasm_bindgen_futures::spawn_local(async { wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new() eframe::WebRunner::new()
.start( .start(
"topola-egui", "topola-egui",
web_options, web_options,
Box::new(|cc| Box::new(App::new(cc))), Box::new(|cc| Box::new(App::new(cc, langid))),
) )
.await .await
.expect("failed to start eframe"); .expect("failed to start eframe");

View File

@ -11,14 +11,6 @@ use topola::autorouter::selection::PinSelection;
use topola::autorouter::Autorouter; use topola::autorouter::Autorouter;
use topola::specctra::design::SpecctraDesign; use topola::specctra::design::SpecctraDesign;
static_loader! {
static LOCALES = {
locales: "./locales",
fallback_language: "en-US",
core_locales: "./locales/core.ftl",
};
}
#[derive(Parser, Debug, Default)] #[derive(Parser, Debug, Default)]
#[command(about, version)] #[command(about, version)]
struct Cli { struct Cli {