Skip to content

Commit

Permalink
Merge pull request #3219 from Automattic/add/js-pot-generator
Browse files Browse the repository at this point in the history
Add scripts to build pot file with PHP and JS texts
  • Loading branch information
renatho authored Jun 2, 2020
2 parents 1afc25d + b12c4b9 commit 955138a
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 307 deletions.
12 changes: 12 additions & 0 deletions babel.makepot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
'@automattic/calypso-build/babel/wordpress-element',
'@automattic/calypso-build/babel/default',
],
plugins: [
[
'@wordpress/babel-plugin-makepot',
{ output: 'lang/tmp-sensei-lms-js.pot' },
],
],
};
71 changes: 71 additions & 0 deletions bin/pot-dist-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const fs = require( 'fs' );

const PATH = './lang/sensei-lms.pot';

/**
* Additional references by RegExp.
*/
const additionalReferences = [
{
re: /^#: assets\/onboarding\//gm,
reference: '#: assets/dist/onboarding/index.js:1',
},
];

/**
* Run script to add new references.
*/
const run = () => {
fs.readFile( PATH, 'utf8', ( err, data ) => {
if ( err ) throw err;

saveChanges( addNewReferences( data ) );
} );
};

/**
* Save changes.
*
* @param {string} newPotString New content to be saved.
*/
const saveChanges = ( newPotString ) => {
fs.writeFile( PATH, newPotString, 'utf8', ( err ) => {
if ( err ) throw err;
} );
};

/**
* Add new references to the pot string.
*
* @param {string} potString Current pot string to be replaced.
*
* @return {string} Pot string after adding new references.
*/
const addNewReferences = ( potString ) => {
const potSplitter = '\n\n';
const splittedPot = potString.split( potSplitter );
const header = splittedPot[ 0 ];

const messages = splittedPot
.slice( 1 ) // Skip header.
.map( ( message ) => {
let newMessage = message;

additionalReferences.forEach( ( r ) => {
if ( ! message.match( r.re ) ) {
return;
}

newMessage = newMessage.replace(
/\n(?!#)/,
'\n' + r.reference + '\n'
);
} );

return newMessage;
} );

return [ header, ...messages ].join( potSplitter );
};

run();
111 changes: 44 additions & 67 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
*
*/

var checktextdomain = require( 'gulp-checktextdomain' );
var del = require( 'del' );
var exec = require( 'child_process' ).exec;
var gulp = require( 'gulp' );
var sort = require( 'gulp-sort' );
var wpPot = require( 'gulp-wp-pot' );
var zip = require( 'gulp-zip' );
var process = require( 'process' );

function npm_run ( command ) {
var npmProcess = exec( `npm run ${command}` );
var del = require( 'del' );
var exec = require( 'child_process' ).exec;
var gulp = require( 'gulp' );
var zip = require( 'gulp-zip' );
var process = require( 'process' );

function npm_run( command ) {
var npmProcess = exec( `npm run ${ command }` );
npmProcess.stdout.pipe( process.stdout );
npmProcess.stderr.pipe( process.stderr );
return npmProcess;
Expand All @@ -31,7 +28,7 @@ var paths = {
select2: [
'node_modules/select2/dist/css/select2.min.css',
'node_modules/select2/dist/js/select2.full.js',
'node_modules/select2/dist/js/select2.full.min.js'
'node_modules/select2/dist/js/select2.full.min.js',
],
packageContents: [
'assets/**/*',
Expand All @@ -49,85 +46,65 @@ var paths = {
'wpml-config.xml',
],
packageDir: 'build/sensei-lms',
packageZip: 'build/sensei-lms.zip'
packageZip: 'build/sensei-lms.zip',
};

gulp.task( 'clean', gulp.series( function( cb ) {
return del( [
'assets/dist/**',
'assets/vendor/select2/**',
'build'
], cb );
} ) );

function buildWebpack () {
return npm_run( 'build' )
gulp.task(
'clean',
gulp.series( function( cb ) {
return del(
[ 'assets/dist/**', 'assets/vendor/select2/**', 'build' ],
cb
);
} )
);

function buildWebpack() {
return npm_run( 'build' );
}

gulp.task( 'webpack', gulp.series( buildWebpack ) );

gulp.task( 'block-editor-assets', gulp.series( function( cb ) {
exec( 'npm run block-editor-assets', cb );
} ) );

gulp.task( 'pot', gulp.series( function() {
return gulp.src( [ '**/**.php', '!node_modules/**', '!vendor/**', '!build/**', '!tests/**' ] )
.pipe( sort() )
.pipe( wpPot( {
domain: 'sensei-lms',
bugReport: 'https://translate.wordpress.org/projects/wp-plugins/sensei-lms/'
} ) )
.pipe( gulp.dest( 'lang/sensei-lms.pot' ) );
} ) );

gulp.task( 'textdomain', gulp.series( function() {
return gulp.src( [ '**/*.php', '!node_modules/**', '!build/**' , '!vendor/**' ] )
.pipe( checktextdomain( {
text_domain: 'sensei-lms',
keywords: [
'__:1,2d',
'_e:1,2d',
'_x:1,2c,3d',
'esc_html__:1,2d',
'esc_html_e:1,2d',
'esc_html_x:1,2c,3d',
'esc_attr__:1,2d',
'esc_attr_e:1,2d',
'esc_attr_x:1,2c,3d',
'_ex:1,2c,3d',
'_n:1,2,4d',
'_nx:1,2,4c,5d',
'_n_noop:1,2,3d',
'_nx_noop:1,2,3c,4d'
]
} ) );
} ) );
gulp.task(
'block-editor-assets',
gulp.series( function( cb ) {
exec( 'npm run block-editor-assets', cb );
} )
);

gulp.task( 'vendor', function() {
return gulp.src( paths.select2 )
return gulp
.src( paths.select2 )
.pipe( gulp.dest( 'assets/vendor/select2' ) );
} );

gulp.task( 'test', gulp.series( function npm_test ( ) {
npm_run( 'test' )
} ) );
gulp.task(
'test',
gulp.series( function npm_test() {
npm_run( 'test' );
} )
);

gulp.task( 'build', gulp.series( 'test', 'clean', 'webpack', 'vendor' ) );
gulp.task( 'build-unsafe', gulp.series( 'clean', 'webpack', 'vendor' ) );


gulp.task( 'copy-package', function() {
return gulp.src( paths.packageContents, { base: '.' } )
return gulp
.src( paths.packageContents, { base: '.' } )
.pipe( gulp.dest( paths.packageDir ) );
} );

gulp.task( 'zip-package', function() {
return gulp.src( paths.packageDir + '/**/*', { base: paths.packageDir + '/..' } )
return gulp
.src( paths.packageDir + '/**/*', { base: paths.packageDir + '/..' } )
.pipe( zip( paths.packageZip ) )
.pipe( gulp.dest( '.' ) );
} );

gulp.task( 'package', gulp.series( 'build', 'copy-package', 'zip-package' ) );
gulp.task( 'package-unsafe', gulp.series( 'build-unsafe', 'copy-package', 'zip-package' ) );
gulp.task(
'package-unsafe',
gulp.series( 'build-unsafe', 'copy-package', 'zip-package' )
);

gulp.task( 'default', gulp.series( 'build' ) );
12 changes: 11 additions & 1 deletion includes/admin/class-sensei-onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public function register_wizard_page() {
* @access private
*/
public function enqueue_scripts() {
Sensei()->assets->enqueue( 'sensei-setupwizard', 'onboarding/index.js', [], true );
Sensei()->assets->enqueue( 'sensei-setupwizard', 'onboarding/index.js', [ 'wp-i18n' ], true );
$this->setup_wizard_set_script_translations();
}

/**
Expand All @@ -139,6 +140,15 @@ public function enqueue_styles() {
Sensei()->assets->enqueue( 'sensei-setupwizard', 'onboarding/style.css', [ 'wp-components' ] );
}

/**
* Set script translations.
*
* @access private
*/
public function setup_wizard_set_script_translations() {
wp_set_script_translations( 'sensei-setupwizard', 'sensei-lms' );
}

/**
* Add global classes for Setup Wizard page.
*
Expand Down
Loading

0 comments on commit 955138a

Please sign in to comment.