Skip to content

Commit

Permalink
add ui language selection
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Feb 27, 2024
1 parent d831bdf commit 2feb277
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct App {
pub df_running: bool,
pub dfhack_installed: bool,
pub selected_language: String,
pub ui_locale: String,
pub df_os: OS,
pub df_dir: Option<PathBuf>,
pub df_bin: Option<PathBuf>,
Expand Down Expand Up @@ -48,6 +49,7 @@ impl Default for App {
df_running: is_df_running(),
dfhack_installed: is_dfhack_installed(&df_dir),
selected_language,
ui_locale: LOCALE.read().current_locale(),
df_os: df_os_by_bin(&df_bin),
df_dir: df_dir,
df_bin,
Expand Down Expand Up @@ -110,12 +112,22 @@ impl eframe::App for App {

// UI block
// status bar
egui::TopBottomPanel::bottom("status").show(ctx, |ui| {
egui::TopBottomPanel::bottom("status").min_height(25.).show(ctx, |ui| {
ui.horizontal_centered(|ui| {
ui.add(egui::Image::new(GITHUB_ICON.to_owned()).max_height(15.).max_width(15.));
ui.hyperlink_to(t!("Report bug"), URL_BUGS);
ui.label(format!("v{VERSION}"));
})
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
egui::ComboBox::from_id_source("locale").selected_text(&self.ui_locale).width(60.).show_ui(ui, |ui| {
let mut lock = LOCALE.write();
for item in lock.locales() {
if ui.selectable_value(&mut self.ui_locale, item.clone(), item.clone()).clicked() {
lock.set(&item)
}
}
});
});
});
});

egui::CentralPanel::default().show(ctx, |ui| {
Expand Down
1 change: 0 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub const URL_HOOK_MANIFEST: &'static str =
"https://raw.githubusercontent.com/dfint/update-data/main/metadata/hook.json";
pub const URL_DICT_MANIFEST: &'static str =
"https://raw.githubusercontent.com/dfint/update-data/main/metadata/dict.json";
pub const URL_BOOSTY: &'static str = "https://boosty.to/dfrus";
pub const URL_BUGS: &'static str = "https://github.com/dfint/installer/issues";

pub const PATH_DATA: &'static str = "dfint-data";
Expand Down
47 changes: 35 additions & 12 deletions src/localization.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
use std::collections::HashMap;

#[static_init::dynamic]
pub static LOCALE: Localization = Localization::new();
pub static mut LOCALE: Localization = {
let locale = sys_locale::get_locale().unwrap_or("en-US".to_owned());
Localization::new(locale)
};

#[static_init::dynamic]
static TRANSLATIONS: HashMap<String, &'static str> = HashMap::from([
("en-US".to_owned(), std::include_str!("../locale/en-US.json")),
("ru-RU".to_owned(), std::include_str!("../locale/ru-RU.json")),
]);

macro_rules! t {
($l:expr) => {
LOCALE.get($l)
LOCALE.read().get($l)
};
}
pub(crate) use t;

pub struct Localization {
map: HashMap<String, String>,
locale: String,
}

impl Localization {
fn new() -> Self {
let current_locale = sys_locale::get_locale().unwrap_or("en-US".to_owned());
let translations = HashMap::<String, &'static str>::from([
("en-US".to_owned(), std::include_str!("../locale/en-US.json")),
("ru-RU".to_owned(), std::include_str!("../locale/ru-RU.json")),
]);

let translation: HashMap<String, String> = match translations.get(&current_locale) {
pub fn new(locale: String) -> Self {
let translation: HashMap<String, String> = match TRANSLATIONS.get(&locale) {
Some(v) => serde_json::from_str(v).unwrap(),
None => serde_json::from_str(translations.get("en-US").unwrap()).unwrap(),
None => serde_json::from_str(TRANSLATIONS.get("en-US").unwrap()).unwrap(),
};

Self { map: translation }
Self {
map: translation,
locale,
}
}

pub fn get(&self, s: &str) -> String {
self.map.get(s).unwrap_or(&"unknown key".to_owned()).to_owned()
}

pub fn set(&mut self, s: &str) {
self.locale = s.to_owned();
self.map = match TRANSLATIONS.get(s) {
Some(v) => serde_json::from_str(v).unwrap(),
None => serde_json::from_str(TRANSLATIONS.get("en-US").unwrap()).unwrap(),
};
}

pub fn current_locale(&self) -> String {
self.locale.clone()
}

pub fn locales(&self) -> Vec<String> {
TRANSLATIONS.keys().cloned().collect()
}
}

0 comments on commit 2feb277

Please sign in to comment.