Skip to content

Commit

Permalink
fix(stdlib): wait for async translations (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkipke authored Nov 24, 2020
1 parent f854504 commit 9cec4c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function execute(filenames: string[], options: Partial<ExecutionOpt
throw new Error("Unable to build interpreter.");
}

loadTranslationFiles(interpreter, executionOptions.root);
await loadTranslationFiles(interpreter, executionOptions.root);

if (executionOptions.generateCoverage) {
coverageCollector = new CoverageCollector(executionOptions.root, lexerParserFn);
Expand Down
31 changes: 16 additions & 15 deletions src/stdlib/Localization.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs";
import * as path from "path";
import pSettle from "p-settle";
import { promisify } from "util";
const readFile = promisify(fs.readFile);

Expand Down Expand Up @@ -51,22 +52,22 @@ function parseTranslations(xmlNode: XmlDocument, locale: string) {
* @param rootDir The root package directory
*/
export async function loadTranslationFiles(interpreter: Interpreter, rootDir: string) {
locales.forEach(async (locale) => {
const filePath = path.join(rootDir, "locale", locale, "translations.ts");
if (fs.existsSync(filePath)) {
let xmlNode: XmlDocument;
try {
let contents = await readFile(filePath, "utf-8");
let xmlStr = contents.toString().replace(/\r?\n|\r/g, "");
xmlNode = new XmlDocument(xmlStr);
} catch (err) {
interpreter.stderr.write(`Error reading translations file ${filePath}: ${err}`);
return;
await pSettle(
Array.from(locales).map(async (locale) => {
const filePath = path.join(rootDir, "locale", locale, "translations.ts");
if (fs.existsSync(filePath)) {
let xmlNode: XmlDocument;
try {
let contents = await readFile(filePath, "utf-8");
let xmlStr = contents.toString().replace(/\r?\n|\r/g, "");
xmlNode = new XmlDocument(xmlStr);
parseTranslations(xmlNode, locale);
} catch (err) {
interpreter.stderr.write(`Error reading translations file ${filePath}: ${err}`);
}
}

parseTranslations(xmlNode, locale);
}
});
})
);
}

export const Tr = new Callable("Tr", {
Expand Down

0 comments on commit 9cec4c2

Please sign in to comment.