diff options
author | π΄πΏπ¦ Lukas Eipert (OOO until Summit) <leipert@gitlab.com> | 2018-08-16 19:03:57 +0000 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2018-08-16 19:03:57 +0000 |
commit | 8f9442fcdf9d98ea28e6abd582aad1dc2086e1bd (patch) | |
tree | 347faa88ea898062b131e047a77130f44333704b /scripts | |
parent | 76cd1dd68198bb464b12de7525ab067c6d4dc025 (diff) | |
download | gitlab-ce-8f9442fcdf9d98ea28e6abd582aad1dc2086e1bd.tar.gz |
Frontend: Proper gettext extraction with gettext-extractor
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/frontend/extract_gettext_all.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/scripts/frontend/extract_gettext_all.js b/scripts/frontend/extract_gettext_all.js new file mode 100644 index 00000000000..af8cc4c3341 --- /dev/null +++ b/scripts/frontend/extract_gettext_all.js @@ -0,0 +1,72 @@ +const argumentsParser = require('commander'); + +const { GettextExtractor, JsExtractors } = require('gettext-extractor'); +const { + decorateJSParserWithVueSupport, + decorateExtractorWithHelpers, +} = require('gettext-extractor-vue'); +const ensureSingleLine = require('../../app/assets/javascripts/locale/ensure_single_line.js'); + +const arguments = 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, + }, + }), +]); + +const vueParser = decorateJSParserWithVueSupport(jsParser); + +function printJson() { + const messages = extractor.getMessages().reduce((result, message) => { + let text = message.text; + if (message.textPlural) { + text += `\u0000${message.textPlural}`; + } + + message.references.forEach(reference => { + const filename = reference.replace(/:\d+$/, ''); + + if (!Array.isArray(result[filename])) { + result[filename] = []; + } + + result[filename].push([text, reference]); + }); + + return result; + }, {}); + + console.log(JSON.stringify(messages)); +} + +if (arguments.file) { + vueParser.parseFile(arguments.file).then(() => printJson()); +} else if (arguments.all) { + vueParser.parseFilesGlob('{ee/app,app}/assets/javascripts/**/*.{js,vue}').then(() => printJson()); +} else { + console.warn('ERROR: Please use the script correctly:'); + arguments.outputHelp(); + process.exit(1); +} |