Skip to content
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

Make pot from WP CLI implemented #1455

Open
wants to merge 3 commits into
base: dev
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
7 changes: 2 additions & 5 deletions classes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ public function admin_scripts() {
// @since 3.0.0 add tax react app on the settings page.
if ( 'tutor_settings' === $page && ! Input::has( 'edit' ) ) {
wp_enqueue_script( 'tutor-shared', tutor()->url . 'assets/js/tutor-shared.min.js', array( 'wp-i18n', 'wp-element' ), TUTOR_VERSION, true );
wp_enqueue_script( 'tutor-tax-settings.min', tutor()->url . 'assets/js/tutor-tax-settings.min.js', array( 'wp-i18n', 'wp-element', 'tutor-shared' ), TUTOR_VERSION, true );
wp_enqueue_script( 'tutor-payment-settings.min', tutor()->url . 'assets/js/tutor-payment-settings.min.js', array( 'wp-i18n', 'wp-element', 'tutor-shared' ), TUTOR_VERSION, true );
wp_enqueue_script( 'tutor-tax-settings', tutor()->url . 'assets/js/tutor-tax-settings.min.js', array( 'wp-i18n', 'wp-element', 'tutor-shared' ), TUTOR_VERSION, true );
wp_enqueue_script( 'tutor-payment-settings', tutor()->url . 'assets/js/tutor-payment-settings.min.js', array( 'wp-i18n', 'wp-element', 'tutor-shared' ), TUTOR_VERSION, true );
}
}
}
Expand Down Expand Up @@ -591,9 +591,6 @@ public function tutor_generator_tag( $gen, $type ) {
public function tutor_script_text_domain() {
wp_set_script_translations( 'tutor-frontend', 'tutor', tutor()->path . 'languages/' );
wp_set_script_translations( 'tutor-admin', 'tutor', tutor()->path . 'languages/' );
wp_set_script_translations( 'tutor-order-details', 'tutor', tutor()->path . 'languages/' );
wp_set_script_translations( 'tutor-tax-settings', 'tutor', tutor()->path . 'languages/' );
wp_set_script_translations( 'tutor-coupon', 'tutor', tutor()->path . 'languages/' );
}

/**
Expand Down
90 changes: 13 additions & 77 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ for (let task in scss_blueprints) {
});
}

var added_texts = [];
const regex = /__\(\s*(['"])((?:(?!(?<!\\)\1).)+)\1(?:,\s*(['"])((?:(?!(?<!\\)\3).)+)\3)?\s*\)/gi;
const js_files = [
'tutor',
'tutor-front',
Expand All @@ -83,67 +81,22 @@ const js_files = [
'tutor-course-builder',
'tutor-order-details',
'tutor-tax-settings',
'tutor-payment-settings',
'tutor-coupon'
].map((f) => 'assets/js/' + f + '.min.js:1').join(', ');
].map((f) => `#: assets/js/${f}.min.js:1`).join('\n');

function i18n_makepot(callback, target_dir) {
const parent_dir = target_dir || __dirname;
var translation_texts = '';
// Replace js file locations with these min.js files
function replace_pot_string(callback) {
const potFilePath = path.join(__dirname, 'languages', 'tutor.pot');
let potFileContent = fs.readFileSync(potFilePath, 'utf8');
potFileContent = potFileContent.replace(
/(?:^#:\s*.*\.(?:js|min\.js):\d+\n)+(^msgid ".*"\nmsgstr ""\n)/gm,
(_, msgBlock) => `${js_files}\n${msgBlock}`
);

// Loop through JS files inside js directory
fs.readdirSync(parent_dir).forEach(function (file_name) {
if (file_name == 'node_modules' || file_name.indexOf('.') === 0) {
return;
}

var full_path = parent_dir + '/' + file_name;
var stat = fs.lstatSync(full_path);

if (stat.isDirectory()) {
i18n_makepot(null, full_path);
return;
}

// Make sure only js and ts extension file to process
const extensions = ['.js', '.ts', '.tsx'];
if (stat.isFile() && (extensions.includes(path.extname(file_name))) &&
(full_path.indexOf('assets/react') > -1 || full_path.indexOf('v2-library/src') > -1)
) {
var codes = fs.readFileSync(full_path).toString();
var lines = codes.split('\n');

// Loop through every single line in the JS file
for (var i = 0; i < lines.length; i++) {
var found = lines[i].match(regex);
!Array.isArray(found) ? (found = []) : 0;

// Loop through found translations
for (var n = 0; n < found.length; n++) {
// Parse the string

var string = found[n];
var delimeter = string[3] == ' ' ? string[4] : string[3];
var first_quote = string.indexOf(delimeter) + 1;
var second_quote = string.indexOf(delimeter, first_quote);
var text = string.slice(first_quote, second_quote);
fs.writeFileSync(potFilePath, potFileContent, 'utf8');

if (added_texts.indexOf(text) > -1) {
// Avoid duplicate entry
continue;
}

added_texts.push(text);
translation_texts += '\n#: ' + js_files + '\nmsgid "' + text + '"\nmsgstr ""' + '\n';
}
}
}
});

// Finally append the texts to the pot file
var text_domain = path.basename(__dirname);
fs.appendFileSync(__dirname + '/languages/' + text_domain.toLowerCase() + '.pot', translation_texts);

callback ? callback() : 0;
if (callback) callback();
}

gulp.task('watch', function () {
Expand All @@ -164,23 +117,6 @@ gulp.task('watch', function () {
});
});

gulp.task('makepot', function () {
return gulp
.src('**/*.php')
.pipe(
plumber({
errorHandler: onError,
}),
)
.pipe(
wpPot({
domain: 'tutor',
package: 'Tutor LMS',
}),
)
.pipe(gulp.dest('languages/tutor.pot'));
});

/**
* Build
*/
Expand Down Expand Up @@ -278,6 +214,6 @@ gulp.task('make-zip', function () {
/**
* Export tasks
*/
exports.build = gulp.series(...task_keys, 'clean-zip', 'clean-build', 'makepot', i18n_makepot, 'copy', 'copy-fonts', 'copy-tutor-droip', 'make-zip', 'clean-build');
exports.build = gulp.series(...task_keys, 'clean-zip', 'clean-build', replace_pot_string, 'copy', 'copy-fonts', 'copy-tutor-droip', 'make-zip', 'clean-build');
exports.sass = gulp.parallel(...task_keys);
exports.default = gulp.parallel(...task_keys, 'watch');
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"node": ">=14.17.5"
},
"scripts": {
"build": "composer install --no-dev && webpack --env=build --mode=production && npm run tutor-droip && npm run gulp-build",
"build": "composer install --no-dev && webpack --env=build --mode=production && npm run tutor-droip && npm run make-pot && npm run gulp-build",
"build-dev": "webpack --mode=development && npm run gulp-build-dev",
"gulp-build": "npm run rm-lang && _GULP_ENV='build' gulp build",
"gulp-build": "_GULP_ENV='build' gulp build",
"gulp-build-dev": "npm run rm-lang && gulp build",
"css-watch": "gulp",
"js-watch": "webpack --watch --mode=development",
"watch": "concurrently \"npm run css-watch\" \"npm run js-watch\"",
"rm-lang": "rm ./languages/tutor.pot || echo \"No tutor pot yet\"",
"make-pot": "wp i18n make-pot . ./languages/tutor.pot --slug=tutor --domain=tutor --include='**/*.php,assets/**/*.js' --exclude='node_modules,vendor,build,tutor-droip,assets/lib'",
"biome:check": "npx @biomejs/biome check --apply ./assets/react/v3",
"tutor-droip": "cd tutor-droip && npm run build && cp -r dist/* ../tutor-droip"
},
Expand Down
Loading