Эх сурвалжийг харах

app: load translations from disk

darkfi 1 жил өмнө
parent
commit
dbed68f981

+ 2 - 0
bin/app/assets/lang/en-US/app.ftl

@@ -0,0 +1,2 @@
+hello-world = Hello World!!!!
+channels-label = CHANNELS

+ 2 - 0
bin/app/assets/lang/tr/app.ftl

@@ -0,0 +1,2 @@
+hello-world = Hello World!!!!
+channels-label = KANALLAR

+ 53 - 0
bin/app/src/app/locale.rs

@@ -0,0 +1,53 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2025 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+use std::{fs, path::Path};
+
+#[cfg(target_os = "android")]
+mod ui_consts {
+    pub const LOCALE_PATH: &str = "lang/{locale}/";
+}
+#[cfg(all(
+    any(target_os = "linux", target_os = "macos", target_os = "windows"),
+    not(feature = "emulate-android")
+))]
+mod ui_consts {
+    pub const LOCALE_PATH: &str = "assets/lang/{locale}/";
+}
+
+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)
+}
+
+pub fn read_locale_ftl(locale: &str) -> String {
+    let dir = LOCALE_PATH.replace("{locale}", locale);
+    read_files_to_string(dir).unwrap()
+}

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

@@ -38,6 +38,8 @@ use crate::{
     ExecutorPtr,
 };
 
+pub mod locale;
+use locale::read_locale_ftl;
 mod node;
 mod schema;
 use schema::get_settingsdb_path;
@@ -143,13 +145,17 @@ impl App {
     }
 
     fn setup_locale(&self, window: &mut SceneNode) -> I18nBabelFish {
+        /*
         let i18n_src = indoc! {"
             hello-world = Hello, world!
             channels-label = CHANNELS
         "}
         .to_owned();
+        */
+        let locale = "en-US";
+        let i18n_src = read_locale_ftl(locale);
         // Will be managed by settings eventually
-        let i18n_fish = I18nBabelFish::new(i18n_src, "en-US");
+        let i18n_fish = I18nBabelFish::new(i18n_src, locale);
 
         // sys-locale = "0.3"
         // fluent-langneg = "0.14"
@@ -169,7 +175,7 @@ impl App {
         */
 
         let mut prop = Property::new("locale", PropertyType::Str, PropertySubType::Locale);
-        prop.set_defaults_str(vec!["en-US".to_string()]).unwrap();
+        prop.set_defaults_str(vec![locale.to_string()]).unwrap();
         window.add_property(prop).unwrap();
 
         i18n_fish

+ 1 - 0
bin/app/src/ui/text.rs

@@ -114,6 +114,7 @@ impl Text {
 
         let text = if self.use_i18n.get() {
             if let Some(trans) = self.i18n_fish.tr(&text) {
+                //t!("Translate '{text}' to '{trans}'");
                 trans
             } else {
                 format!("tr err: {}", text)

+ 5 - 1
bin/app/src/ui/win.rs

@@ -21,6 +21,7 @@ use parking_lot::Mutex as SyncMutex;
 use std::sync::{Arc, Weak};
 
 use crate::{
+    app::locale::read_locale_ftl,
     gfx::{
         GfxDrawCall, GfxDrawInstruction, GraphicsEventCharSub, GraphicsEventKeyDownSub,
         GraphicsEventKeyUpSub, GraphicsEventMouseButtonDownSub, GraphicsEventMouseButtonUpSub,
@@ -473,15 +474,18 @@ impl Window {
     }
 
     async fn reload_locale(&self) {
-        // todo: Load this from disk
+        /*
         let i18n_src = indoc::indoc! {"
             hello-world = Hello, world!
             channels-label = KANALLAR
         "}
         .to_owned();
+        */
 
         let locale = self.locale.get();
+        let i18n_src = read_locale_ftl(&locale);
         i!("Changed locale to: {locale}");
+        i!("loaded {i18n_src}");
         assert_eq!(locale, "tr");
         let i18n_fish = I18nBabelFish::new(i18n_src, &locale);
         self.i18n_fish.set(&i18n_fish);