-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts to change from piped to nested format and back
- Loading branch information
1 parent
42601d6
commit 6d3ecef
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |