2020-07-24 15:52:21 +02:00
|
|
|
import {UIElement} from "../UIElement"
|
2020-07-20 12:39:43 +02:00
|
|
|
import Locale from "./Locale"
|
2020-07-24 15:52:21 +02:00
|
|
|
import Combine from "../Base/Combine";
|
2020-07-20 12:39:43 +02:00
|
|
|
|
|
|
|
|
2020-07-21 00:07:04 +02:00
|
|
|
export default class Translation extends UIElement {
|
2020-07-24 15:52:21 +02:00
|
|
|
|
|
|
|
private static forcedLanguage = undefined;
|
|
|
|
|
2020-07-31 16:17:16 +02:00
|
|
|
public Subs(text: any) {
|
2020-07-24 15:52:21 +02:00
|
|
|
const newTranslations = {};
|
|
|
|
for (const lang in this.translations) {
|
|
|
|
let template: string = this.translations[lang];
|
|
|
|
for (const k in text) {
|
|
|
|
const combined = [];
|
|
|
|
const parts = template.split("{" + k + "}");
|
|
|
|
const el: string | UIElement = text[k];
|
2020-08-30 01:13:18 +02:00
|
|
|
if(el === undefined){
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-24 15:52:21 +02:00
|
|
|
let rtext: string = "";
|
|
|
|
if (typeof (el) === "string") {
|
|
|
|
rtext = el;
|
|
|
|
} else {
|
|
|
|
Translation.forcedLanguage = lang; // This is a very dirty hack - it'll bite me one day
|
|
|
|
rtext = el.InnerRender();
|
2020-08-22 02:12:46 +02:00
|
|
|
}
|
2020-07-24 15:52:21 +02:00
|
|
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
|
|
combined.push(parts[i]);
|
|
|
|
combined.push(rtext)
|
|
|
|
}
|
|
|
|
combined.push(parts[parts.length - 1]);
|
|
|
|
template = new Combine(combined).InnerRender();
|
|
|
|
}
|
|
|
|
newTranslations[lang] = template;
|
|
|
|
}
|
|
|
|
Translation.forcedLanguage = undefined;
|
|
|
|
return new Translation(newTranslations);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-20 23:43:42 +02:00
|
|
|
get txt(): string {
|
2020-07-24 15:52:21 +02:00
|
|
|
const txt = this.translations[Translation.forcedLanguage ?? Locale.language.data];
|
2020-07-21 01:13:51 +02:00
|
|
|
if (txt !== undefined) {
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
const en = this.translations["en"];
|
2020-07-21 15:18:11 +02:00
|
|
|
if (en !== undefined) {
|
|
|
|
return en;
|
|
|
|
}
|
|
|
|
for (const i in this.translations) {
|
|
|
|
return this.translations[i]; // Return a random language
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
console.log("Missing language ",Locale.language.data,"for",this.translations)
|
2020-07-21 15:18:11 +02:00
|
|
|
return "Missing translation"
|
2020-07-20 12:39:43 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 01:13:51 +02:00
|
|
|
InnerRender(): string {
|
2020-07-20 23:43:42 +02:00
|
|
|
return this.txt
|
|
|
|
}
|
2020-07-20 12:39:43 +02:00
|
|
|
|
|
|
|
public readonly translations: object
|
2020-07-21 00:07:04 +02:00
|
|
|
|
2020-07-20 12:39:43 +02:00
|
|
|
constructor(translations: object) {
|
|
|
|
super(Locale.language)
|
2020-08-30 01:13:18 +02:00
|
|
|
let count = 0;
|
|
|
|
for (const translationsKey in translations) {
|
|
|
|
count++;
|
|
|
|
}
|
2020-07-20 12:39:43 +02:00
|
|
|
this.translations = translations
|
|
|
|
}
|
2020-07-21 00:07:04 +02:00
|
|
|
|
2020-08-22 17:33:08 +02:00
|
|
|
public replace(a: string, b: string) {
|
|
|
|
if(a.startsWith("{") && a.endsWith("}")){
|
|
|
|
a = a.substr(1, a.length - 2);
|
|
|
|
}
|
|
|
|
const result= this.Subs({[a]: b});
|
|
|
|
return result;
|
2020-08-22 02:12:46 +02:00
|
|
|
}
|
2020-07-21 01:13:51 +02:00
|
|
|
|
2020-07-21 00:07:04 +02:00
|
|
|
public R(): string {
|
|
|
|
return new Translation(this.translations).Render();
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:52:21 +02:00
|
|
|
public Clone() {
|
2020-07-22 00:18:07 +02:00
|
|
|
return new Translation(this.translations)
|
|
|
|
}
|
2020-07-24 15:52:21 +02:00
|
|
|
|
2020-08-22 02:12:46 +02:00
|
|
|
|
2020-07-20 12:39:43 +02:00
|
|
|
}
|