SVGHMI: generate JS translation literals so that it is possible to also translate uses of texts, and added JS code that apply translations to tspans svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Thu, 04 Feb 2021 21:23:58 +0100
branchsvghmi
changeset 3126 f45e413a0f40
parent 3125 1fb0c07bd97b
child 3127 d4dfd47f8156
SVGHMI: generate JS translation literals so that it is possible to also translate uses of texts, and added JS code that apply translations to tspans
svghmi/i18n.ysl2
svghmi/svghmi.js
--- a/svghmi/i18n.ysl2	Thu Feb 04 21:20:01 2021 +0100
+++ b/svghmi/i18n.ysl2	Thu Feb 04 21:23:58 2021 +0100
@@ -22,14 +22,21 @@
     const "translations", "ns:GetTranslations($translatable_strings)";
     > var langs = [
     foreach "$translations/langs/lang" {
-        value  ".";
+        > "«.»"
         if "position()!=last()" > ,
     }
     | ];
     | var translations = [
     foreach "$translatable_texts" {
         const "n","position()";
-    >   ["«@id»",[
+        const "current_id","@id";
+        const "text_unlinked_uses","$result_svg_ns//svg:text[@original = $current_id]/@id";
+    >   [[
+        foreach "@id | $text_unlinked_uses" {
+            > id("«.»")
+            if "position()!=last()" > ,
+        }
+    > ],[
         foreach "$translations/messages/msgid[$n]/msg" {
             > "
             foreach "line" {
--- a/svghmi/svghmi.js	Thu Feb 04 21:20:01 2021 +0100
+++ b/svghmi/svghmi.js	Thu Feb 04 21:23:58 2021 +0100
@@ -465,3 +465,18 @@
     current_modal = undefined;
 };
 
+function switch_lang(lang) {
+    langnum = langs.indexOf(lang);
+    if(langnum == -1) {
+        console.log("Unknown language: "+lang+", return to original");
+        location.reload(true);
+    }
+
+    for (let translation of translations) {
+        let [objs, msgs] = translation;
+        let msg = msgs[langnum];
+        for (let obj of objs) {
+            msg.split('\\\\n').map((line,i) => {obj.children[i].textContent = line;});
+        }
+    }
+}