Skip to content

Commit

Permalink
wip - i18n & wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Nov 13, 2023
1 parent 5e21ece commit dfc3a3e
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 200 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/resources/i18n/*
/target
/macros/target
/Cargo.lock
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,21 @@ kaspad = { path = "../rusty-kaspa/kaspad" }
# workflow-chrome = "0.8.1"
# workflow-core = "0.8.1"
# workflow-dom = "0.8.1"
# workflow-http = "0.8.1"
# workflow-i18n = "0.8.1"
# workflow-log = "0.8.1"
# workflow-store = "0.8.1"
workflow-chrome = { path = "../workflow-rs/chrome" }
workflow-core = { path = "../workflow-rs/core", features = ["no-unsafe-eval"] }
workflow-dom = { path = "../workflow-rs/dom" }
workflow-http = { path = "../workflow-rs/http" }
workflow-i18n = { path = "../workflow-rs/i18n" }
workflow-log = { path = "../workflow-rs/log" }
workflow-store = { path = "../workflow-rs/store" }
# workflow-chrome = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master" }
# workflow-core = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master", features = ["no-unsafe-eval"] }
# workflow-dom = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master" }
# workflow-http = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master" }
# workflow-i18n = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master" }
# workflow-log = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master" }
# workflow-store = { git = "https://github.com/workflow-rs/workflow-rs.git", branch = "master" }
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kaspa-wrpc-client.workspace = true
workflow-log.workspace = true
workflow-core.workspace = true
workflow-dom.workspace = true
workflow-http.workspace = true
workflow-store.workspace = true
workflow-i18n.workspace = true

Expand Down
209 changes: 151 additions & 58 deletions core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use cfg_if::cfg_if;
use kaspa_ng_core::interop;
use kaspa_ng_core::settings::Settings;
use kaspa_wallet_core::api::WalletApi;
use std::path::PathBuf;
use std::sync::Arc;
use workflow_i18n::*;
use workflow_log::*;

#[allow(unused)]
pub const KASPA_NG_ICON_256X256: &[u8] = include_bytes!("../../resources/icons/icon-256.png");
pub const I18N_DATA: &str = include_str!("../../resources/i18n/i18n.json");
pub const I18N_EMBEDDED: &str = include_str!("../../resources/i18n/i18n.json");

cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
Expand All @@ -22,13 +21,23 @@ cfg_if! {
use kaspa_ng_core::runtime;
use clap::ArgAction;
use eframe::IconData;
use crate::utils::*;
use std::fs;

// use log::info;

#[derive(Debug)]
enum I18n {

Import,
Export,
}

enum Args {
I18n { op : I18n },
Kng {
reset_settings : bool,
disable : bool,
rebuild_i18n: bool,
},
Kaspad { args : Box<NodeArgs> },
}
Expand All @@ -41,6 +50,7 @@ cfg_if! {
Args::Kaspad { args : Box::new(parse_kaspad_args()) }
} else {
let cmd = Command::new("kaspa-ng")

.about(format!("kaspa-ng v{} (rusty-kaspa v{})", env!("CARGO_PKG_VERSION"), kaspa_wallet_core::version()))
// .version(env!("CARGO_PKG_VERSION"))
.arg(arg!(--disable "Disable node services when starting."))
Expand All @@ -51,22 +61,73 @@ cfg_if! {
.help("Reset KaspaNG settings.")
)
.arg(
Arg::new("i18n")
.long("i18n")
Arg::new("i18n-export")
.long("i18n-export")
.action(ArgAction::SetTrue)
.hide(true)
.help("Update i18n dictionary data.")
)
// .arg(arg!(--i18n "Process."))

// .arg(arg!(--i18n "Process."))
.subcommand(
Command::new("i18n").hide(true)
.subcommand(
Command::new("import")//.hide(true)
// .help("import i18n data")
.about("import JSON files suffixed with language codes (*_en.json, *_de.json, etc.)")
)
.subcommand(
Command::new("export")//.hide(true)
.about("export default 'en' data as JSON")
)
)
;



let matches = cmd.get_matches();
let reset_settings = matches.get_one::<bool>("reset-settings").cloned().unwrap_or(false);
let disable = matches.get_one::<bool>("disable").cloned().unwrap_or(false);
let rebuild_i18n = matches.get_one::<bool>("i18n").cloned().unwrap_or(false);

Args::Kng { reset_settings, disable, rebuild_i18n }
// let v = matches.subcommand_matches("i18n")
// .and_then(|matches|{
// matches.subcommand_matches("import").map(|_|I18n::Import).or_else(||{
// matches.subcommand_matches("export").map(|_|I18n::Export)
// })

// });

// println!("v: {:#?}", v);
// std::process::exit(1);

// .map(|_matches|I18n::Import)
// .or_else(|| matches);

if let Some(matches) = matches.subcommand_matches("i18n") {
if let Some(_matches) = matches.subcommand_matches("import") {
Args::I18n { op : I18n::Import }
} else if let Some(_matches) = matches.subcommand_matches("export") {
Args::I18n { op : I18n::Export }
} else {
panic!("unknown i18n subcommand")
}
} else {
let reset_settings = matches.get_one::<bool>("reset-settings").cloned().unwrap_or(false);
let disable = matches.get_one::<bool>("disable").cloned().unwrap_or(false);

Args::Kng { reset_settings, disable }

}
// println!("Args: {:#?}", v);
// std::process::exit(1);

// let i18n_import = matches.subcommand_matches("i18n").and_then(|matches|matches.subcommand_matches("import")).is_some();
// let i18n_export = matches.subcommand_matches("i18n").and_then(|matches|matches.subcommand_matches("export")).is_some();


// println!("matches: {:#?}", matches);

// println!("i18n import: {:#?}", i18n_import);
// println!("i18n export: {:#?}", i18n_export);
// std::process::exit(1);
}
}

Expand All @@ -84,7 +145,42 @@ cfg_if! {
core.run();
}

Args::Kng { reset_settings, disable, rebuild_i18n } => {
Args::I18n {
op
} => {
let i18n_json_file = i18n_storage_file()?;
let i18n_json_file_store = i18n_storage_file()?;
i18n::Builder::new("en", "en")
.with_static_json_data(I18N_EMBEDDED)
.with_string_json_data(i18n_json_file.exists().then(move ||{
fs::read_to_string(i18n_json_file)
}).transpose()?)
.with_store(move |json_data: &str| {
Ok(fs::write(&i18n_json_file_store, json_data)?)
})
.try_init()?;

match op {
I18n::Import => {
let source_folder = std::env::current_dir()?;
println!("importing translation files from: '{}'", source_folder.display());
i18n::import_translation_files(source_folder,false)?;
}
I18n::Export => {
let target_folder = if let Some(cwd) = try_cwd_repo_root()? {
cwd.join("resources").join("i18n")
} else {
std::env::current_dir()?
};
println!("exporting default language to: '{}'", target_folder.display());
i18n::export_default_language(move |json_data: &str| {
Ok(fs::write(&target_folder, json_data)?)
})?;
}
}
}

Args::Kng { reset_settings, disable } => {

println!("kaspa-ng v{} (rusty-kaspa v{})", env!("CARGO_PKG_VERSION"), kaspa_wallet_core::version());

Expand All @@ -101,34 +197,25 @@ cfg_if! {
})
};

println!("SETTINGS: {:#?}", settings);
println!("settings: {:#?}", settings);

let i18n_json_file = i18n_storage_file()?;
let i18n_json_file_load = i18n_json_file.clone();
let i18n_json_file_store = i18n_json_file.clone();
i18n::Builder::new(settings.language_code.as_str(), "en")
.with_static_json_data(I18N_EMBEDDED)
.with_string_json_data(i18n_json_file.exists().then(move ||{
fs::read_to_string(i18n_json_file_load)
}).transpose()?)
.with_store(move |json_data: &str| {
Ok(fs::write(&i18n_json_file_store, json_data)?)
})
.try_init()?;

if disable {
settings.node.node_kind = kaspa_ng_core::settings::KaspadNodeKind::Disable;
}

if rebuild_i18n {

let storage_path = i18n::storage_path()?;
let i18n_json = storage_path.join("i18n.json");
if !i18n_json.exists() {
i18n::try_init(settings.language_code.as_str(), "en", None, Option::<PathBuf>::None).expect("failed to init i18n");
i18n::store(i18n::storage_path()?).expect("failed to store i18n data");
}
// else {

// }
// i18n::store("hello").expect("failed to store i18n data");

return Ok(());
} else {
// i18n::try_init(settings.language_code.as_str(), "en", Some(I18N_DATA), Some(i18n::storage_path()?)).expect("failed to init i18n");
i18n::try_init(settings.language_code.as_str(), "en", Some(I18N_DATA), Option::<PathBuf>::None).expect("failed to init i18n");

}



let interop: Arc<Mutex<Option<interop::Interop>>> = Arc::new(Mutex::new(None));
let delegate = interop.clone();
// println!("spawn done");
Expand Down Expand Up @@ -188,32 +275,38 @@ cfg_if! {
Settings::default()
});

init_i18n(settings.language_code.as_str()).expect("failed to init i18n");

// wasm_bindgen_futures::spawn_local(async {
use workflow_log::*;
log_info!("starting");
eframe::WebRunner::new()
.start(
"kaspa-ng",
web_options,
Box::new(move |cc| {
let interop = interop::Interop::new(&cc.egui_ctx, &settings);
interop.start();
// init_i18n(settings.language_code.as_str()).expect("failed to init i18n");

let adaptor = kaspa_ng_core::adaptor::Adaptor::new(interop.clone());
let window = web_sys::window().expect("no global `window` exists");
js_sys::Reflect::set(
&window,
&JsValue::from_str("adaptor"),
&JsValue::from(adaptor),
).expect("failed to set adaptor");
i18n::Builder::new(settings.language_code.as_str(), "en")
.with_static_json_data(I18N_EMBEDDED)
// .with_store(move |json_data: &str| {
// })
.try_init()?;

Box::new(kaspa_ng_core::Core::new(cc, interop, settings))
}),
)
.await
.expect("failed to start eframe");
// wasm_bindgen_futures::spawn_local(async {
use workflow_log::*;
log_info!("starting");
eframe::WebRunner::new()
.start(
"kaspa-ng",
web_options,
Box::new(move |cc| {
let interop = interop::Interop::new(&cc.egui_ctx, &settings);
interop.start();

let adaptor = kaspa_ng_core::adaptor::Adaptor::new(interop.clone());
let window = web_sys::window().expect("no global `window` exists");
js_sys::Reflect::set(
&window,
&JsValue::from_str("adaptor"),
&JsValue::from(adaptor),
).expect("failed to set adaptor");

Box::new(kaspa_ng_core::Core::new(cc, interop, settings))
}),
)
.await
.expect("failed to start eframe");

// log_info!("shutting down...");
// });
Expand Down
Loading

0 comments on commit dfc3a3e

Please sign in to comment.