Просмотр исходного кода

Refactor the i18n service to use our own package

Lucas de Souza 6 лет назад
Родитель
Сommit
ffc62821a1
4 измененных файлов с 26 добавлено и 75 удалено
  1. 18 25
      js/services/languageService.js
  2. 2 49
      js/services/localizedStringsService.js
  3. 4 0
      package-lock.json
  4. 2 1
      package.json

+ 18 - 25
js/services/languageService.js

@@ -1,46 +1,39 @@
 import Lexers from './../../grammar/';
+import line_i18n from 'line-i18n';
 
 const DEFAULT_LANG = "pt";
 
-export const LanguageService  = Object.freeze({
+class LanguageServiceExtended extends line_i18n.LanguageService {
 
-  getLang: () => {
-    const lang = localStorage.getItem('ivprog.lang');
-    if (lang === null || lang === undefined) {
-      console.warn("Internal Error. User language information has not been set. Returning default...");
-      return LanguageService.getDefaultLang();
-    }
-    return lang;
-  },
-
-  getDefaultLang: () => {
-    return DEFAULT_LANG;
-  },
+  constructor () {
+    super("ivprog.lang", DEFAULT_LANG);
+  }
 
-  getCurrentLexer: () => {
-    const langInfo = Lexers[LanguageService.getLang()];
+  getCurrentLexer () {
+    const langInfo = Lexers[this.getLang()];
     if(langInfo === null || langInfo === undefined) {
-      return Lexers[DEFAULT_LANG].lexer;
+      return Lexers[this.getDefaultLang()].lexer;
     } else {
       return langInfo.lexer;
     }
-  },
+  }
 
-  getCurrentLangFuncs: () => {
-    const langInfo = Lexers[LanguageService.getLang()];
+  getCurrentLangFuncs () {
+    const langInfo = Lexers[this.getLang()];
     if(langInfo === null || langInfo === undefined) {
-      return Lexers[DEFAULT_LANG].langFuncs;
+      return Lexers[this.getDefaultLang()].langFuncs;
     } else {
       return langInfo.langFuncs;
     }
-  },
+  }
 
-  getCurrentLangLibs: () => {
-    const langInfo = Lexers[LanguageService.getLang()];
+  getCurrentLangLibs () {
+    const langInfo = Lexers[this.getLang()];
     if(langInfo === null || langInfo === undefined) {
-      return Lexers[DEFAULT_LANG].langLibs;
+      return Lexers[this.getDefaultLang()].langLibs;
     }
     return langInfo.langLibs;
   }
+}
 
-});
+export const LanguageService  = Object.freeze(new LanguageServiceExtended());

+ 2 - 49
js/services/localizedStringsService.js

@@ -1,52 +1,5 @@
 import { LanguageService } from "./languageService";
+import line_i18n from 'line-i18n'
 import Langs from './../../i18n';
 
-export const StringTypes = Object.freeze({
-  ERROR: "error",
-  MESSAGE: "message",
-  UI: "ui"
-});
-
-export const LocalizedStrings = Object.freeze({
-
-  getString: (id, type) => {
-    let i18nObj = Langs[LanguageService.getLang()];
-    if(!!!i18nObj) {
-      console.warn(`Internal Error. The language set at ivprog.lang is not valid: ${LanguageService.getLang()}`);
-      i18nObj = Langs[LanguageService.getDefaultLang()];
-    }
-    if(!!!i18nObj[type]) {
-      return "{MISSING_I18N_TYPE_IDENTIFIER}";
-    } else if (!!!i18nObj[type][id]) {
-      return "{MISSING_I18N_IDENTIFIER}";
-    } else {
-      return i18nObj[type][id];
-    }
-  },
-
-  getOR: () => LocalizedStrings.getUI('join_or'),
-
-  getError: (id, context = []) => {
-    const text = LocalizedStrings.getString(id, StringTypes.ERROR);
-    return LocalizedStrings.processString(text, context)
-  },
-
-  getMessage: (id, context = []) => {
-    const text = LocalizedStrings.getString(id, StringTypes.MESSAGE);
-    return LocalizedStrings.processString(text, context)
-  },
-
-  getUI: (id, context = []) => {
-    const text = LocalizedStrings.getString(id, StringTypes.UI);
-    return LocalizedStrings.processString(text, context)
-  },
-
-  processString: (text, context) => {
-    for (let i = 0; i < context.length; i++) {
-      const v = context[i];
-      text = text.replace('\$'+i, v);
-    }
-    return text;
-  }
-
-});
+export const LocalizedStrings = Object.freeze(new line_i18n.LocalizedStrings(LanguageService, Langs, true));

+ 4 - 0
package-lock.json

@@ -5072,6 +5072,10 @@
         "invert-kv": "^1.0.0"
       }
     },
+    "line-i18n": {
+      "version": "git+https://git.lcalion.com/lucascalion/line-i18n.git#d62cb2d82d27761758fe59376d22f4f5b191b51a",
+      "from": "git+https://git.lcalion.com/lucascalion/line-i18n.git"
+    },
     "load-json-file": {
       "version": "1.1.0",
       "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz",

+ 2 - 1
package.json

@@ -47,6 +47,7 @@
     "jquery": "^3.3.1",
     "melanke-watchjs": "^1.5.0",
     "server": "^1.0.18",
-    "bignumber.js": "^7.2.1"
+    "bignumber.js": "^7.2.1",
+    "line-i18n": "git+https://git.lcalion.com/lucascalion/line-i18n.git"
   }
 }