Skip to content

Commit

Permalink
Fix message extraction to handle multiline gettexts
Browse files Browse the repository at this point in the history
prettier will often break those
  • Loading branch information
brunobesson committed May 6, 2020
1 parent 0a9c1a5 commit 7186ac2
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions tools/extract-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const compiler = require('vue-template-compiler');
const NODETYPE_TEXT = 3;

const template_regex = /(<template>[\s\S]*<\/template>)/;
const gettext_template1 = /\$gettext\('((?:[^']|\\')*?)'\)/g;
const gettext_template2 = /\$gettext\('((?:[^']|\\')*?)', *'([^']*?)'\)/g;
const gettext_template1 = /\$gettext\(\s*'((?:[^']|\\')*?)'\s*\)/gm;
const gettext_template2 = /\$gettext\(\s*'((?:[^']|\\')*?)'\s*,\s*'([^']*?)'\s*\)/gm;

/**************************************************************************
a "Result" is a .pot item (msgctxt/msgid), with every associated meta-data
Expand Down Expand Up @@ -75,7 +75,7 @@ Process.prototype.push = function (file, line, msgctxt, msgid) {
this.data[key] = new Result(msgctxt, msgid);
}

const position = this.includeLineNumberInPositions ? `${file}:${line}` : file;
const position = this.includeLineNumberInPositions ? `${file}:${line || '?'}` : file;
this.data[key].addFile(position);
};

Expand All @@ -93,15 +93,12 @@ Process.prototype.addScript = function (file, data) {
Process.prototype.parseScript = function (file, data, regex) {
let msgData;

const lines = data.split('\n');
while ((msgData = regex.exec(data)) !== null) {
const msgid = msgData[1];
const msgctxt = msgData[2];
const line = (data.slice(0, msgData.index).match(/\n/g) || []).lengh;

for (let i = 0; i < lines.length; i++) {
while ((msgData = regex.exec(lines[i])) !== null) {
const msgid = msgData[1];
const msgctxt = msgData[2];

this.push(file, i + 1, msgctxt, msgid.replace(/\\'/g, "'"));
}
this.push(file, line, msgctxt, msgid.replace(/\\'/g, "'"));
}
};

Expand Down Expand Up @@ -130,11 +127,11 @@ Process.prototype.parseTemplate = function (file, data) {
let msgctxt;

if (node.children.length !== 1) {
throw new Error(`In ${position}\nNodes with v-translate directive must contains only one child`);
throw new Error(`In ${line}\nNodes with v-translate directive must contains only one child`);
}

if (node.children[0].type !== NODETYPE_TEXT) {
throw new Error(`In ${position}\nInterploation is not yet supported. Please use $gettext`);
throw new Error(`In ${line}\nInterploation is not yet supported. Please use $gettext`);
}

for (const attribute of node.attrsList) {
Expand Down

0 comments on commit 7186ac2

Please sign in to comment.