Skip to content

Commit

Permalink
chore: add scripts to convert translations between CSV and JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed Aug 24, 2020
1 parent 44fd01b commit e696350
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@typescript-eslint/parser": "3.9.1",
"babel-eslint": "10.1.0",
"cra-append-sw": "2.7.0",
"csv-parse": "^4.12.0",
"cypress": "5.0.0",
"eslint": "6.8.0",
"eslint-config-airbnb": "18.2.0",
Expand All @@ -47,6 +48,7 @@
"eslint-plugin-react-hooks": "4.1.0",
"husky": "4.2.5",
"lint-staged": "10.2.11",
"lodash.set": "^4.3.2",
"prettier": "2.0.5",
"react-test-renderer": "16.13.1",
"start-server-and-test": "1.11.3"
Expand Down Expand Up @@ -120,6 +122,8 @@
"lint": "eslint src",
"format": "prettier --write 'src/**/*.js'",
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!ol)/\"",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"i18n:convert2csv": "node ./src/i18n/convert2csv.js",
"i18n:convert2json": "node ./src/i18n/convert2json.js"
}
}
38 changes: 38 additions & 0 deletions src/i18n/convert2csv.js
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'));
})();
23 changes: 23 additions & 0 deletions src/i18n/convert2json.js
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));
})();
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4175,6 +4175,11 @@ csstype@^2.6.7:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.10.tgz#e63af50e66d7c266edb6b32909cfd0aabe03928b"
integrity sha512-D34BqZU4cIlMCY93rZHbrq9pjTAQJ3U8S8rfBqjwHxkGPThWFjzZDQpgMJY0QViLxth6ZKYiwFBo14RdN44U/w==

csv-parse@^4.12.0:
version "4.12.0"
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-4.12.0.tgz#fd42d6291bbaadd51d3009f6cadbb3e53b4ce026"
integrity sha512-wPQl3H79vWLPI8cgKFcQXl0NBgYYEqVnT1i6/So7OjMpsI540oD7p93r3w6fDSyPvwkTepG05F69/7AViX2lXg==

cyclist@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
Expand Down Expand Up @@ -7852,6 +7857,11 @@ lodash.once@^4.1.1:
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down

0 comments on commit e696350

Please sign in to comment.