mapcomplete/UI/i18n/Translation.ts

239 lines
8 KiB
TypeScript
Raw Normal View History

2020-11-06 01:58:26 +01:00
import Locale from "./Locale";
import {Utils} from "../../Utils";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
2020-11-06 01:58:26 +01:00
2021-06-10 01:36:20 +02:00
export class Translation extends BaseUIElement {
2020-11-06 01:58:26 +01:00
public static forcedLanguage = undefined;
2020-11-06 01:58:26 +01:00
2020-11-11 16:23:49 +01:00
public readonly translations: object
2022-01-26 21:40:38 +01:00
2020-11-11 16:23:49 +01:00
constructor(translations: object, context?: string) {
2021-06-10 01:36:20 +02:00
super()
if (translations === undefined) {
2020-11-11 16:23:49 +01:00
throw `Translation without content (${context})`
}
2022-01-26 21:40:38 +01:00
if (typeof translations === "string") {
2021-12-21 18:35:31 +01:00
translations = {"*": translations};
}
2020-11-11 16:23:49 +01:00
let count = 0;
for (const translationsKey in translations) {
2021-06-10 01:36:20 +02:00
if (!translations.hasOwnProperty(translationsKey)) {
continue
}
2020-11-11 16:23:49 +01:00
count++;
2021-04-11 19:21:41 +02:00
if (typeof (translations[translationsKey]) != "string") {
2021-04-23 17:22:01 +02:00
console.error("Non-string object in translation: ", translations[translationsKey])
2021-04-11 19:21:41 +02:00
throw "Error in an object depicting a translation: a non-string object was found. (" + context + ")\n You probably put some other section accidentally in the translation"
2021-04-10 03:50:44 +02:00
}
2020-11-11 16:23:49 +01:00
}
this.translations = translations;
if (count === 0) {
2020-11-11 16:23:49 +01:00
throw `No translations given in the object (${context})`
}
}
get txt(): string {
return this.textFor(Translation.forcedLanguage ?? Locale.language.data)
2021-06-14 02:39:23 +02:00
}
2021-11-07 16:34:51 +01:00
static ExtractAllTranslationsFrom(object: any, context = ""): { context: string, tr: Translation }[] {
const allTranslations: { context: string, tr: Translation }[] = []
for (const key in object) {
const v = object[key]
if (v === undefined || v === null) {
continue
}
if (v instanceof Translation) {
allTranslations.push({context: context + "." + key, tr: v})
continue
}
if (typeof v === "object") {
allTranslations.push(...Translation.ExtractAllTranslationsFrom(v, context + "." + key))
}
}
return allTranslations
}
static fromMap(transl: Map<string, string>) {
const translations = {}
let hasTranslation = false;
transl?.forEach((value, key) => {
translations[key] = value
hasTranslation = true
})
if (!hasTranslation) {
return undefined
}
return new Translation(translations);
}
2022-01-26 21:40:38 +01:00
Destroy() {
super.Destroy();
this.isDestroyed = true;
}
public textFor(language: string): string {
if (this.translations["*"]) {
return this.translations["*"];
}
2021-06-14 02:39:23 +02:00
const txt = this.translations[language];
if (txt !== undefined) {
return txt;
}
const en = this.translations["en"];
if (en !== undefined) {
return en;
}
for (const i in this.translations) {
2021-06-10 01:36:20 +02:00
if (!this.translations.hasOwnProperty(i)) {
continue;
}
return this.translations[i]; // Return a random language
}
console.error("Missing language ", Locale.language.data, "for", this.translations)
return "";
}
2021-06-10 01:36:20 +02:00
InnerConstructElement(): HTMLElement {
const el = document.createElement("span")
2022-01-06 18:51:52 +01:00
const self = this
2021-06-10 01:36:20 +02:00
Locale.language.addCallbackAndRun(_ => {
2022-01-26 21:40:38 +01:00
if (self.isDestroyed) {
2022-01-06 18:51:52 +01:00
return true
}
2021-06-10 01:36:20 +02:00
el.innerHTML = this.txt
})
return el;
}
public SupportedLanguages(): string[] {
const langs = []
for (const translationsKey in this.translations) {
2021-06-10 01:36:20 +02:00
if (!this.translations.hasOwnProperty(translationsKey)) {
continue;
}
if (translationsKey === "#") {
continue;
}
if (!this.translations.hasOwnProperty(translationsKey)) {
2021-06-08 18:54:29 +02:00
continue
}
langs.push(translationsKey)
}
return langs;
}
2022-01-26 21:40:38 +01:00
public AllValues(): string[] {
return this.SupportedLanguages().map(lng => this.translations[lng]);
}
2020-11-11 16:23:49 +01:00
2020-11-06 01:58:26 +01:00
public Subs(text: any): Translation {
const newTranslations = {};
for (const lang in this.translations) {
2021-06-10 01:36:20 +02:00
if (!this.translations.hasOwnProperty(lang)) {
continue;
}
2020-11-06 01:58:26 +01:00
let template: string = this.translations[lang];
newTranslations[lang] = Utils.SubstituteKeys(template, text, lang);
2020-11-06 01:58:26 +01:00
}
return new Translation(newTranslations);
}
/**
*
* Given a translation such as `{en: "How much of bicycle_types are rented here}` (which is this translation)
* and a translation object `{ en: "electrical bikes" }`, plus the translation specification `bicycle_types`, will return
* a new translation:
* `{en: "How much electrical bikes are rented here?"}`
*
* @param translationObject
* @param stringToReplace
* @constructor
*/
public Fuse(translationObject: Translation, stringToReplace: string): Translation{
const translations = this.translations
const newTranslations = {}
for (const lang in translations) {
const target = translationObject.textFor(lang)
if(target === undefined){
continue
}
if(typeof target !== "string"){
throw "Invalid object in Translation.fuse: translationObject['"+lang+"'] is not a string, it is: "+JSON.stringify(target)
}
newTranslations[lang] = this.translations[lang].replaceAll(stringToReplace, target)
}
return new Translation(newTranslations)
}
2020-11-06 01:58:26 +01:00
public replace(a: string, b: string) {
if (a.startsWith("{") && a.endsWith("}")) {
a = a.substr(1, a.length - 2);
}
2021-06-10 01:36:20 +02:00
return this.Subs({[a]: b});
2020-11-06 01:58:26 +01:00
}
public Clone() {
return new Translation(this.translations)
}
FirstSentence() {
const tr = {};
for (const lng in this.translations) {
2021-06-10 01:36:20 +02:00
if (!this.translations.hasOwnProperty(lng)) {
continue
}
2020-11-06 01:58:26 +01:00
let txt = this.translations[lng];
txt = txt.replace(/\..*/, "");
txt = Utils.EllipsesAfter(txt, 255);
tr[lng] = txt;
}
return new Translation(tr);
}
public ExtractImages(isIcon = false): string[] {
const allIcons: string[] = []
for (const key in this.translations) {
2021-06-10 01:36:20 +02:00
if (!this.translations.hasOwnProperty(key)) {
continue;
}
const render = this.translations[key]
if (isIcon) {
const icons = render.split(";").filter(part => part.match(/(\.svg|\.png|\.jpg)$/) != null)
allIcons.push(...icons)
} else if (!Utils.runningFromConsole) {
// This might be a tagrendering containing some img as html
const htmlElement = document.createElement("div")
htmlElement.innerHTML = render
const images = Array.from(htmlElement.getElementsByTagName("img")).map(img => img.src)
allIcons.push(...images)
} else {
// We are running this in ts-node (~= nodejs), and can not access document
// So, we fallback to simple regex
2021-04-10 03:50:44 +02:00
try {
const matches = render.match(/<img[^>]+>/g)
if (matches != null) {
const sources = matches.map(img => img.match(/src=("[^"]+"|'[^']+'|[^/ ]+)/))
.filter(match => match != null)
.map(match => match[1].trim().replace(/^['"]/, '').replace(/['"]$/, ''));
allIcons.push(...sources)
}
2021-04-11 19:21:41 +02:00
} catch (e) {
2021-04-10 03:50:44 +02:00
console.error("Could not search for images: ", render, this.txt)
throw e
}
}
}
return allIcons.filter(icon => icon != undefined)
}
2022-01-26 21:40:38 +01:00
2021-11-08 02:36:01 +01:00
AsMarkdown(): string {
return this.txt
}
2020-11-06 01:58:26 +01:00
}