diff --git a/cms/top-announcement-banner.json b/cms/top-announcement-banner.json index 7a1a5ed..ba1f114 100644 --- a/cms/top-announcement-banner.json +++ b/cms/top-announcement-banner.json @@ -1,15 +1,5 @@ { "$schema": "../schemas/top-announcement-banner.schema.json", "isChainHalted": false, - "banner": { - "enTextOrLocalizationPath": "This is a test of the top announcement banner.", - "link": { - "enTextOrLocalizationKey": "Click here to learn more.", - "isExternal": true, - "url": "https://www.google.com" - }, - "localStorageKey": "top-announcement-banner-test", - "startDate": "2024-01-08T00:00:00.000Z", - "endDate": "2024-02-01T00:00:00.000Z" - } + "banner": null } diff --git a/scripts/translate-cms.js b/scripts/translate-cms.js new file mode 100644 index 0000000..1406544 --- /dev/null +++ b/scripts/translate-cms.js @@ -0,0 +1,77 @@ +const fs = require("fs"); +const path = require("path"); + +// Load the settings +const settings = JSON.parse( + fs.readFileSync("project.inlang/settings.json", "utf8") +); + +// Load the banner file +const obj = JSON.parse( + fs.readFileSync("cms/top-announcement-banner.json", "utf8") +); + +/** + * Set a value in an object using a path. + * + * @example + * // Set the value of `object.a.b.c` to `3` + * setObjValue(object, "a.b.c", 3); + */ +function setObjValue(object, path, value) { + const clone = { ...object }; + const way = path.replace(/\[/g, ".").replace(/\]/g, "").split("."), + last = way.pop(); + + way.reduce(function (o, k, i, kk) { + return (o[k] = + o[k] || (isFinite(i + 1 in kk ? kk[i + 1] : last) ? [] : {})); + }, object)[last] = value; + return clone; +} + +/** + * Get a value in an object using a path. + * @example + * // Get the value of `object.a.b.c` + * getObjValue("a.b.c", object); + */ +function getObjValue(key, object) { + return key.split(".").reduce((o, i) => o[i], object); +} + +// Iterate over each language +settings.languageTags.forEach((languageTag) => { + // Load the localization file for this language + const localization = JSON.parse( + fs.readFileSync(path.join("localizations", `${languageTag}.json`), "utf8") + ); + + let resultObj = { ...obj }; + Object.entries(resultObj).forEach(([key, value]) => { + // Set the value in the banner object + resultObj = setObjValue( + obj, + `${key}.enTextOrLocalizationPath`, + languageTag + ); + + // Inline the translations + obj[key][`enTextOrLocalizationPath`] = + localization[obj[key].enTextOrLocalizationPath]; + }); + + setObjValue(banner, `banner.enTextOrLocalizationPath`, languageTag); + // Inline the translations + banner.banner[`enTextOrLocalizationPath`] = + localization[banner.banner.enTextOrLocalizationPath]; + banner.banner.link[`enTextOrLocalizationKey`] = + localization[banner.banner.link.enTextOrLocalizationKey]; +}); + +// Save the updated banner file +fs.writeFileSync( + "cms/top-announcement-banner.json", + JSON.stringify(banner, null, 2), + "utf8" +);