Skip to content

Add extraction support for translatePlural filter #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
12 changes: 9 additions & 3 deletions lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var mkAttrRegex = function (startDelim, endDelim) {
start = '^';
}

return new RegExp(start + '\\s*(\'|"|")(.*?)\\1\\s*\\|\\s*translate\\s*(' + end + '|\\|)', 'g');
return new RegExp(start + '\\s*(\'|"|")(.*?)\\1\\s*\\|\\s*translate(Plural:.*:\\1(.*?)\\1)?\\s*(' + end + '|\\|)', 'g');
};

var noDelimRegex = mkAttrRegex('', '');
Expand Down Expand Up @@ -205,8 +205,13 @@ var Extractor = (function () {
self.addString(filename, str, plural, extractedComment);
} else if (matches = noDelimRegex.exec(node.attr(attr))) {
str = matches[2].replace(/\\\'/g, '\'');
self.addString(filename, str);
plural = (typeof matches[4] === 'string') ? matches[4].replace(/\\\'/g, '\'') : undefined;
self.addString(filename, str, plural);
}

// Regex with global modifier keeps lastIndex
// Reset it to make sure all strings are checked from index 0
noDelimRegex.lastIndex = 0;
}

if (typeof node.attr('data-translate') !== 'undefined') {
Expand All @@ -219,7 +224,8 @@ var Extractor = (function () {
var matches;
while (matches = this.attrRegex.exec(src)) {
var str = matches[2].replace(/\\\'/g, '\'');
this.addString(filename, str);
var plural = (typeof matches[4] === 'string') ? matches[4].replace(/\\\'/g, '\'') : undefined;
this.addString(filename, str, plural);
}
};

Expand Down
28 changes: 27 additions & 1 deletion test/extract.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,30 @@ describe 'Extract', ->
]
catalog = testExtract(files)

assert.equal(catalog.items.length, 0)
assert.equal(catalog.items.length, 0)

it 'Extracts filter strings for translatePlural', ->
files = [
'test/fixtures/filter-plural.html'
]
catalog = testExtract(files)

# Notes on the ordering: Strings seem to be sorted alphabetically
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's by design.

assert.equal(catalog.items.length, 3)
assert.equal(catalog.items[0].msgid, '1 coin')
assert.equal(catalog.items[0].msgid_plural, '{} coins')
assert.equal(catalog.items[0].msgstr.length, 2)
assert.equal(catalog.items[0].references.length, 1)
assert.equal(catalog.items[0].references[0], 'test/fixtures/filter-plural.html')

assert.equal(catalog.items[1].msgid, 'One file selected')
assert.equal(catalog.items[1].msgid_plural, '{} files selected')
assert.equal(catalog.items[1].msgstr.length, 2)
assert.equal(catalog.items[1].references.length, 1)
assert.equal(catalog.items[1].references[0], 'test/fixtures/filter-plural.html')

assert.equal(catalog.items[2].msgid, 'You have an unread message')
assert.equal(catalog.items[2].msgid_plural, 'You have {} unread messages')
assert.equal(catalog.items[2].msgstr.length, 2)
assert.equal(catalog.items[2].references.length, 1)
assert.equal(catalog.items[2].references[0], 'test/fixtures/filter-plural.html')
Loading