Skip to content
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

fix(stdlib): wait for async translations #587

Merged
merged 2 commits into from
Nov 24, 2020
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
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