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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 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');
2023-03-17 16:20:25 +05:30
const ensureSingleLine = require('../../app/assets/javascripts/locale/ensure_single_line.cjs');
2018-11-20 20:47:30 +05:30
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:
2023-06-20 00:43:36 +05:30
JsExtractors.callExpression(['__', 's__'], {
2018-11-20 20:47:30 +05:30
arguments: {
text: 0,
},
}),
JsExtractors.callExpression('n__', {
arguments: {
text: 0,
textPlural: 1,
},
}),
]);
2021-03-11 19:13:27 +05:30
const vueParser = decorateJSParserWithVueSupport(jsParser, {
2021-04-17 20:07:23 +05:30
vue2TemplateCompiler,
2023-06-20 00:43:36 +05:30
// All of our expressions contain `__`.
// So we can safely ignore parsing files _not_ containing it.
guard: '__',
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
});