feat(SW-706): add diff tooling

This commit is contained in:
Michael Zetterberg
2025-04-11 08:53:18 +02:00
parent 35862d03f0
commit 7de46cafa8
3 changed files with 78 additions and 5 deletions

View File

@@ -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. 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 ```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 #### Message upload to Lokalise
Set the environment variable `LOKALISE_API_KEY` to the API key for 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. It supports the different upload phases from Lokalise meaning that once this command completes the messages are available for translation in Lokalise.
```bash ```bash
npm run i18n:upload yarn workspace @scandic-hotels/scandic-web i18n:upload
``` ```
#### Message download from Lokalise #### Message download from Lokalise
@@ -330,7 +339,7 @@ Running the following command will download the translated assets from Lokalise
_DOCUMENTATION PENDING FOR FULL WORKFLOW._ _DOCUMENTATION PENDING FOR FULL WORKFLOW._
```bash ```bash
npm run i18n:download yarn workspace @scandic-hotels/scandic-web i18n:download
``` ```
#### Message compilation #### Message compilation
@@ -340,9 +349,15 @@ Compiles the assets that were downloaded from Lokalise into the dictionaries use
_DOCUMENTATION PENDING FOR FULL WORKFLOW._ _DOCUMENTATION PENDING FOR FULL WORKFLOW._
```bash ```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 ### The workflow
We use the following technical stack to handle translations of UI labels. We use the following technical stack to handle translations of UI labels.

View File

@@ -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("")
}

View File

@@ -25,7 +25,8 @@
"i18n:compile": "formatjs compile-folder --ast --format i18n/tooling/formatter.mjs i18n/tooling/translations i18n/dictionaries", "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:push": "yarn i18n:extract && yarn i18n:upload",
"i18n:pull": "yarn i18n:download && yarn i18n:compile", "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": { "dependencies": {
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.27", "@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.27",