Tiny changes...
[vchess.git] / client / src / main.js
index e68447e..4659a5b 100644 (file)
@@ -3,6 +3,7 @@ import App from "./App.vue";
 import router from "./router";
 import params from "./parameters"; //for socket connection
 import { ajax } from "./utils/ajax";
+import { util } from "./utils/misc";
 
 Vue.config.productionTip = false;
 
@@ -10,13 +11,33 @@ new Vue({
   router,
   render: function(h) {
     return h(App);
+  },
+//  data: {
+//    lang: "",
+//  },
+  watch: {
+    $lang: async function(newLang) {
+      // Fill modalWelcome, and import translations from "./translations/$lang.js"
+      document.getElementById("modalWelcome").innerHTML =
+        require("raw-loader!pug-plain-loader!./modals/welcome/" + newLang + ".pug");
+      const tModule = await import("./translations/" + newLang + ".js");
+      Vue.prototype.$tr = tModule.translations;
+      //console.log(tModule.translations);
+    },
+    $route: function(newRoute) {
+      console.log(this.$route.params);
+      //TODO: conn.send("enter", newRoute)
+    },
   },
        created: function() {
-               //alert("test");
-               ajax("/variants", "GET", res => {
-                       Vue.prototype.$variants = res.variantArray;
-               });
-               Vue.prototype.$conn = null; //TODO
+    const supportedLangs = ["en","es","fr"];
+    Vue.prototype.$lang = localStorage["lang"] ||
+      supportedLangs.includes(navigator.language)
+        ? navigator.language
+        : "en";
+               ajax("/variants", "GET", res => { Vue.prototype.$variants = res.variantArray; });
+    Vue.prototype.$tr = {}; //to avoid a compiler error
+               // TODO: if there is a socket ID in localStorage, it means a live game was interrupted (and should resume)
                const myid = localStorage["myid"] || util.getRandString();
                // NOTE: in this version, we don't say on which page we are, yet
                // ==> we'll say "enter/leave" page XY (in fact juste "enter", seemingly)
@@ -44,10 +65,4 @@ new Vue({
 // game : aussi systématique
 // problems: on-demand
 //
-// It works (to watch for route change), in a component:
-//watch: {
-//  $route: function(newRoute) {
-//    console.log(this.$route.params);
-//  },
-//},
 // See https://router.vuejs.org/guide/essentials/dynamic-matching.html#reacting-to-params-changes