Skip to content

Fix extract script #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 0 additions & 81 deletions examples/TranslationsExtractor.re

This file was deleted.

46 changes: 46 additions & 0 deletions examples/extractor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require("fs");
const cp = require("child_process");
const path = require("path");

const locales = [
"en",
"ru",
];

const cwd = process.cwd();
const src = path.join(cwd, "examples");
const bin = path.join(cwd, "node_modules", ".bin", "bs-react-intl-extractor");

console.log(`=== ⏳ Extracting messages...`);
const extracted = JSON.parse(cp.execSync(`${bin} --allow-duplicates ${src}`));
console.log(`=== ✅ Extracting messages... done.`);

for (const locale of locales) {
console.log(`\n=== ⏳ Updating ${locale} translation...`);
const file = path.join(src, "translations", `${locale}.json`);

let content;
try {
content = JSON.parse(fs.readFileSync(file));
} catch(error) {
if (error.code === "ENOENT") {
console.log(`=== ⚠️ Translation for ${locale} wasn't found. Creating new one.`);
content = [];
} else {
throw error;
}
}

const cache = content.reduce(
(acc, msg) => ({ ...acc, [msg.id]: msg }),
{},
);
const messages = extracted.map(msg => ({
id: msg.id,
defaultMessage: msg.defaultMessage,
message: cache[msg.id] && cache[msg.id].message ? cache[msg.id].message : "",
}));

fs.writeFileSync(file, JSON.stringify(messages, null, 2) + "\n");
console.log(`=== ✅ Updating ${locale} translation... done.`);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build": "bsb -clean-world -make-world",
"watch": "bsb -clean-world -make-world -w",
"clean": "bsb -clean-world",
"extract": "node examples/TranslationsExtractor.bs.js",
"extract": "node examples/extractor",
"preextract": "yarn run build",
"format": "bsrefmt --in-place **/*.re",
"test": "exit 0",
Expand Down