Skip to content

Instantly share code, notes, and snippets.

@sareiodata
Created August 5, 2020 05:59
Show Gist options
  • Save sareiodata/ed439d15b6a9a50a2d9ff4af28847634 to your computer and use it in GitHub Desktop.
Save sareiodata/ed439d15b6a9a50a2d9ff4af28847634 to your computer and use it in GitHub Desktop.
var gulp = require('gulp'),
pot = require( 'gulp-wp-pot' ),
fs = require('fs'),
git = require('gulp-git');
var addons = [ 'pms-add-on-bbpress', 'pms-add-on-content-dripping', 'pms-add-on-discount-codes', 'pms-add-on-email-reminders',
'pms-add-on-global-content-restriction', 'pms-add-on-invoices', 'pms-add-on-member-subscription-fixed-period', 'pms-add-on-multiple-subscriptions-per-user'
, 'pms-add-on-navigation-menu-filtering', 'pms-add-on-paypal-express-pro', 'pms-add-on-paypal-standard-recurring-payments', 'pms-add-on-recaptcha'
, 'pms-add-on-stripe', 'pms-add-on-pay-what-you-want', 'pms-add-on-group-memberships', 'pms-add-on-tax' ];
/* create a pot file from the main plugin and all the addons */
gulp.task( 'pot', function () {
//make sure we are in the original working directory
process.chdir('./../paid-member-subscriptions');
//create all the paths in witch we look for gettext
lookIn = [ 'extend/**/*.php','includes/**/*.php', '*.php' ];
addons.forEach(function(addon){
lookIn.push( './../'+ addon +'/**/*.php' );
});
return gulp.src( lookIn )
.pipe( pot({
domain: 'paid-member-subscriptions',
package: 'Paid Member Subscriptions'
}) )
.pipe( gulp.dest( 'translations/paid-member-subscriptions.pot' ) );
});
//create a php catalog that contain all the strings in the pot. this will reside in the translation folder and is used for translate.wordpress.org when they scan for the strings and the string is not in the free version
gulp.task('catalog', function(cb) {
var fileContent = fs.readFileSync("translations/paid-member-subscriptions.pot", "utf8");
returnFile = '';
regex = new RegExp( /msgid ([^]*?)msgstr/g ); //grab everything between msgid and msgstr
match = regex.exec(fileContent.toString());
while (match != null) {
if( typeof match[1] != 'undefined' ) {
match[1] = match[1].replace( /(?:\r\n|\r|\n)/g, '' ); //remove the new lines inserted by pot
match[1] = match[1].replace( /(?<!\\|^)""/g, '' ); //recombine in a single string by removing the ""
// Check if row contains named printf placeholders
var matches = match[1].match(/%(\d)\$/g)
if( matches != null ){
// string is "This is my string"
// this removes the double quotes around it so we can add single quotes later
// double quotes are changing the meaning of strings that contain sprintf variables (%1$s)
match[1] = match[1].replace( /["]+/g, "" )
// escape ' that are inside the string
match[1] = match[1].replace( /[']+/g, "\\'" )
returnFile += "<?php __('" + match[1] + "', 'paid-member-subscriptions' ); ?>\n"
} else
returnFile += '<?php __(' + match[1] + ', "paid-member-subscriptions"); ?>\n'
}
match = regex.exec(fileContent.toString());
}
returnFile = returnFile.replace( /msgid_plural /g, ', "paid-member-subscriptions"); ?>\n<?php __(' );
fs.writeFile( 'translations/paid-member-subscriptions.catalog.php', returnFile, cb );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment