@vendure/ngx-translate-extract
Version:
Extract strings from projects using ngx-translate
29 lines (28 loc) • 1.09 kB
JavaScript
import { TranslationCollection } from '../utils/translation.collection.js';
import { getStringsFromExpression, findSimpleCallExpressions, getAST } from '../utils/ast-helpers.js';
import pkg from 'typescript';
const { isIdentifier } = pkg;
export class FunctionParser {
fnName;
constructor(fnName) {
this.fnName = fnName;
}
extract(source, filePath) {
const sourceFile = getAST(source, filePath);
let collection = new TranslationCollection();
const callExpressions = findSimpleCallExpressions(sourceFile, this.fnName);
callExpressions.forEach((callExpression) => {
if (!isIdentifier(callExpression.expression)
|| callExpression.expression.escapedText !== this.fnName) {
return;
}
const [firstArg] = callExpression.arguments;
if (!firstArg) {
return;
}
const strings = getStringsFromExpression(firstArg);
collection = collection.addKeys(strings, filePath);
});
return collection;
}
}