Explorar el Código

app: locale loading using the miniquad asset manager rather than filesystem (which works for accessing APK assets on android)

darkfi hace 11 meses
padre
commit
228362ba30
Se han modificado 2 ficheros con 21 adiciones y 20 borrados
  1. 20 18
      bin/app/src/app/locale.rs
  2. 1 2
      bin/app/src/app/mod.rs

+ 20 - 18
bin/app/src/app/locale.rs

@@ -16,38 +16,40 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
  */
 
 
-use std::{fs, path::Path};
+use std::sync::mpsc::sync_channel;
 
 
 #[cfg(target_os = "android")]
 #[cfg(target_os = "android")]
 mod ui_consts {
 mod ui_consts {
-    pub const LOCALE_PATH: &str = "lang/{locale}/";
+    pub const LOCALE_PATH: &str = "lang/{locale}/{entry}";
 }
 }
 #[cfg(all(
 #[cfg(all(
     any(target_os = "linux", target_os = "macos", target_os = "windows"),
     any(target_os = "linux", target_os = "macos", target_os = "windows"),
     not(feature = "emulate-android")
     not(feature = "emulate-android")
 ))]
 ))]
 mod ui_consts {
 mod ui_consts {
-    pub const LOCALE_PATH: &str = "assets/lang/{locale}/";
+    pub const LOCALE_PATH: &str = "assets/lang/{locale}/{entry}";
 }
 }
 
 
 pub use ui_consts::*;
 pub use ui_consts::*;
 
 
-fn read_files_to_string<P: AsRef<Path>>(dir: P) -> std::io::Result<String> {
-    let mut output = String::new();
-
-    for entry in fs::read_dir(dir)? {
-        let entry = entry?;
-        let path = entry.path();
-        if path.is_file() {
-            let contents = fs::read_to_string(&path)?;
-            output.push_str(&contents);
-        }
-    }
-
-    Ok(output)
-}
+static ENTRIES: &[&'static str] = &[
+    "app.ftl"
+];
 
 
 pub fn read_locale_ftl(locale: &str) -> String {
 pub fn read_locale_ftl(locale: &str) -> String {
     let dir = LOCALE_PATH.replace("{locale}", locale);
     let dir = LOCALE_PATH.replace("{locale}", locale);
-    read_files_to_string(dir).unwrap()
+
+    let mut output = String::new();
+    for entry in ENTRIES {
+        let path = dir.replace("{entry}", entry);
+        let (sender, recvr) = sync_channel(1);
+        miniquad::fs::load_file(&path, move |res| match res {
+            Ok(res) => sender.send(res).unwrap(),
+            Err(e) => panic!("FTL not found! {e}")
+        });
+        let res = recvr.recv().unwrap();
+        let contents = std::str::from_utf8(&res).unwrap();
+        output.push_str(contents);
+    }
+    output
 }
 }

+ 1 - 2
bin/app/src/app/mod.rs

@@ -18,7 +18,6 @@
 
 
 use chrono::{NaiveDate, NaiveDateTime};
 use chrono::{NaiveDate, NaiveDateTime};
 use darkfi_serial::Encodable;
 use darkfi_serial::Encodable;
-use indoc::indoc;
 use sled_overlay::sled;
 use sled_overlay::sled;
 use smol::Task;
 use smol::Task;
 use std::sync::{Arc, Mutex as SyncMutex};
 use std::sync::{Arc, Mutex as SyncMutex};
@@ -146,7 +145,7 @@ impl App {
 
 
     fn setup_locale(&self, window: &mut SceneNode) -> I18nBabelFish {
     fn setup_locale(&self, window: &mut SceneNode) -> I18nBabelFish {
         /*
         /*
-        let i18n_src = indoc! {"
+        let i18n_src = indoc::indoc! {"
             hello-world = Hello, world!
             hello-world = Hello, world!
             channels-label = CHANNELS
             channels-label = CHANNELS
         "}
         "}