LocalizedStrings.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { LanguageService } from './LanguageService';
  2. import { StringTypes } from './StringTypes';
  3. type LangInfo = {[id: string]: string};
  4. type LangData = {[id: string]: LangInfo};
  5. type LangObject = {[id: string]: LangData}
  6. export class LocalizedStrings {
  7. private document: Document = document;
  8. constructor (private service: LanguageService, private i18nData: LangObject, private listenToChange = false) {
  9. if(this.listenToChange) {
  10. service.registerLanguageChangeListener( () => {
  11. this.updateTagText();
  12. });
  13. }
  14. }
  15. getString (id: string, type: string): string {
  16. let i18nObj = this.i18nData[this.service.getLang()];
  17. if (!!!i18nObj) {
  18. console.warn(`Internal Error. The language set at ivprog.lang is not valid: ${this.service.getLang()}`);
  19. i18nObj = this.i18nData[this.service.getDefaultLang()];
  20. }
  21. if (!!!i18nObj[type]) {
  22. return "{MISSING_I18N_TYPE_IDENTIFIER}";
  23. } else if (!!!i18nObj[type][id]) {
  24. return "{MISSING_I18N_IDENTIFIER}";
  25. } else {
  26. return i18nObj[type][id];
  27. }
  28. }
  29. getOR (): String {
  30. return this.getUI('string_join_or');
  31. }
  32. getError (id: string, context: [] = []) {
  33. const text = this.getString(id, StringTypes.ERROR);
  34. return this.processString(text, context)
  35. }
  36. getMessage (id: string, context: [] = []) {
  37. const text = this.getString(id, StringTypes.MESSAGE);
  38. return this.processString(text, context)
  39. }
  40. getUI (id: string, context: [] = []) {
  41. const text = this.getString(id, StringTypes.UI);
  42. return this.processString(text, context)
  43. }
  44. processString (text: string, context: []) {
  45. for (let i = 0; i < context.length; i++) {
  46. const v = context[i];
  47. text = text.replace('\$' + i, v);
  48. }
  49. return text;
  50. }
  51. updateTagText (func: ((str: string | null) => string) | null = null): void {
  52. if (this.document !== null) {
  53. const list = this.document.querySelectorAll("data.i18n");
  54. list.forEach(node => {
  55. if (func === null) {
  56. node.innerHTML = this.processTagTex(node.getAttribute("value"));
  57. } else {
  58. node.innerHTML = func(node.getAttribute("value"));
  59. }
  60. })
  61. }
  62. }
  63. processTagTex (text: string | null): string {
  64. if (text === null) {
  65. return "<Invalid i18n identifier>";
  66. }
  67. const opts = text.split(':');
  68. const type = opts[0].toLowerCase();
  69. const id = opts[1];
  70. if (StringTypes.ERROR === type) {
  71. return this.getError(id);
  72. } else if (StringTypes.MESSAGE === type) {
  73. return this.getMessage(id);
  74. } else if (StringTypes.UI === type) {
  75. return this.getUI(id);
  76. } else {
  77. console.warn(" A string has been passed to the i18n helper function that was not in the form type:id -> " + text);
  78. return this.getString(id, type);
  79. }
  80. }
  81. }