feat(SW-706): add diff tooling
This commit is contained in:
@@ -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.
|
||||
|
||||
57
apps/scandic-web/i18n/tooling/diff.mjs
Normal file
57
apps/scandic-web/i18n/tooling/diff.mjs
Normal 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("")
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user