mapcomplete/UI/i18n/Translations.ts

101 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-07-21 00:07:04 +02:00
import {FixedUiElement} from "../Base/FixedUiElement";
2020-11-06 01:58:26 +01:00
import {Translation} from "./Translation";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
import * as known_languages from "../../assets/generated/used_languages.json"
import CompiledTranslations from "../../assets/generated/CompiledTranslations";
2020-07-20 12:39:43 +02:00
export default class Translations {
2020-07-21 23:31:41 +02:00
static t = CompiledTranslations.t;
private static knownLanguages = new Set(known_languages.languages)
2020-07-21 23:31:41 +02:00
constructor() {
throw "Translations is static. If you want to intitialize a new translation, use the singular form"
}
2021-06-10 01:36:20 +02:00
public static W(s: string | BaseUIElement): BaseUIElement {
if (typeof (s) === "string") {
return new FixedUiElement(s);
2020-07-20 12:39:43 +02:00
}
if (typeof s === "number") {
return new FixedUiElement("" + s)
}
return s;
}
2021-03-13 19:07:38 +01:00
static T(t: string | any, context = undefined): Translation {
if (t === undefined || t === null) {
2020-10-27 01:01:34 +01:00
return undefined;
}
2021-11-07 16:34:51 +01:00
if (typeof t === "number") {
t = "" + t
}
if (typeof t === "string") {
return new Translation({"*": t}, context);
2020-10-27 01:01:34 +01:00
}
if (t.render !== undefined) {
2020-10-27 01:01:34 +01:00
const msg = "Creating a translation, but this object contains a 'render'-field. Use the translation directly"
console.error(msg, t);
throw msg
}
if (t instanceof Translation) {
return t;
}
2021-03-13 19:07:38 +01:00
return new Translation(t, context);
2020-10-27 01:01:34 +01:00
}
public static WT(s: string | Translation): Translation {
if (s === undefined || s === null) {
2020-10-12 01:25:27 +02:00
return undefined;
}
if (typeof (s) === "string") {
return new Translation({'*': s});
}
if (s instanceof Translation) {
return s.Clone() /* MUST CLONE HERE! */;
}
console.error("Trying to Translation.WT, but got ", s)
throw "??? Not a valid translation"
2020-07-20 12:39:43 +02:00
}
2020-07-21 00:07:04 +02:00
public static CountTranslations() {
const queue: any = [Translations.t];
const tr: Translation[] = [];
while (queue.length > 0) {
const item = queue.pop();
if (item instanceof Translation || item.translations !== undefined) {
tr.push(item);
2020-07-31 17:38:03 +02:00
} else if (typeof (item) === "string") {
console.warn("Got single string in translationgs file: ", item);
} else {
for (const t in item) {
const x = item[t];
queue.push(x)
}
}
}
const langaugeCounts = {};
for (const translation of tr) {
for (const language in translation.translations) {
if (langaugeCounts[language] === undefined) {
langaugeCounts[language] = 1
} else {
langaugeCounts[language]++;
}
}
}
for (const language in langaugeCounts) {
console.log("Total translations in ", language, langaugeCounts[language], "/", tr.length)
}
}
static isProbablyATranslation(transl: any) {
if(typeof transl !== "object"){
return false;
}
// is a weird key found?
return !Object.keys(transl).some(key => !this.knownLanguages.has(key))
}
2020-09-15 18:44:58 +02:00
}