123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { LanguageService } from "./LanguageService";
- import { StringTypes } from "./StringTypes";
- 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(() => {
- this.updateTagText();
- });
- }
- }
- 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()}`
- );
- 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];
- }
- }
- 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");
- }
- getError(id: string, context: [] = []) {
- const text = this.getString(id, StringTypes.ERROR);
- return this.processString(text, context);
- }
- getMessage(id: string, context: [] = []) {
- const text = this.getString(id, StringTypes.MESSAGE);
- return this.processString(text, context);
- }
- getUI(id: string, context: [] = []) {
- const text = this.getString(id, StringTypes.UI);
- return this.processString(text, context);
- }
- processString(text: string, context: []) {
- for (let i = 0; i < context.length; i++) {
- const v = context[i];
- text = text.replace("$" + i, v);
- }
- return text;
- }
- updateTagText(func: ((str: string | null) => string) | null = null): void {
- if (this.document !== null) {
- const list = this.document.querySelectorAll("data.i18n");
- 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 {
- if (text === null) {
- return "<Invalid i18n identifier>";
- }
- const opts = text.split(":");
- const type = opts[0].toLowerCase();
- const id = opts[1];
- if (StringTypes.ERROR === type) {
- return this.getError(id);
- } else if (StringTypes.MESSAGE === type) {
- return this.getMessage(id);
- } 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
- );
- return this.getString(id, type);
- }
- }
- }
|