SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals. svghmi
authorEdouard Tisserant
Fri, 22 Jan 2021 11:30:37 +0100
branchsvghmi
changeset 3116 6da94ec04325
parent 3115 77cfbf1aacf0
child 3117 f058542d0caa
SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
svghmi/gen_index_xhtml.xslt
svghmi/i18n.py
svghmi/i18n.ysl2
--- a/svghmi/gen_index_xhtml.xslt	Fri Jan 22 08:43:24 2021 +0100
+++ b/svghmi/gen_index_xhtml.xslt	Fri Jan 22 11:30:37 2021 +0100
@@ -264,7 +264,9 @@
       <xsl:value-of select="."/>
       <xsl:text>" </xsl:text>
     </xsl:for-each>
-    <xsl:text>
+    <xsl:text> [</xsl:text>
+    <xsl:value-of select="text()"/>
+    <xsl:text>]
 </xsl:text>
     <xsl:apply-templates mode="testtree" select="*">
       <xsl:with-param name="indent">
@@ -959,23 +961,43 @@
     <xsl:text>
 </xsl:text>
     <xsl:variable name="translations" select="ns:GetTranslations($translatable_strings)"/>
-    <xsl:text>var translations = {
-</xsl:text>
-    <xsl:for-each select="$translations/*">
-      <xsl:text>    "</xsl:text>
-      <xsl:value-of select="local-name()"/>
-      <xsl:text>":{
-</xsl:text>
-      <xsl:text>    }</xsl:text>
+    <xsl:text>var langs = [</xsl:text>
+    <xsl:for-each select="$translations/langs/lang">
+      <xsl:value-of select="."/>
+      <xsl:if test="position()!=last()">
+        <xsl:text>,</xsl:text>
+      </xsl:if>
+    </xsl:for-each>
+    <xsl:text>];
+</xsl:text>
+    <xsl:text>var translations = [
+</xsl:text>
+    <xsl:for-each select="$translatable_texts">
+      <xsl:variable name="n" select="position()"/>
+      <xsl:text>  ["</xsl:text>
+      <xsl:value-of select="@id"/>
+      <xsl:text>",[</xsl:text>
+      <xsl:for-each select="$translations/messages/msgid[$n]/msg">
+        <xsl:text>"</xsl:text>
+        <xsl:for-each select="line">
+          <xsl:value-of select="."/>
+          <xsl:if test="position()!=last()">
+            <xsl:text>\n</xsl:text>
+          </xsl:if>
+        </xsl:for-each>
+        <xsl:text>"</xsl:text>
+        <xsl:if test="position()!=last()">
+          <xsl:text>,</xsl:text>
+        </xsl:if>
+      </xsl:for-each>
+      <xsl:text>]]</xsl:text>
       <xsl:if test="position()!=last()">
         <xsl:text>,</xsl:text>
       </xsl:if>
       <xsl:text>
 </xsl:text>
     </xsl:for-each>
-    <xsl:text>};
-</xsl:text>
-    <xsl:text>
+    <xsl:text>]
 </xsl:text>
     <xsl:text>
 </xsl:text>
--- a/svghmi/i18n.py	Fri Jan 22 08:43:24 2021 +0100
+++ b/svghmi/i18n.py	Fri Jan 22 11:30:37 2021 +0100
@@ -106,20 +106,23 @@
 
 def TranslationToEtree(langs,translated_messages):
 
-    langsroot = etree.Element("langs")
+    result = etree.Element("translations")
+
+    langsroot = etree.SubElement(result, "langs")
     for lang in langs:
         langel = etree.SubElement(langsroot, "lang")
         langel.text = lang
 
-    msgsroot = etree.Element("translations")
+    msgsroot = etree.SubElement(result, "messages")
     for msgid, msgs in translated_messages:
         msgidel = etree.SubElement(msgsroot, "msgid")
-        msgidel.text = msgid 
         for msg in msgs:
             msgel = etree.SubElement(msgidel, "msg")
-            msgel.text = msg 
-   
-    return [langsroot,msgsroot]
+            for line in msg.split("\n"):
+                lineel = etree.SubElement(msgel, "line")
+                lineel.text = escape(line.encode("utf-8")).decode("utf-8")
+
+    return result
 
 
 
--- a/svghmi/i18n.ysl2	Fri Jan 22 08:43:24 2021 +0100
+++ b/svghmi/i18n.ysl2	Fri Jan 22 11:30:37 2021 +0100
@@ -20,13 +20,28 @@
 
 emit "preamble:i18n" {
     const "translations", "ns:GetTranslations($translatable_strings)";
-    | var translations = {
-    foreach "$translations/*" {
-    |     "«local-name()»":{
-        /* TODO */
-    |     }`if "position()!=last()" > ,`
+    > var langs = [
+    foreach "$translations/langs/lang" {
+        value  ".";
+        if "position()!=last()" > ,
     }
-    | };
-    |
-
+    | ];
+    | var translations = [
+    foreach "$translatable_texts" {
+        const "n","position()";
+    >   ["«@id»",[
+        foreach "$translations/messages/msgid[$n]/msg" {
+            > "
+            foreach "line" {
+                value ".";
+                if "position()!=last()" > \\\\n
+            }
+            > "
+            if "position()!=last()" > ,
+        }
+        > ]]
+        if "position()!=last()" > ,
+        > \n
+    }
+    | ]
 }