From 6d3ecef2f816795e415f20eef8b6e7c6fe37f2be Mon Sep 17 00:00:00 2001 From: Estelle Comment Date: Wed, 24 Jan 2024 11:26:55 +0100 Subject: [PATCH] Scripts to change from piped to nested format and back --- .../tchap/translations/reformatToNested.js | 49 +++++++++++++++++++ scripts/tchap/translations/reformatToPiped.js | 41 ++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 scripts/tchap/translations/reformatToNested.js create mode 100644 scripts/tchap/translations/reformatToPiped.js diff --git a/scripts/tchap/translations/reformatToNested.js b/scripts/tchap/translations/reformatToNested.js new file mode 100644 index 0000000000..fc58267bc3 --- /dev/null +++ b/scripts/tchap/translations/reformatToNested.js @@ -0,0 +1,49 @@ +/* + Change format from piped format ("aa|bb|cc") to nested format. Output result to stdout. + Usage : node scripts/tchap/translations/reformatToNested.js --file=$TCHAP_TRANSLATION_FILE > $OUTFILE + + aa|bb|cc format (piped format): + { + "security|backup_keys": { + "en": "Hello", + "fr": "Coucou" + } + } + + Nested format : + { + "security": { + "backup_keys": { + "en": "Hello", + "fr": "Coucou" + } + } + } +*/ + +const parseArgs = require("minimist"); + +const argv = parseArgs(process.argv.slice(2), {}); +const tchapTranslations = require(argv.file); + +const reformat = (translations) => { + for (const [key, value] of Object.entries(translations)) { + // Split "aa|bb|cc" into "aa" and "bb|cc" + let [parentKey, ...restOfKey] = key.split('|'); + restOfKey = restOfKey.join('|'); + if (restOfKey === '') { // no "|" in key + // do nothing, it's already in the right format. + } else { + // initialize translations[parentKey] if not exist + if (!(parentKey in translations)) { + translations[parentKey] = {}; + } + translations[parentKey][restOfKey] = value; + delete translations[key]; + reformat(translations[parentKey]); + } + } +} + +reformat(tchapTranslations); +console.log(JSON.stringify(tchapTranslations, null, 4)); diff --git a/scripts/tchap/translations/reformatToPiped.js b/scripts/tchap/translations/reformatToPiped.js new file mode 100644 index 0000000000..1a042da23a --- /dev/null +++ b/scripts/tchap/translations/reformatToPiped.js @@ -0,0 +1,41 @@ +/* + Change format from nested format to piped format ("aa|bb|cc"). Output result to stdout. + Usage : node scripts/tchap/translations/reformatToPiped.js --file=$TCHAP_TRANSLATION_FILE > $OUTFILE + + Nested format : + { + "security": { + "backup_keys": { + "en": "Hello", + "fr": "Coucou" + } + } + } + + aa|bb|cc format (piped format): + { + "security|backup_keys": { + "en": "Hello", + "fr": "Coucou" + } + } + */ + +const parseArgs = require("minimist"); + +const argv = parseArgs(process.argv.slice(2), {}); +const tchapTranslations = require(argv.file); + +const output = {}; +const reformat = (translations, parentKey) => { + for (const key of Object.keys(translations)) { + if ('en' in translations[key]) { + output[parentKey + key] = translations[key]; + } else { + reformat(translations[key], parentKey + key + '|'); + } + } +} + +reformat(tchapTranslations, ""); +console.log(JSON.stringify(output, null, 4)); \ No newline at end of file