-
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.
chore: add scripts to convert translations between CSV and JSON
- Loading branch information
Showing
4 changed files
with
76 additions
and
1 deletion.
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
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,38 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
const path = require('path'); | ||
const fs = require('fs').promises; | ||
|
||
function flatten(arr) { | ||
return arr.reduce( | ||
(a, v) => (Array.isArray(v) ? a.concat(flatten(v)) : a.concat(v)), | ||
[], | ||
); | ||
} | ||
|
||
const getLines = (de, fr, prefix) => { | ||
const deData = Object.entries(de); | ||
const frData = Object.entries(fr); | ||
return deData.map(([deKey, deValue], index) => { | ||
const [frKey, frValue] = frData[index]; | ||
if (frKey !== deKey) { | ||
throw new Error(`French translation missing for ${deKey}!`); | ||
} | ||
const lineKey = prefix ? `${prefix}.${deKey}` : deKey; | ||
if (typeof deValue === 'string') { | ||
return `${lineKey},"${deValue}","${frValue}"`; | ||
} | ||
return getLines(deValue, frValue, lineKey); | ||
}); | ||
}; | ||
|
||
// eslint-disable-next-line func-names | ||
(async function () { | ||
const dePath = path.join(__dirname, 'resources', 'de', 'translation.json'); | ||
const frPath = path.join(__dirname, 'resources', 'fr', 'translation.json'); | ||
const de = JSON.parse(await fs.readFile(dePath)); | ||
const fr = JSON.parse(await fs.readFile(frPath)); | ||
|
||
const csvPath = path.join(__dirname, 'translations.csv'); | ||
const translations = flatten(getLines(de, fr)); | ||
await fs.writeFile(csvPath, ['key,de,fr', ...translations].join('\n')); | ||
})(); |
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,23 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
const path = require('path'); | ||
const fs = require('fs').promises; | ||
const parse = require('csv-parse/lib/sync'); | ||
const set = require('lodash.set'); | ||
|
||
const de = {}; | ||
const fr = {}; | ||
|
||
// eslint-disable-next-line func-names | ||
(async function () { | ||
const data = await fs.readFile(path.join(__dirname, 'translations.csv')); | ||
const records = parse(data); | ||
records.shift(); // remove header | ||
await records.forEach(([key, german, french]) => { | ||
set(de, key.split('.'), german); | ||
set(fr, key.split('.'), french); | ||
}); | ||
const dePath = path.join(__dirname, 'resources', 'de', 'translation.json'); | ||
const frPath = path.join(__dirname, 'resources', 'fr', 'translation.json'); | ||
await fs.writeFile(dePath, JSON.stringify(de, null, 2)); | ||
await fs.writeFile(frPath, JSON.stringify(fr, null, 2)); | ||
})(); |
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