From 18f5141fd62492fe3782364a72531f412adc6a6a Mon Sep 17 00:00:00 2001
From: toto <toto@tata>
Date: Wed, 23 Nov 2022 11:20:12 +0100
Subject: [PATCH] feat: localise service worker messages

refs #445
---
 .../services/internationalisation.service.ts  | 36 ++++++++++++-------
 .../services/service-worker-update.service.ts | 18 +++++-----
 src/locale/messages.en.json                   |  3 ++
 src/locale/messages.fr.json                   |  3 ++
 4 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/src/app/services/internationalisation.service.ts b/src/app/services/internationalisation.service.ts
index 7dddda2a9..af0f2d42e 100644
--- a/src/app/services/internationalisation.service.ts
+++ b/src/app/services/internationalisation.service.ts
@@ -160,12 +160,12 @@ export class I18nService extends Observable implements Observer {
      *
      * @param textKey id du texte (ex: "ERROR_PARAM_NULL")
      */
-    public localizeText(textKey: string) {
+    public localizeText(textKey: string, vars: {} = {}) {
         if (! this._Messages) {
             return `*** messages not loaded: ${this._currentLanguage} ***`;
         }
         if (this._Messages[textKey] !== undefined) {
-            return decodeHtml(this._Messages[textKey]);
+            return this.translateMessage(this._Messages[textKey], vars);
         } else {
             // try general message
             if (this._Messages !== undefined && this._Messages["INFO_LIB_" + textKey.toUpperCase()] !== undefined) {
@@ -176,17 +176,16 @@ export class I18nService extends Observable implements Observer {
     }
 
     /**
-     * Traduit un Message (classe Message de JaLHyd, pour les logs de calcul par exemple)
-     * @param r Message
-     * @param nDigits nombre de chiffres à utiliser pour l'arrondi dans le cas de données numériques
+     * Translate a text optionally subtituting variables denoted by %XXX%
+     * @param m message to translate
+     * @param vars variable map
+     * @returns translated message with variables value
      */
-    public localizeMessage(r: Message, nDigits: number = 3): string {
-        let text: string;
-        let m: string = this.getMessageFromCode(r.code);
-        // replace %X% by formatted value of extraVar.X
-        for (const k in r.extraVar) {
-            if (r.extraVar.hasOwnProperty(k)) {
-                const v: any = r.extraVar[k];
+    private translateMessage(m: string, vars: {}) {
+        // replace %X% by formatted value of vars.X
+        for (const k in vars) {
+            if (vars.hasOwnProperty(k)) {
+                const v: any = vars[k];
                 let s: string;
                 // detect variable names to translate
                 if (k === "variables" && Array.isArray(v)) {
@@ -248,7 +247,18 @@ export class I18nService extends Observable implements Observer {
             return this.localizeText(p1);
         });
 
-        text = decodeHtml(m);
+        return decodeHtml(m);
+    }
+
+    /**
+     * Traduit un Message (classe Message de JaLHyd, pour les logs de calcul par exemple)
+     * @param r Message
+     * @param nDigits nombre de chiffres à utiliser pour l'arrondi dans le cas de données numériques
+     */
+    public localizeMessage(r: Message, nDigits: number = 3): string {
+        let m: string = this.getMessageFromCode(r.code);
+
+        let text: string = this.translateMessage(m, r.extraVar);
 
         // prefix message if needed
         if (r.parent && r.parent.parent && r.parent.parent.sourceNub) {
diff --git a/src/app/services/service-worker-update.service.ts b/src/app/services/service-worker-update.service.ts
index 0cc5205e4..96b84426a 100644
--- a/src/app/services/service-worker-update.service.ts
+++ b/src/app/services/service-worker-update.service.ts
@@ -1,32 +1,34 @@
 import { Injectable } from "@angular/core";
 import { SwUpdate } from '@angular/service-worker';
+import { I18nService } from "./internationalisation.service";
 import { NotificationsService } from "./notifications.service";
 
 @Injectable()
 export class ServiceWorkerUpdateService {
     constructor(
         private swUpdate: SwUpdate,
-        private notificationService: NotificationsService
+        private notificationService: NotificationsService,
+        private i18nService: I18nService
     ) {
         swUpdate.versionUpdates.subscribe(evt => {
             switch (evt.type) {
                 case 'VERSION_DETECTED':
                     let ver = evt.version.appData["version"];
-                    console.log(`new version detected ${ver}`);
-                    notificationService.notify(`Downloading new version: ${ver}`, 10000);
+                    let msg = i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_DETECTED", { "ver": ver });
+                    notificationService.notify(msg, 10000);
                     break;
 
                 case 'VERSION_READY':
-                    const currVer = evt.currentVersion.appData["version"];
                     const newVer = evt.latestVersion.appData["version"];
-                    console.log(`new version ready ${newVer}, current = ${currVer}`);
-                    notificationService.notify(`New version ready for use: ${newVer}, replacing ${currVer}`, 10000);
+                    // const currVer = evt.currentVersion.appData["version"];
+                    msg = i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer });
+                    notificationService.notify(msg, 10000);
                     break;
 
                 case 'VERSION_INSTALLATION_FAILED':
                     ver = evt.version.appData["version"];
-                    console.log(`version install failed ${ver} : ${evt.error}`);
-                    notificationService.notify(`Failed to install version '${ver}': ${evt.error}`, 10000);
+                    msg = i18nService.localizeText("ERROR_SERVICE_WORKER_INSTALL_FAILED", { "ver": ver });
+                    notificationService.notify(msg, 10000);
                     break;
             }
         });
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index 5171fdefc..4e7499a2b 100755
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -670,6 +670,9 @@
     "INFO_ESPECE_TITRE": "Fish species characteristics",
     "INFO_ESPECE_DESCRIPTION": "ichtyocompatible",
     "INFO_ESPECE_TITRE_COURT": "Species",
+    "INFO_SERVICE_WORKER_VERSION_DETECTED": "Downloading Cassiopée version %ver%...",
+    "INFO_SERVICE_WORKER_VERSION_READY": "Cassiopée version %ver% is ready to be used, please restart.",
+    "ERROR_SERVICE_WORKER_INSTALL_FAILED": "Cassiopée version %ver% installation failed.",
     "ERROR_JET_SUBMERGED_NO_SOLUTION": "There is no solution",
     "WARNING_DEVER_ZDV_INF_ZR": "Apron elevation of structure #%number% is below river bed elevation",
     "WARNING_JET_START_SUBMERGED": "Water elevation is greater than jet start elevation",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index d41d2edd4..9aa28d5f1 100755
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -671,6 +671,9 @@
     "INFO_ESPECE_TITRE": "Caractéristiques d'une espèce",
     "INFO_ESPECE_DESCRIPTION": "ichtyocompatible",
     "INFO_ESPECE_TITRE_COURT": "Espèce",
+    "INFO_SERVICE_WORKER_VERSION_DETECTED": "Téléchargement de la version %ver% de Cassiopée en cours...",
+    "INFO_SERVICE_WORKER_VERSION_READY": "La version %ver% de Cassiopée est prête à être utilisée, veuillez redémarrer.",
+    "ERROR_SERVICE_WORKER_INSTALL_FAILED": "Erreur d'installation de Cassiopée version %ver%.",
     "ERROR_JET_SUBMERGED_NO_SOLUTION": "Il n'y a pas de solution",
     "WARNING_DEVER_ZDV_INF_ZR": "La cote de radier de l'ouvrage n°%number% est sous la cote de fond du lit",
     "WARNING_JET_START_SUBMERGED": "La cote de l'eau est supérieure à la cote de départ du jet",
-- 
GitLab