@lingui/cli
Version:
Lingui CLI to extract messages, compile catalogs, and manage translation workflows
34 lines (33 loc) • 1.06 kB
JavaScript
import fs from "fs/promises";
import { createBabelExtractor } from "./babel.js";
let defaultExtractor;
function createDefaultExtractor(linguiConfig) {
if (!defaultExtractor) {
defaultExtractor = createBabelExtractor({
parserOptions: linguiConfig.extractorParserOptions,
});
}
return defaultExtractor;
}
export default async function extract(filename, onMessageExtracted, linguiConfig) {
const extractorsToExtract = linguiConfig.extractors ?? [
createDefaultExtractor(linguiConfig),
];
for (const ext of extractorsToExtract) {
if (!ext.match(filename))
continue;
try {
const file = await fs.readFile(filename);
await ext.extract(filename, file.toString(), onMessageExtracted, {
linguiConfig,
});
return true;
}
catch (e) {
console.error(`Cannot process file ${filename} ${e.message}`);
console.error(e.stack);
return false;
}
}
return true;
}