Skip to content

Commit ece2c09

Browse files
committed
Fix extract script
1 parent 945fb59 commit ece2c09

File tree

3 files changed

+47
-82
lines changed

3 files changed

+47
-82
lines changed

examples/TranslationsExtractor.re

Lines changed: 0 additions & 81 deletions
This file was deleted.

examples/extractor/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const fs = require("fs");
2+
const cp = require("child_process");
3+
const path = require("path");
4+
5+
const locales = [
6+
"en",
7+
"ru",
8+
];
9+
10+
const cwd = process.cwd();
11+
const src = path.join(cwd, "examples");
12+
const bin = path.join(cwd, "node_modules", ".bin", "bs-react-intl-extractor");
13+
14+
console.log(`=== ⏳ Extracting messages...`);
15+
const extracted = JSON.parse(cp.execSync(`${bin} --allow-duplicates ${src}`));
16+
console.log(`=== ✅ Extracting messages... done.`);
17+
18+
for (const locale of locales) {
19+
console.log(`\n=== ⏳ Updating ${locale} translation...`);
20+
const file = path.join(src, "translations", `${locale}.json`);
21+
22+
let content;
23+
try {
24+
content = JSON.parse(fs.readFileSync(file));
25+
} catch(error) {
26+
if (error.code === "ENOENT") {
27+
console.log(`=== ⚠️ Translation for ${locale} wasn't found. Creating new one.`);
28+
content = [];
29+
} else {
30+
throw error;
31+
}
32+
}
33+
34+
const cache = content.reduce(
35+
(acc, msg) => ({ ...acc, [msg.id]: msg }),
36+
{},
37+
);
38+
const messages = extracted.map(msg => ({
39+
id: msg.id,
40+
defaultMessage: msg.defaultMessage,
41+
message: cache[msg.id] && cache[msg.id].message ? cache[msg.id].message : "",
42+
}));
43+
44+
fs.writeFileSync(file, JSON.stringify(messages, null, 2) + "\n");
45+
console.log(`=== ✅ Updating ${locale} translation... done.`);
46+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build": "bsb -clean-world -make-world",
1111
"watch": "bsb -clean-world -make-world -w",
1212
"clean": "bsb -clean-world",
13-
"extract": "node examples/TranslationsExtractor.bs.js",
13+
"extract": "node examples/extractor",
1414
"preextract": "yarn run build",
1515
"format": "bsrefmt --in-place **/*.re",
1616
"test": "exit 0",

0 commit comments

Comments
 (0)