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]
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"

View File

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

View File

@ -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<Overlay>,
@ -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) {

View File

@ -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");

View File

@ -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 {