Skip to content

Commit

Permalink
parse all locales in dir in compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Feb 27, 2024
1 parent 2feb277 commit 2886344
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ egui_extras = { version = "0.23.0", features = ["all_loaders"] }
egui_file = "0.11.0"
env_logger = "0.11.1"
exe = "0.5.6"
include_dir = "0.7.3"
serde = "1.0.190"
serde_derive = "1.0.190"
serde_json = "1.0.108"
Expand Down
17 changes: 13 additions & 4 deletions src/localization.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
use include_dir::{include_dir, Dir};
use std::collections::HashMap;

const LOCALES: Dir<'_> = include_dir!("./locale");

#[static_init::dynamic]
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")),
]);
static TRANSLATIONS: HashMap<String, &'static str> = {
let mut map = HashMap::<String, &'static str>::new();
for file in LOCALES.files() {
map.insert(
file.path().file_stem().unwrap().to_str().unwrap().to_owned(),
file.contents_utf8().unwrap(),
);
}
map
};

macro_rules! t {
($l:expr) => {
Expand Down

0 comments on commit 2886344

Please sign in to comment.