From 7de46cafa87dfe560340525c9fb6fe8d47bb1380 Mon Sep 17 00:00:00 2001 From: Michael Zetterberg Date: Fri, 11 Apr 2025 08:53:18 +0200 Subject: [PATCH] feat(SW-706): add diff tooling --- apps/scandic-web/i18n/i18n.md | 23 +++++++++-- apps/scandic-web/i18n/tooling/diff.mjs | 57 ++++++++++++++++++++++++++ apps/scandic-web/package.json | 3 +- 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 apps/scandic-web/i18n/tooling/diff.mjs diff --git a/apps/scandic-web/i18n/i18n.md b/apps/scandic-web/i18n/i18n.md index c734ccb3e..ca0e1fce3 100644 --- a/apps/scandic-web/i18n/i18n.md +++ b/apps/scandic-web/i18n/i18n.md @@ -306,9 +306,18 @@ Extracts the messages from calls to `intl.formatMessage()` and other supported m Running the following command will generate a JSON file at `./i18n/tooling/extracted.json`. The format of this file is for consumption by Lokalise. This JSON file is what gets uploaded to Lokalise. ```bash -npm run i18n:extract +yarn workspace @scandic-hotels/scandic-web i18n:extract ``` +#### Checking for changes between codebase and Lokalise + +> _NOTE_: Diff only considers the English language. + +It is recommended to download the latest labels from Lokalise to make sure you have the latest before diffing. See below. + +- Run the message extraction above. +- Run `yarn workspace @scandic-hotels/scandic-web i18n:diff` + #### Message upload to Lokalise Set the environment variable `LOKALISE_API_KEY` to the API key for Lokalise. @@ -318,7 +327,7 @@ Running the following command will upload the JSON file, that was generated by e It supports the different upload phases from Lokalise meaning that once this command completes the messages are available for translation in Lokalise. ```bash -npm run i18n:upload +yarn workspace @scandic-hotels/scandic-web i18n:upload ``` #### Message download from Lokalise @@ -330,7 +339,7 @@ Running the following command will download the translated assets from Lokalise _DOCUMENTATION PENDING FOR FULL WORKFLOW._ ```bash -npm run i18n:download +yarn workspace @scandic-hotels/scandic-web i18n:download ``` #### Message compilation @@ -340,9 +349,15 @@ Compiles the assets that were downloaded from Lokalise into the dictionaries use _DOCUMENTATION PENDING FOR FULL WORKFLOW._ ```bash -npm run i18n:compile +yarn workspace @scandic-hotels/scandic-web i18n:compile ``` +#### Convenience script targets + +Extract and upload: `yarn workspace @scandic-hotels/scandic-web i18n:push` +Download and compile: `yarn workspace @scandic-hotels/scandic-web i18n:pull` +Extract, upload, download and compile (push && pull): `yarn workspace @scandic-hotels/scandic-web i18n:sync` + ### The workflow We use the following technical stack to handle translations of UI labels. diff --git a/apps/scandic-web/i18n/tooling/diff.mjs b/apps/scandic-web/i18n/tooling/diff.mjs new file mode 100644 index 000000000..d2c12999f --- /dev/null +++ b/apps/scandic-web/i18n/tooling/diff.mjs @@ -0,0 +1,57 @@ +import fromLokalise from "./translations/en.json" with { type: "json" } +import fromCodebase from "./extracted.json" with { type: "json" } + +function diffArray(json1, json2) { + const diff = [] + const keys1 = Object.keys(json1) + const keys2 = Object.keys(json2) + + keys1.forEach((key) => { + if (!keys2.includes(key)) { + diff.push(key) + } + }) + + return diff +} + +function resolveLabels(ids, arr) { + return ids.map((id) => { + return { + id, + ...arr[id], + } + }) +} + +const labelsToRemove = diffArray(fromLokalise, fromCodebase) +const labelsToAdd = diffArray(fromCodebase, fromLokalise) + +if (labelsToRemove.length === 0 && labelsToAdd.length === 0) { + console.log(`Nothing has changed!`) +} else { + console.log(`Labels to REMOVE from Lokalise: ${labelsToRemove.length}`) + console.log(`Labels to ADD to Lokalise: ${labelsToAdd.length}`) + console.log("") +} + +if (labelsToRemove.length) { + console.log(`${labelsToRemove.length} labels to remove from Lokalise:`) + console.table(resolveLabels(labelsToRemove, fromLokalise)) + console.log("") +} + +if (labelsToAdd.length) { + console.log("") + console.log(`${labelsToAdd.length} labels to add to Lokalise`) + console.table(resolveLabels(labelsToAdd, fromCodebase)) + console.log("") +} + +if (labelsToRemove.length === 0 && labelsToAdd.length === 0) { + console.log(`Nothing has changed!`) +} else { + console.log(`Labels to REMOVE from Lokalise: ${labelsToRemove.length}`) + console.log(`Labels to ADD to Lokalise: ${labelsToAdd.length}`) + console.log("") +} diff --git a/apps/scandic-web/package.json b/apps/scandic-web/package.json index 741110be8..f4f7eab67 100644 --- a/apps/scandic-web/package.json +++ b/apps/scandic-web/package.json @@ -25,7 +25,8 @@ "i18n:compile": "formatjs compile-folder --ast --format i18n/tooling/formatter.mjs i18n/tooling/translations i18n/dictionaries", "i18n:push": "yarn i18n:extract && yarn i18n:upload", "i18n:pull": "yarn i18n:download && yarn i18n:compile", - "i18n:sync": "yarn i18n:push && yarn i18n:pull" + "i18n:sync": "yarn i18n:push && yarn i18n:pull", + "i18n:diff": "node i18n/tooling/diff.mjs" }, "dependencies": { "@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.27",