Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function Parser(customKeywordSpec) {
// literals).
`(?:${stringPatternGroup})`,
// Optional comma for multiple parameters.
'(?:\\s*,\\s*)?',
'(?:\\s*,\\s*)?((?:\\\\.|[^()\\\\])*)?',
')+',
')',
].join(''), 'g');
Expand All @@ -97,12 +97,13 @@ function Parser(customKeywordSpec) {
Parser.prototype.parse = function parse(template) {
const result = {};
let match;

// eslint-disable-next-line no-cond-assign
while ((match = this.expressionPattern.exec(template)) !== null) {
const keyword = match[1];
const params = match[2].match(this.stringPattern).map(trim).map(trimQuotes).map(unescapeQuotes);

if(match[6]) {
params.push(match[6].trim())
}
const spec = this.keywordSpec[keyword];
const msgidIndex = spec.msgid;
const msgid = params[msgidIndex];
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/singular.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<p>{{ $t("\"double escape\"") }}</p>
<p>{{ $t("word \"escaped, word\", with comma") }}</p>
<p>{{ $t("ending with an escaped quote\"") }}</p>
<p>{{ $ngettext('plural string', num) }}</p>
</div>
</template>

Expand All @@ -15,8 +16,9 @@
return {
t1: this.$t('data \'single escape\''),
t2: this.$t("data \"double escape\""),
t3: this.$t("Description")
t3: this.$t("Description"),
num: 5
};
}
}
</script>
</script>
38 changes: 20 additions & 18 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ const fs = require('fs');
const test = require('ava');

test('default', t => {
t.deepEqual((new Parser()).keywordSpec.$t, { msgid : 0 },
'should have default keyword spec when none is passed');
t.deepEqual((new Parser()).keywordSpec.$t, { msgid: 0 },
'should have default keyword spec when none is passed');
});

test.cb('singular', t => {
fs.readFile(__dirname + '/fixtures/singular.vue', {encoding: 'utf8'}, (err, data) => {
if (err) throw err;
fs.readFile(__dirname + '/fixtures/singular.vue', { encoding: 'utf8' }, (err, data) => {
if (err) throw err;

const result = (new Parser()).parse(data);
t.is(typeof result, 'object');
t.true('This is a title' in result);
t.true('This is wrapped in single quotes' in result);
t.true('\'single escape\'' in result);
t.true('"double escape"' in result);
t.true('word "escaped, word", with comma' in result);
t.true('ending with an escaped quote"' in result);
t.true('data \'single escape\'' in result);
t.true('data "double escape"' in result);
t.true('Description' in result);
t.is(result['Description'].line.length, 3);
t.end();
});
const result = (new Parser()).parse(data);
t.is(typeof result, 'object');
t.true('This is a title' in result);
t.true('This is wrapped in single quotes' in result);
t.true('\'single escape\'' in result);
t.true('"double escape"' in result);
t.true('word "escaped, word", with comma' in result);
t.true('ending with an escaped quote"' in result);
t.true('data \'single escape\'' in result);
t.true('data "double escape"' in result);
t.true('Description' in result);
t.true('plural string' in result);
t.true(result['plural string'].plural==='num')
t.is(result['Description'].line.length, 3);
t.end();
});
});