Skip to content

Commit

Permalink
1.2.1: Fix for noop-functions and added more tests.
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
Rasmus Bengtsson committed Apr 22, 2016
1 parent 5297e7c commit 1a27316
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function hasContext(key) {
return /(_x|_ex|esc_attr_x|esc_html_x|_nx|_nx_noop)/.test(key);
}

function isNoop(key) {
return /(_nx_noop|_n_noop)/.test(key);
}

/**
* Get key chain.
*
Expand Down Expand Up @@ -188,10 +192,17 @@ function translationToPot(buffer) {

output.push('#: ' + buffer[el].info.replace(/\\/g, '/'));

if (!isPlural(key) && hasContext(key)) {
output.push('msgctxt "' + buffer[el].functionArgs[1] + '"');
} else if (isPlural(key) && hasContext(key)) {
output.push('msgctxt "' + buffer[el].functionArgs[3] + '"');
if (hasContext(key)) {
var argKey = 1;
if (isPlural(key)) {
argKey = argKey + 2;
}

if (isNoop(key)) {
argKey = argKey - 1;
}

output.push('msgctxt "' + buffer[el].functionArgs[argKey] + '"');
}

if (/\n/.test(buffer[el].functionArgs[0])) {
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-wp-pot",
"version": "1.2.0",
"version": "1.2.1",
"description": "Generate pot-files for WordPress localization",
"license": "MIT",
"repository": "rasmusbe/gulp-wp-pot",
Expand Down
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('generate tests', function () {
assert(file.isBuffer());
var fileContents = file.contents.toString();
assert(fileContents.indexOf("msgctxt \"stars translation\"\n") !== -1);
assert(fileContents.indexOf("msgid \"%s star\"\n") !== -1);
assert(fileContents.indexOf("msgid_plural \"%s stars\"\n") !== -1);
done();
});
Expand Down Expand Up @@ -210,6 +211,24 @@ describe('generate tests', function () {
stream.write(testFile);
stream.end();
});

it ('should generate a pot file from php file with noop function', function (done) {
var testFile = new File({
contents: new Buffer('<?php _n_noop( "%s star", "%s stars", "test" ); ?>')
});
var stream = wpPot({
domain: 'test'
});
stream.once('data', function (file) {
assert(file.isBuffer());
var fileContents = file.contents.toString();
assert(fileContents.indexOf("msgid \"%s star\"\n") !== -1);
assert(fileContents.indexOf("msgid_plural \"%s stars\"\n") !== -1);
done();
});
stream.write(testFile);
stream.end();
});
});

describe('domain tests', function () {
Expand Down Expand Up @@ -246,6 +265,25 @@ describe('domain tests', function () {
stream.write(testFile);
stream.end();
});

it ('should generate a pot file from php file with context and domain set as a constant', function (done) {
var testFile = new File({
contents: new Buffer('<?php _nx_noop( "%s star", "%s stars", "stars translation", TEST ); ?>')
});
var stream = wpPot({
domain: 'TEST'
});
stream.once('data', function (file) {
assert(file.isBuffer());
var fileContents = file.contents.toString();
assert(fileContents.indexOf("msgctxt \"stars translation\"\n") !== -1);
assert(fileContents.indexOf("msgid \"%s star\"\n") !== -1);
assert(fileContents.indexOf("msgid_plural \"%s stars\"\n") !== -1);
done();
});
stream.write(testFile);
stream.end();
});
});

describe('header tests', function () {
Expand Down

0 comments on commit 1a27316

Please sign in to comment.