فهرست منبع

Implement fallback function in LocalizedStrings for missing type/id strings

Lucas de Souza 3 سال پیش
والد
کامیت
1f6cd89423
1فایلهای تغییر یافته به همراه54 افزوده شده و 34 حذف شده
  1. 54 34
      LocalizedStrings.ts

+ 54 - 34
LocalizedStrings.ts

@@ -1,82 +1,99 @@
-import { LanguageService } from './LanguageService';
-import { StringTypes } from './StringTypes';
+import { LanguageService } from "./LanguageService";
+import { StringTypes } from "./StringTypes";
 
-type LangInfo = {[id: string]: string};
-type LangData = {[id: string]: LangInfo};
-type LangObject = {[id: string]: LangData}
+type LangInfo = { [id: string]: string };
+type LangData = { [id: string]: LangInfo };
+type LangObject = { [id: string]: LangData };
 
 export class LocalizedStrings {
-
   private document: Document = document;
 
-  constructor (private service: LanguageService, private i18nData: LangObject, private listenToChange = false) {
-    if(this.listenToChange) {
-      service.registerLanguageChangeListener( () => {
+  constructor(
+    private service: LanguageService,
+    private i18nData: LangObject,
+    private listenToChange = false
+  ) {
+    if (this.listenToChange) {
+      service.registerLanguageChangeListener(() => {
         this.updateTagText();
       });
     }
   }
 
-  getString (id: string, type: string): string {
+  getString(id: string, type: string): string {
     let i18nObj = this.i18nData[this.service.getLang()];
-    if (!!!i18nObj) {
-      console.warn(`Internal Error. The language set at ivprog.lang is not valid: ${this.service.getLang()}`);
-      i18nObj = this.i18nData[this.service.getDefaultLang()];
+    if (!i18nObj) {
+      console.warn(
+        `Internal Error. The language set at ivprog.lang is not valid: ${this.service.getLang()}`
+      );
+      return this.getDefaultString(id, type);
+    }
+    if (!i18nObj[type]) {
+      return this.getDefaultString(id, type);
+    } else if (!i18nObj[type][id]) {
+      return this.getDefaultString(id, type);
+    } else {
+      return i18nObj[type][id];
     }
-    if (!!!i18nObj[type]) {
-      return "{MISSING_I18N_TYPE_IDENTIFIER}";
-    } else if (!!!i18nObj[type][id]) {
-      return "{MISSING_I18N_IDENTIFIER}";
+  }
+
+  private getDefaultString(id: string, type: string): string {
+    let i18nObj = this.i18nData[this.service.getDefaultLang()];
+
+    if (!i18nObj[type]) {
+      return `{MISSING_I18N_TYPE_IDENTIFIER: ${type}}`;
+    } else if (!i18nObj[type][id]) {
+      return `{MISSING_I18N_IDENTIFIER: ${id}}`;
     } else {
       return i18nObj[type][id];
     }
   }
 
-  getOR (): String {
-    return this.getUI('string_join_or');
+  getOR(): String {
+    return this.getUI("string_join_or");
   }
 
-  getError (id: string, context: [] = []) {
+  getError(id: string, context: [] = []) {
     const text = this.getString(id, StringTypes.ERROR);
-    return this.processString(text, context)
+    return this.processString(text, context);
   }
 
-  getMessage (id: string, context: [] = []) {
+  getMessage(id: string, context: [] = []) {
     const text = this.getString(id, StringTypes.MESSAGE);
-    return this.processString(text, context)
+    return this.processString(text, context);
   }
 
-  getUI (id: string, context: [] = []) {
+  getUI(id: string, context: [] = []) {
     const text = this.getString(id, StringTypes.UI);
-    return this.processString(text, context)
+    return this.processString(text, context);
   }
 
-  processString (text: string, context: []) {
+  processString(text: string, context: []) {
     for (let i = 0; i < context.length; i++) {
       const v = context[i];
-      text = text.replace('\$' + i, v);
+      text = text.replace("$" + i, v);
     }
     return text;
   }
 
-  updateTagText (func: ((str: string | null) => string) | null = null): void {
+  updateTagText(func: ((str: string | null) => string) | null = null): void {
     if (this.document !== null) {
       const list = this.document.querySelectorAll("data.i18n");
-      list.forEach(node => {
+      list.forEach((node) => {
         if (func === null) {
           node.innerHTML = this.processTagTex(node.getAttribute("value"));
         } else {
           node.innerHTML = func(node.getAttribute("value"));
         }
-      })
+      });
     }
   }
 
-  processTagTex (text: string | null): string {
+  processTagTex(text: string | null): string {
     if (text === null) {
       return "<Invalid i18n identifier>";
     }
-    const opts = text.split(':');
+    const opts = text.split(":");
     const type = opts[0].toLowerCase();
     const id = opts[1];
     if (StringTypes.ERROR === type) {
@@ -86,9 +103,12 @@ export class LocalizedStrings {
     } else if (StringTypes.UI === type) {
       return this.getUI(id);
     } else {
-      console.warn(" A string has been passed to the i18n helper function that was not in the form type:id -> " + text);
+      console.warn(
+        " A string has been passed to the i18n helper function that was not in the form type:id -> " +
+          text
+      );
       return this.getString(id, type);
     }
   }
+}
 
-}