forked from MapComplete/MapComplete
Add translation buttons
This commit is contained in:
parent
592bc4ae0b
commit
2c7fb556dc
31 changed files with 442 additions and 150 deletions
|
@ -19,7 +19,7 @@ import {DesugaringContext} from "../Models/ThemeConfig/Conversion/Conversion";
|
|||
|
||||
class LayerOverviewUtils {
|
||||
|
||||
writeSmallOverview(themes: { id: string, title: any, shortDescription: any, icon: string, hideFromOverview: boolean }[]) {
|
||||
writeSmallOverview(themes: { id: string, title: any, shortDescription: any, icon: string, hideFromOverview: boolean, mustHaveLanguage: boolean }[]) {
|
||||
const perId = new Map<string, any>();
|
||||
for (const theme of themes) {
|
||||
const data = {
|
||||
|
@ -27,7 +27,8 @@ class LayerOverviewUtils {
|
|||
title: theme.title,
|
||||
shortDescription: theme.shortDescription,
|
||||
icon: theme.icon,
|
||||
hideFromOverview: theme.hideFromOverview
|
||||
hideFromOverview: theme.hideFromOverview,
|
||||
mustHaveLanguage: theme.mustHaveLanguage
|
||||
}
|
||||
perId.set(theme.id, data);
|
||||
}
|
||||
|
@ -73,6 +74,7 @@ class LayerOverviewUtils {
|
|||
continue
|
||||
}
|
||||
questions[key].id = key;
|
||||
questions[key]["source"] = "shared-questions"
|
||||
dict.set(key, <TagRenderingConfigJson>questions[key])
|
||||
}
|
||||
for (const key in icons["default"]) {
|
||||
|
@ -218,7 +220,8 @@ class LayerOverviewUtils {
|
|||
return {
|
||||
...t,
|
||||
hideFromOverview: t.hideFromOverview ?? false,
|
||||
shortDescription: t.shortDescription ?? new Translation(t.description).FirstSentence().translations
|
||||
shortDescription: t.shortDescription ?? new Translation(t.description).FirstSentence().translations,
|
||||
mustHaveLanguage: t.mustHaveLanguage?.length > 0
|
||||
}
|
||||
}));
|
||||
return fixed;
|
||||
|
|
|
@ -244,11 +244,11 @@ function isTranslation(tr: any): boolean {
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts a translation object into something that can be added to the 'generated translations'
|
||||
* @param obj
|
||||
* @param depth
|
||||
* Converts a translation object into something that can be added to the 'generated translations'.
|
||||
*
|
||||
* To debug the 'compiledTranslations', add a languageWhiteList to only generate a single language
|
||||
*/
|
||||
function transformTranslation(obj: any, depth = 1) {
|
||||
function transformTranslation(obj: any, path: string[] = [], languageWhitelist : string[] = undefined) {
|
||||
|
||||
if (isTranslation(obj)) {
|
||||
return `new Translation( ${JSON.stringify(obj)} )`
|
||||
|
@ -259,15 +259,24 @@ function transformTranslation(obj: any, depth = 1) {
|
|||
if (key === "#") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key.match("^[a-zA-Z0-9_]*$") === null) {
|
||||
throw "Invalid character in key: " + key
|
||||
}
|
||||
const value = obj[key]
|
||||
let value = obj[key]
|
||||
|
||||
if (isTranslation(value)) {
|
||||
values += (Utils.Times((_) => " ", depth)) + "get " + key + "() { return new Translation(" + JSON.stringify(value) + ") }" + ",\n"
|
||||
if(languageWhitelist !== undefined){
|
||||
const nv = {}
|
||||
for (const ln of languageWhitelist) {
|
||||
nv[ln] = value[ln]
|
||||
}
|
||||
value = nv;
|
||||
}
|
||||
values += `${Utils.Times((_) => " ", path.length + 1)}get ${key}() { return new Translation(${JSON.stringify(value)}, "core:${path.join(".")}.${key}") },
|
||||
`
|
||||
} else {
|
||||
values += (Utils.Times((_) => " ", depth)) + key + ": " + transformTranslation(value, depth + 1) + ",\n"
|
||||
values += (Utils.Times((_) => " ", path.length + 1)) + key + ": " + transformTranslation(value, [...path, key], languageWhitelist) + ",\n"
|
||||
}
|
||||
}
|
||||
return `{${values}}`;
|
||||
|
@ -305,11 +314,11 @@ function formatFile(path) {
|
|||
*/
|
||||
function genTranslations() {
|
||||
const translations = JSON.parse(fs.readFileSync("./assets/generated/translations.json", "utf-8"))
|
||||
const transformed = transformTranslation(translations);
|
||||
const transformed = transformTranslation(translations);
|
||||
|
||||
let module = `import {Translation} from "../../UI/i18n/Translation"\n\nexport default class CompiledTranslations {\n\n`;
|
||||
module += " public static t = " + transformed;
|
||||
module += "}"
|
||||
module += "\n }"
|
||||
|
||||
fs.writeFileSync("./assets/generated/CompiledTranslations.ts", module);
|
||||
|
||||
|
@ -541,7 +550,7 @@ for (const path of allTranslationFiles) {
|
|||
}
|
||||
|
||||
|
||||
// SOme validation
|
||||
// Some validation
|
||||
TranslationPart.fromDirectory("./langs").validateStrict("./langs")
|
||||
TranslationPart.fromDirectory("./langs/layers").validateStrict("layers")
|
||||
TranslationPart.fromDirectory("./langs/themes").validateStrict("themes")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue