Skip to content

Commit

Permalink
Improved stop condition for delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-nextbit committed Apr 1, 2016
1 parent 9beb715 commit 30194f5
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 10 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var defaults = {
locales: './locales',
delimiter: {
prefix: 'R.',
stopCondition: /[\-;,<>\{}()\[\]"'\s&$]/
stopCondition: /[^\.\w]/
},
filename: '${path}/${name}-${lang}.${ext}',
blacklist: [],
Expand Down Expand Up @@ -203,16 +203,17 @@ function translate(options, contents, copied) {
var i = contents.indexOf(options.delimiter.prefix);
while ((i !== -1)) {
var endMatch, length, token, key;
var tail = contents.substr(i);
if (options.delimiter.suffix) {
endMatch = contents.substr(i).match(options.delimiter.suffix);
endMatch = tail.match(options.delimiter.suffix);
length = endMatch.index + endMatch[0].length;
token = contents.substr(i, length);
token = tail.substr(0, length);
key = token.substr(options.delimiter.prefix.length, token.length - options.delimiter.prefix.length - options.delimiter.suffix.length);
}
else if (options.delimiter.stopCondition) {
endMatch = contents.substr(i).match(options.delimiter.stopCondition);
length = endMatch.index + endMatch[0].length - 1;
token = contents.substr(i, length);
endMatch = tail.match(options.delimiter.stopCondition);
length = endMatch == null ? tail.length : length = endMatch.index + endMatch[0].length - 1;
token = tail.substr(0, length);
key = token.substr(options.delimiter.prefix.length);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-international",
"version": "1.0.1",
"version": "1.0.2",
"description": "A gulp plugin that creates multi language versions of your source files",
"license": "Apache-2.0",
"homepage": "http://github.com/mallocator/gulp-international",
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Default:
```
{
prefix: 'R.',
stopCondition: /[\-;,\.<>\{}()\[\]"'\s$]/
stopCondition: /[^\.\w]/
}
```

Expand Down Expand Up @@ -209,15 +209,15 @@ Similar to JSON you can just use node.js module and export your json:
module.exports = {
token1: 'translation1',
section1: {
token2: 'translation2',
token2: 'translation2', // This format also allows comments
subsection1: {
token3: 'translation3'
}
}
}
```

The tokens map the same way as a JSON file would.
The tokens map the same way as a JSON file would. If you don't know which format to choose I would choose this one.


### CSV
Expand Down
29 changes: 29 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ describe('gulp-international', () => {

describe('Error cases', () => {

it('should work with A number of delimiters', done => {
var stopSignals = "\"'{}[]|~`:;,/!@#$%^&*()=+<>`";
var options = { locales: 'test/locales', whitelist: 'en_US' };
var processed = 0;
for (let i = 0; i < stopSignals.length; i++) {
var content = stopSignals[i] + "R.token1" + stopSignals[i];
helper(options, content, files => {
expect(files[0].contents.toString('utf8')).to.equal(stopSignals[i] + "content1" + stopSignals[i]);
processed++;
if (processed == stopSignals.length) {
done();
}
});
}
});


it('should leave files without tokens unprocessed', done => {
var content = '<html><body>Not replaced</body></html>';
var options = { locales: 'test/locales' };
Expand Down Expand Up @@ -249,6 +266,18 @@ describe('gulp-international', () => {
});


it('should be fast', done => {
var content = fs.readFileSync('test/locales/lorem.ipsum').toString('utf8');
var options = { locales: 'test/locales', whitelist: 'en_US' };
var start = process.hrtime();
helper(options, content, () => {
var end = process.hrtime(start);
expect(end[0] * 1e3 + end[1] * 1e-6).to.be.lt(25);
done();
});
});


it('should be able to process a larger file with multiple replacements', done => {
var content = `
<html>
Expand Down
Loading

0 comments on commit 30194f5

Please sign in to comment.