From 0aad8930b407ff4a63cf0265845d742a9e3b1770 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sun, 19 May 2019 11:57:05 +0200
Subject: [PATCH] [enh] Parameters > Language: new view to select prefered
 interface language

---
 src/lib/constants.ts                   |  5 ++-
 src/lib/i18n/en.ts                     | 28 ++++++++++++++++
 src/lib/i18n/fr.ts                     | 28 ++++++++++++++++
 src/lib/i18n/lang.ts                   | 26 +++++++++++++++
 src/lib/messages.ts                    | 23 -------------
 src/views/Home.vue                     |  6 +++-
 src/views/home/Parameters.vue          | 46 ++++++++++++++++++++++++++
 src/views/home/parameters/Language.vue | 35 ++++++++++++++++++++
 src/vue-modules/register-i18n.ts       | 27 +++++++++++++--
 src/vue-modules/register-router.ts     | 22 ++++++++++++
 10 files changed, 218 insertions(+), 28 deletions(-)
 create mode 100644 src/lib/i18n/en.ts
 create mode 100644 src/lib/i18n/fr.ts
 create mode 100644 src/lib/i18n/lang.ts
 delete mode 100644 src/lib/messages.ts
 create mode 100644 src/views/home/Parameters.vue
 create mode 100644 src/views/home/parameters/Language.vue

diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index 480f424..21b13d2 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -1,5 +1,8 @@
 export const Constants = {
-  API_URL: process.env.VUE_APP_REMOTE
+  API_URL: process.env.VUE_APP_REMOTE,
+  STORAGE_LANGUAGE: 'LANG',
+  DEFAULT_LANGUAGE: 'en',
+  FALLBACK_LANGUAGE: 'en',
 }
 
 export const RouteNames = {
diff --git a/src/lib/i18n/en.ts b/src/lib/i18n/en.ts
new file mode 100644
index 0000000..35390b0
--- /dev/null
+++ b/src/lib/i18n/en.ts
@@ -0,0 +1,28 @@
+import {lang} from '@/lib/i18n/lang'
+
+export const i18nLangEn: lang = {
+  'app.loading': 'Loading...',
+  'init.title': 'Duniter node ready for sync.',
+  'init.node.select': 'Please select a node from which starting the sync:',
+  'init.message': 'This node looks like a fresh new install. You need to synchronize it with a Duniter currency network to continue.',
+  'init.node.g1.currency': 'Ğ1 currency',
+  'init.node.gt.currency': 'Ğ1-Test currency',
+  'init.sync.start': 'Synchronize',
+  'init.failed.no_node_found.title': 'Synchronization failed',
+  'init.failed.no_node_found.message': 'No node was able to give the next chunk of blocks.',
+  'init.failed.cannot_connect_to_remote.title': 'Synchronization failed',
+  'init.failed.cannot_connect_to_remote.message': 'Cannot connect to selected node.',
+  'sync.milestones': 'Milestones',
+  'sync.download': 'Download',
+  'sync.apply': 'Apply',
+  'sync.sandboxes': 'Sandboxes',
+  'sync.peers': 'Peers',
+  'menu.item.node': 'Node',
+  'menu.item.node.start': 'Start',
+  'menu.item.node.reset': 'Reset',
+  'parameters.menu.conf': 'Configuration file',
+  'parameters.menu.network': 'Network',
+  'parameters.conf.file.explanation': 'This is your configuration file. You can directly edit it from here if you wish.',
+  'fork.view.too.much': 'Too much blocks requested: max is {0}.',
+  'parameters.menu.lang': 'Language',
+}
\ No newline at end of file
diff --git a/src/lib/i18n/fr.ts b/src/lib/i18n/fr.ts
new file mode 100644
index 0000000..d40c24e
--- /dev/null
+++ b/src/lib/i18n/fr.ts
@@ -0,0 +1,28 @@
+import {lang} from '@/lib/i18n/lang'
+
+export const i18nLangFr: lang = {
+  'app.loading': 'Chargement de l\'application...',
+  'init.title': 'Nœud Duniter prêt pour la synchronisation.',
+  'init.node.select': 'Sélectionner un nœud à partir duquel synchroniser :',
+  'init.message': 'Ce nœud semble tout neuf. Vous devez le synchroniser pour continuer.',
+  'init.node.g1.currency': 'Monnaie Ğ1',
+  'init.node.gt.currency': 'Monnaie ĞTest',
+  'init.sync.start': 'Synchroniser',
+  'init.failed.no_node_found.title': 'La synchronisation a échoué',
+  'init.failed.no_node_found.message': 'Aucun nœud n\'a pu transmettre la prochaine tranche de blocs.',
+  'init.failed.cannot_connect_to_remote.title': 'La synchronisation a échoué',
+  'init.failed.cannot_connect_to_remote.message': 'Connexion au nœud sélectionnné impossible.',
+  'sync.milestones': 'Jalons',
+  'sync.download': 'Téléchargement',
+  'sync.apply': 'Application',
+  'sync.sandboxes': 'Piscines',
+  'sync.peers': 'Pairs',
+  'menu.item.node': 'Nœud',
+  'menu.item.node.start': 'Démarrer',
+  'menu.item.node.reset': 'Réinitialiser',
+  'parameters.menu.conf': 'Fichier de configuration',
+  'parameters.menu.network': 'Réseau',
+  'parameters.conf.file.explanation': 'Ceci est votre fichier de configuration. Vous pouvez le modifier directement ici si vous le souhaitez.',
+  'fork.view.too.much': 'Trop de blocs demandés : le maximum est de {0}.',
+  'parameters.menu.lang': 'Langage',
+}
\ No newline at end of file
diff --git a/src/lib/i18n/lang.ts b/src/lib/i18n/lang.ts
new file mode 100644
index 0000000..de0c9a0
--- /dev/null
+++ b/src/lib/i18n/lang.ts
@@ -0,0 +1,26 @@
+export interface lang {
+  'app.loading': string
+  'init.title': string
+  'init.node.select': string
+  'init.message': string
+  'init.node.g1.currency': string
+  'init.node.gt.currency': string
+  'init.sync.start': string
+  'init.failed.no_node_found.title': string
+  'init.failed.no_node_found.message': string
+  'init.failed.cannot_connect_to_remote.title': string
+  'init.failed.cannot_connect_to_remote.message': string
+  'sync.milestones': string
+  'sync.download': string
+  'sync.apply': string
+  'sync.sandboxes': string
+  'sync.peers': string
+  'menu.item.node': string
+  'menu.item.node.start': string
+  'menu.item.node.reset': string
+  'parameters.menu.conf': string
+  'parameters.menu.network': string
+  'parameters.conf.file.explanation': string
+  'fork.view.too.much': string
+  'parameters.menu.lang': string
+}
diff --git a/src/lib/messages.ts b/src/lib/messages.ts
deleted file mode 100644
index 8361689..0000000
--- a/src/lib/messages.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-export const i18nMessages = {
-  en: {
-    'app.loading': 'Loading...',
-    'init.title': 'Duniter node ready for sync.',
-    'init.node.select': 'Please select a node from which starting the sync:',
-    'init.message': 'This node looks like a fresh new install. You need to synchronize it with a Duniter currency network to continue.',
-    'init.node.g1.currency': 'Ğ1 currency',
-    'init.node.gt.currency': 'Ğ1-Test currency',
-    'init.sync.start': 'Synchronize',
-    'init.failed.no_node_found.title': 'Synchronization failed',
-    'init.failed.no_node_found.message': 'No node was able to give the next chunk of blocks.',
-    'init.failed.cannot_connect_to_remote.title': 'Synchronization failed',
-    'init.failed.cannot_connect_to_remote.message': 'Cannot connect to selected node.',
-    'sync.milestones': 'Milestones',
-    'sync.download': 'Download',
-    'sync.apply': 'Apply',
-    'sync.sandboxes': 'Sandboxes',
-    'sync.peers': 'Peers',
-    'menu.item.node': 'Node',
-    'menu.item.node.start': 'Start',
-    'menu.item.node.reset': 'Reset'
-  }
-}
diff --git a/src/views/Home.vue b/src/views/Home.vue
index ab4746d..571ed50 100644
--- a/src/views/Home.vue
+++ b/src/views/Home.vue
@@ -43,7 +43,11 @@
 
           </b-nav-item>
 
-          <b-nav-item><button class="btn" routerLink="params"><font-awesome-icon icon="cog"/></button></b-nav-item>
+          <b-nav-item>
+            <router-link to="/home/parameters">
+              <button class="btn"><font-awesome-icon icon="cog"/></button>
+            </router-link>
+          </b-nav-item>
         </b-navbar-nav>
 
       </b-collapse>
diff --git a/src/views/home/Parameters.vue b/src/views/home/Parameters.vue
new file mode 100644
index 0000000..d64a080
--- /dev/null
+++ b/src/views/home/Parameters.vue
@@ -0,0 +1,46 @@
+<style lang="css" scoped>
+</style>
+
+<template>
+
+  <div class="container-fluid m-0 p-0">
+
+    <div class="row mt-2">
+      <div class="col-2">
+        <div class="nav flex-column nav-pills">
+
+          <router-link :to="menu.route" v-for="menu in menus" :key="menu.route">
+            <span class="nav-link" :class="{ 'active': currentRoute === menu.route }">{{ $t(menu.name )}}</span>
+          </router-link>
+        </div>
+      </div>
+
+      <div class="col-10">
+
+        <router-view></router-view>
+
+      </div>
+
+    </div>
+
+  </div>
+
+</template>
+
+<script lang="ts">
+  import {Component, Vue} from 'vue-property-decorator';
+
+  @Component({
+    components: {},
+  })
+  export default class extends Vue {
+
+    menus = [
+      { route: '/home/parameters/lang', name: 'parameters.menu.lang' },
+    ]
+
+    get currentRoute() {
+      return this.$route.path
+    }
+  }
+</script>
diff --git a/src/views/home/parameters/Language.vue b/src/views/home/parameters/Language.vue
new file mode 100644
index 0000000..c70a8aa
--- /dev/null
+++ b/src/views/home/parameters/Language.vue
@@ -0,0 +1,35 @@
+<style lang="css" scoped>
+</style>
+
+<template>
+
+  <div>
+    <select v-model="lang" @change="changeLocale">
+      <option value="en">English</option>
+      <option value="fr">Français</option>
+    </select>
+  </div>
+
+</template>
+
+<script lang="ts">
+  import {Component, Vue} from 'vue-property-decorator';
+  import {Constants} from "@/lib/constants";
+
+  @Component({
+    components: {},
+  })
+  export default class extends Vue {
+
+    lang: string = ''
+
+    mounted() {
+      this.lang = this.$i18n.locale
+    }
+
+    changeLocale() {
+      this.$i18n.locale = this.lang
+      localStorage.setItem(Constants.STORAGE_LANGUAGE, this.lang)
+    }
+  }
+</script>
diff --git a/src/vue-modules/register-i18n.ts b/src/vue-modules/register-i18n.ts
index 3dc0f3f..da062fb 100644
--- a/src/vue-modules/register-i18n.ts
+++ b/src/vue-modules/register-i18n.ts
@@ -1,11 +1,32 @@
 import Vue from 'vue'
 import VueI18n from 'vue-i18n'
-import {i18nMessages} from '@/lib/messages'
+import {i18nLangEn} from '@/lib/i18n/en'
+import {i18nLangFr} from '@/lib/i18n/fr'
+import {Constants} from '@/lib/constants'
+
+export const i18nMessages = {
+  en: (i18nLangEn as any),
+  fr: (i18nLangFr as any),
+}
+
+let browserLanguage = navigator.language || (navigator as any).userLanguage
+
+if (browserLanguage) {
+  if (browserLanguage.match(/fr-FR/)) {
+    browserLanguage = 'fr'
+  }
+  else if (browserLanguage.match(/en-.*/)) {
+    browserLanguage = 'en'
+  }
+  else {
+    browserLanguage = Constants.DEFAULT_LANGUAGE
+  }
+}
 
 Vue.use(VueI18n)
 
 export default new VueI18n({
-  locale: 'en',
-  fallbackLocale: 'en',
+  locale: localStorage.getItem(Constants.STORAGE_LANGUAGE) || browserLanguage,
+  fallbackLocale: Constants.FALLBACK_LANGUAGE,
   messages: i18nMessages
 })
\ No newline at end of file
diff --git a/src/vue-modules/register-router.ts b/src/vue-modules/register-router.ts
index 57f1e3b..af58b7c 100644
--- a/src/vue-modules/register-router.ts
+++ b/src/vue-modules/register-router.ts
@@ -5,7 +5,9 @@ import Home from '@/views/Home.vue'
 import Loader from '@/views/Loader.vue'
 import Sync from '@/views/Sync.vue'
 import Overview from '@/views/home/Overview.vue'
+import Parameters from '@/views/home/Parameters.vue'
 import Forkview from '@/views/home/Forkview.vue'
+import Language from '@/views/home/parameters/Language.vue'
 
 Vue.use(Router)
 
@@ -35,8 +37,28 @@ export default new Router({
         path: 'forkview',
         name: 'forkview',
         component: Forkview
+      },{
+        path: 'parameters',
+        name: 'parameters',
+        component: Parameters,
+        children: [{
+          path: 'lang',
+          name: 'lang',
+          component: Language
+        }]
       }]
     },
+    /*
+    {
+          path: '',
+          name: 'conf',
+          component: Conf
+        },{
+          path: 'network',
+          name: 'network',
+          component: Network
+        },
+     */
     {
       path: '/about',
       name: 'about',
-- 
GitLab