mapcomplete/UI/i18n/Translations.ts

134 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { FixedUiElement } from "../Base/FixedUiElement"
import { Translation, TypedTranslation } from "./Translation"
import BaseUIElement from "../BaseUIElement"
import CompiledTranslations from "../../assets/generated/CompiledTranslations"
import LanguageUtils from "../../Utils/LanguageUtils"
2020-07-20 12:39:43 +02:00
export default class Translations {
2022-09-08 21:40:48 +02:00
static readonly t: typeof CompiledTranslations.t & Readonly<typeof CompiledTranslations.t> =
CompiledTranslations.t
private static knownLanguages = LanguageUtils.usedLanguages
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"
}
public static W(s: string | number | BaseUIElement): BaseUIElement {
2022-09-08 21:40:48 +02:00
if (typeof s === "string") {
return new FixedUiElement(s)
2020-07-20 12:39:43 +02:00
}
if (typeof s === "number") {
return new FixedUiElement("" + s)
}
2022-09-08 21:40:48 +02:00
return s
}
/**
* Converts a string or an object into a typed translation.
* Translation objects ('Translation' and 'TypedTranslation') are converted/returned
2022-09-08 21:40:48 +02:00
*
* Translations.T("some text") // => new TypedTranslation({"*": "some text"})
* Translations.T("some text").txt // => "some text"
*
* const t = new Translation({"nl": "vertaling", "en": "translation"})
* Translations.T(t) // => new TypedTranslation<object>({"nl": "vertaling", "en": "translation"})
2022-09-08 21:40:48 +02:00
*
* const t = new TypedTranslation({"nl": "vertaling", "en": "translation"})
* Translations.T(t) // => t
2022-09-08 21:40:48 +02:00
*
* const json: any = {"en": "English", "nl": "Nederlands"};
* const translation = Translations.T(new Translation(json));
* translation.textFor("en") // => "English"
* translation.textFor("nl") // => "Nederlands"
2022-09-08 21:40:48 +02:00
*
*/
2022-10-27 01:50:41 +02:00
static T(
t:
| string
| Record<string, string>
| undefined
| null
| Translation
| TypedTranslation<object>,
2022-10-27 01:50:41 +02:00
context = undefined
): TypedTranslation<object> {
if (t === undefined || t === null) {
2022-09-08 21:40:48 +02:00
return undefined
2020-10-27 01:01:34 +01:00
}
2021-11-07 16:34:51 +01:00
if (typeof t === "number") {
t = "" + t
}
if (typeof t === "string") {
2022-09-08 21:40:48 +02:00
return new TypedTranslation<object>({ "*": t }, context)
2020-10-27 01:01:34 +01:00
}
if (t["render"] !== undefined) {
2022-09-08 21:40:48 +02:00
const msg =
"Creating a translation, but this object contains a 'render'-field. Use the translation directly"
console.error(msg, t)
2020-10-27 01:01:34 +01:00
throw msg
}
if (t instanceof TypedTranslation) {
2022-09-08 21:40:48 +02:00
return t
}
2022-09-08 21:40:48 +02:00
if (t instanceof Translation) {
return new TypedTranslation<object>(t.translations)
}
2022-09-08 21:40:48 +02:00
return new TypedTranslation<object>(t, context)
2020-07-20 12:39:43 +02:00
}
2020-07-21 00:07:04 +02:00
public static CountTranslations() {
2022-09-08 21:40:48 +02:00
const queue: any = [Translations.t]
const tr: Translation[] = []
while (queue.length > 0) {
2022-09-08 21:40:48 +02:00
const item = queue.pop()
if (item instanceof Translation || item.translations !== undefined) {
2022-09-08 21:40:48 +02:00
tr.push(item)
} else if (typeof item === "string") {
console.warn("Got single string in translationgs file: ", item)
} else {
for (const t in item) {
2022-09-08 21:40:48 +02:00
const x = item[t]
queue.push(x)
}
}
}
2022-09-08 21:40:48 +02:00
const langaugeCounts = {}
for (const translation of tr) {
for (const language in translation.translations) {
if (langaugeCounts[language] === undefined) {
langaugeCounts[language] = 1
} else {
2022-09-08 21:40:48 +02:00
langaugeCounts[language]++
}
}
}
for (const language in langaugeCounts) {
2022-09-08 21:40:48 +02:00
console.log(
"Total translations in ",
language,
langaugeCounts[language],
"/",
tr.length
)
}
}
static isProbablyATranslation(transl: any) {
2022-09-08 21:40:48 +02:00
if (typeof transl !== "object") {
return false
}
2022-09-08 21:40:48 +02:00
if (Object.keys(transl).length == 0) {
// No translations found; not a translation
return false
}
// is a weird key found?
2022-09-08 21:40:48 +02:00
if (
Object.keys(transl).some((key) => key !== "_context" && !this.knownLanguages.has(key))
) {
return false
}
2022-09-08 21:40:48 +02:00
return true
}
2020-09-15 18:44:58 +02:00
}