From 941a271339e7654bce085d2090e7603c88150092 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Mon, 14 Oct 2024 04:26:12 +0200 Subject: [PATCH] fix(egui): show language display names instead of codes --- Cargo.toml | 8 +++++++- src/bin/topola-egui/menu_bar.rs | 29 ++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0cd37b6..f125a4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,10 @@ egui = [ "dep:env_logger", "dep:fluent-templates", "dep:futures-lite", + "dep:icu", "dep:rfd", "dep:sys-locale", - "dep:unic-langid" + "dep:unic-langid", ] disable_contracts = ["contracts-try/disable_contracts"] @@ -74,6 +75,11 @@ features = ["macros", "serde"] optional = true version = "0.11" +[dependencies.icu] +optional = true +version = "1.5.0" +features = ["experimental"] + [dependencies.eframe] optional = true version = "0.29" diff --git a/src/bin/topola-egui/menu_bar.rs b/src/bin/topola-egui/menu_bar.rs index 0f24c16..1977f87 100644 --- a/src/bin/topola-egui/menu_bar.rs +++ b/src/bin/topola-egui/menu_bar.rs @@ -1,4 +1,8 @@ -use std::{ops::ControlFlow, path::Path, sync::mpsc::Sender}; +use icu::{ + experimental::displaynames::LocaleDisplayNamesFormatter, + locid::{locale, LanguageIdentifier, Locale}, +}; +use std::{borrow::Cow, ops::ControlFlow, path::Path, sync::mpsc::Sender}; use topola::{ autorouter::{ @@ -168,12 +172,23 @@ impl MenuBar { ui.menu_button(tr.text("tr-menu-properties"), |ui| { ui.menu_button(tr.text("tr-menu-properties-set-language"), |ui| { for langid in Translator::locales() { - ui.radio_value( - tr.langid_mut(), - langid.clone(), - langid.language.as_str(), - ); - //ui.add(egui::RadioButton::new(true, locale.language.as_str())); + if let Ok(locale) = + Locale::try_from_bytes(langid.to_string().as_bytes()) + { + if let Ok(formatter) = LocaleDisplayNamesFormatter::try_new( + &locale.clone().into(), + Default::default(), + ) { + ui.radio_value( + tr.langid_mut(), + langid.clone(), + formatter.of(&locale), + ); + continue; + } + } + + ui.radio_value(tr.langid_mut(), langid.clone(), langid.to_string()); } }); });