1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-13 18:21:51 +00:00

Add support for modifying and registering new transltions

This commit is contained in:
tobspr 2022-01-13 22:30:52 +01:00
parent 6bac7cec57
commit ebb2f3a1c6
5 changed files with 40 additions and 18 deletions

View File

@ -233,7 +233,7 @@ export class BackgroundResourcesLoader {
this.numAssetsToLoadTotal = 0;
this.numAssetsLoaded = 0;
})
.then(MODS.modInterface.injectSprites.bind(MODS))
.then(MODS.modInterface.injectSprites.bind(MODS.modInterface))
);
}
}

View File

@ -16,5 +16,5 @@ export let GLOBAL_APP = null;
export function setGlobalApp(app) {
assert(!GLOBAL_APP, "Create application twice!");
GLOBAL_APP = app;
MODS.linkApp(app);
MODS.app = app;
}

View File

@ -60,5 +60,16 @@ export class DemoMod extends Mod {
this.modLoader.signals.modifyLevelDefinitions.add(definitions => {
definitions[0].shape = "LuCuLuCu";
});
this.modLoader.modInterface.registerTranslations("en", {
ingame: {
interactiveTutorial: {
title: "Hello",
hints: {
"1_1_extractor": "World!",
},
},
},
});
}
}

View File

@ -8,6 +8,8 @@ import { AtlasSprite, SpriteAtlasLink } from "../core/sprites";
import { Mod } from "./mod";
import { enumShortcodeToSubShape, enumSubShape, enumSubShapeToShortcode } from "../game/shape_definition";
import { Loader } from "../core/loader";
import { LANGUAGES } from "../languages";
import { matchDataRecursive, T } from "../translations";
const LOG = createLogger("mod-interface");
@ -35,11 +37,6 @@ export class ModInterface {
* @param {ModLoader} modLoader
*/
constructor(modLoader) {
/**
* @param {Application} app
*/
this.app = undefined;
this.modLoader = modLoader;
/** @type {Map<string, AtlasSprite>} */
@ -105,4 +102,16 @@ export class ModInterface {
MODS_ADDITIONAL_SHAPE_MAP_WEIGHTS[id] = weightComputation;
MODS_ADDITIONAL_SUB_SHAPE_DRAWERS[id] = shapeDrawer;
}
registerTranslations(language, translations) {
const data = LANGUAGES[language];
if (!data) {
throw new Error("Unknown language: " + language);
}
matchDataRecursive(data.data, translations, true);
if (language === "en") {
matchDataRecursive(T, translations, true);
}
}
}

View File

@ -24,15 +24,6 @@ if (G_IS_DEV && globalConfig.debug.testTranslations) {
mapTranslations(T);
}
export function applyLanguage(languageCode) {
logger.log("Applying language:", languageCode);
const data = LANGUAGES[languageCode];
if (!data) {
logger.error("Language not found:", languageCode);
return false;
}
}
// Language key is something like de-DE or en or en-US
function mapLanguageCodeToId(languageKey) {
const key = languageKey.toLowerCase();
@ -97,17 +88,20 @@ export function autoDetectLanguageId() {
return "en";
}
function matchDataRecursive(dest, src) {
export function matchDataRecursive(dest, src, addNewKeys = false) {
if (typeof dest !== "object" || typeof src !== "object") {
return;
}
if (dest === null || src === null) {
return;
}
for (const key in dest) {
if (src[key]) {
// console.log("copy", key);
const data = dest[key];
if (typeof data === "object") {
matchDataRecursive(dest[key], src[key]);
matchDataRecursive(dest[key], src[key], addNewKeys);
} else if (typeof data === "string" || typeof data === "number") {
// console.log("match string", key);
dest[key] = src[key];
@ -116,6 +110,14 @@ function matchDataRecursive(dest, src) {
}
}
}
if (addNewKeys) {
for (const key in src) {
if (!dest[key]) {
dest[key] = JSON.parse(JSON.stringify(src[key]));
}
}
}
}
export function updateApplicationLanguage(id) {