debian-mirror-gitlab/scripts/frontend/extract_gettext_all.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
const argumentsParser = require('commander');
const { GettextExtractor, JsExtractors } = require('gettext-extractor');
const {
decorateJSParserWithVueSupport,
decorateExtractorWithHelpers,
} = require('gettext-extractor-vue');
2021-04-17 20:07:23 +05:30
const vue2TemplateCompiler = require('vue-template-compiler');
2018-11-20 20:47:30 +05:30
const ensureSingleLine = require('../../app/assets/javascripts/locale/ensure_single_line.js');
const args = argumentsParser
.option('-f, --file <file>', 'Extract message from one single file')
.option('-a, --all', 'Extract message from all js/vue files')
.parse(process.argv);
const extractor = decorateExtractorWithHelpers(new GettextExtractor());
extractor.addMessageTransformFunction(ensureSingleLine);
const jsParser = extractor.createJsParser([
// Place all the possible expressions to extract here:
JsExtractors.callExpression('__', {
arguments: {
text: 0,
},
}),
JsExtractors.callExpression('n__', {
arguments: {
text: 0,
textPlural: 1,
},
}),
JsExtractors.callExpression('s__', {
arguments: {
text: 0,
},
}),
]);
2021-03-11 19:13:27 +05:30
const vueParser = decorateJSParserWithVueSupport(jsParser, {
2021-04-17 20:07:23 +05:30
vue2TemplateCompiler,
2021-03-11 19:13:27 +05:30
});
2018-11-20 20:47:30 +05:30
function printJson() {
2021-04-17 20:07:23 +05:30
const messages = extractor.getMessages().reduce((acc, message) => {
let { text } = message;
2018-11-20 20:47:30 +05:30
if (message.textPlural) {
text += `\u0000${message.textPlural}`;
}
2021-03-08 18:12:59 +05:30
message.references.forEach((reference) => {
2018-11-20 20:47:30 +05:30
const filename = reference.replace(/:\d+$/, '');
2021-04-17 20:07:23 +05:30
if (!Array.isArray(acc[filename])) {
acc[filename] = [];
2018-11-20 20:47:30 +05:30
}
2021-04-17 20:07:23 +05:30
acc[filename].push([text, reference]);
2018-11-20 20:47:30 +05:30
});
2021-04-17 20:07:23 +05:30
return acc;
2018-11-20 20:47:30 +05:30
}, {});
console.log(JSON.stringify(messages));
}
2021-04-17 20:07:23 +05:30
async function main() {
if (args.file) {
return vueParser.parseFile(args.file).then(() => printJson());
}
if (args.all) {
return vueParser
.parseFilesGlob('{ee/app,app}/assets/javascripts/**/*.{js,vue}')
.then(() => printJson());
}
throw new Error('ERROR: Please use the script correctly:');
}
main().catch((error) => {
console.warn(error.message);
2018-11-20 20:47:30 +05:30
args.outputHelp();
process.exit(1);
2021-04-17 20:07:23 +05:30
});