2018-08-16 15:03:57 -04:00
|
|
|
const argumentsParser = require('commander');
|
|
|
|
|
|
|
|
const { GettextExtractor, JsExtractors } = require('gettext-extractor');
|
|
|
|
const {
|
|
|
|
decorateJSParserWithVueSupport,
|
|
|
|
decorateExtractorWithHelpers,
|
|
|
|
} = require('gettext-extractor-vue');
|
2021-03-10 10:09:11 -05:00
|
|
|
const vue2TemplateCompiler = require('vue-template-compiler');
|
2021-07-01 02:07:35 -04:00
|
|
|
const ensureSingleLine = require('../../app/assets/javascripts/locale/ensure_single_line');
|
2018-08-16 15:03:57 -04:00
|
|
|
|
2018-08-19 03:16:05 -04:00
|
|
|
const args = argumentsParser
|
2018-08-16 15:03:57 -04:00
|
|
|
.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-02-15 04:09:00 -05:00
|
|
|
const vueParser = decorateJSParserWithVueSupport(jsParser, {
|
2021-03-10 10:09:11 -05:00
|
|
|
vue2TemplateCompiler,
|
2021-02-15 04:09:00 -05:00
|
|
|
});
|
2018-08-16 15:03:57 -04:00
|
|
|
|
|
|
|
function printJson() {
|
2021-03-10 10:09:11 -05:00
|
|
|
const messages = extractor.getMessages().reduce((acc, message) => {
|
|
|
|
let { text } = message;
|
2018-08-16 15:03:57 -04:00
|
|
|
if (message.textPlural) {
|
|
|
|
text += `\u0000${message.textPlural}`;
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
message.references.forEach((reference) => {
|
2018-08-16 15:03:57 -04:00
|
|
|
const filename = reference.replace(/:\d+$/, '');
|
|
|
|
|
2021-03-10 10:09:11 -05:00
|
|
|
if (!Array.isArray(acc[filename])) {
|
|
|
|
acc[filename] = [];
|
2018-08-16 15:03:57 -04:00
|
|
|
}
|
|
|
|
|
2021-03-10 10:09:11 -05:00
|
|
|
acc[filename].push([text, reference]);
|
2018-08-16 15:03:57 -04:00
|
|
|
});
|
|
|
|
|
2021-03-10 10:09:11 -05:00
|
|
|
return acc;
|
2018-08-16 15:03:57 -04:00
|
|
|
}, {});
|
|
|
|
|
|
|
|
console.log(JSON.stringify(messages));
|
|
|
|
}
|
|
|
|
|
2021-03-10 10:09:11 -05:00
|
|
|
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-08-19 03:16:05 -04:00
|
|
|
args.outputHelp();
|
2018-08-16 15:03:57 -04:00
|
|
|
process.exit(1);
|
2021-03-10 10:09:11 -05:00
|
|
|
});
|