Skip to content

Commit

Permalink
Scripts to change from piped to nested format and back
Browse files Browse the repository at this point in the history
  • Loading branch information
estellecomment committed Jan 24, 2024
1 parent 42601d6 commit 6d3ecef
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
49 changes: 49 additions & 0 deletions scripts/tchap/translations/reformatToNested.js
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));
41 changes: 41 additions & 0 deletions scripts/tchap/translations/reformatToPiped.js
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));

0 comments on commit 6d3ecef

Please sign in to comment.