@vendure/ngx-translate-extract
Version:
Extract strings from projects using ngx-translate
32 lines (31 loc) • 1.49 kB
JavaScript
import { TranslationCollection } from '../utils/translation.collection.js';
import { getNamedImportAlias, findFunctionCallExpressions, getStringsFromExpression, getAST } from '../utils/ast-helpers.js';
const MARKER_MODULE_NAME = new RegExp('ngx-translate-extract-marker');
const MARKER_IMPORT_NAME = 'marker';
const NGX_TRANSLATE_MARKER_MODULE_NAME = '@ngx-translate/core';
const NGX_TRANSLATE_MARKER_IMPORT_NAME = '_';
export class MarkerParser {
extract(source, filePath) {
const sourceFile = getAST(source, filePath);
const markerImportName = this.getMarkerImportNameFromSource(sourceFile);
if (!markerImportName) {
return null;
}
let collection = new TranslationCollection();
const callExpressions = findFunctionCallExpressions(sourceFile, markerImportName);
callExpressions.forEach((callExpression) => {
const [firstArg] = callExpression.arguments;
if (!firstArg) {
return;
}
const strings = getStringsFromExpression(firstArg);
collection = collection.addKeys(strings, filePath);
});
return collection;
}
getMarkerImportNameFromSource(sourceFile) {
const markerImportName = getNamedImportAlias(sourceFile, MARKER_IMPORT_NAME, MARKER_MODULE_NAME) ||
getNamedImportAlias(sourceFile, NGX_TRANSLATE_MARKER_IMPORT_NAME, NGX_TRANSLATE_MARKER_MODULE_NAME);
return markerImportName ?? '';
}
}