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

Update to support makepot logic run against JavaScript files #6

Closed
bobbingwide opened this issue Jul 27, 2020 · 3 comments
Closed

Update to support makepot logic run against JavaScript files #6

bobbingwide opened this issue Jul 27, 2020 · 3 comments
Assignees
Labels
enhancement Gutenberg Required for the WordPress block editor - Gutenberg

Comments

@bobbingwide
Copy link
Owner

bobbingwide commented Jul 27, 2020

The WordPress block editor ( Gutenberg ) allows plugins and themes to be developed in JavaScript. Text strings written in the JavaScript (REACT) files should be translatable. WP-cli provides an i18n make-pot subcommand that enables the PHP and JavaScript strings used in the WordPress approved internationalization functions to be extracted to the main .pot file.
It also provides the i18n make-json subcommand to extract the Javascript strings from translated .po files into 'json files.
See notes to documentation below.

Requirement

Proposed solution

There are a couple of alternative

  • Change the do_makeoik() function to call the same logic as wp-cli
  • Change the do_makeoik() function to not bother to rebuild the .pot file that was generated using the wp-cli method.

Notes:

  • In plugins which generate blocks we can add the npm run makepot command to build the main .pot file

package.json would include a line like this

"makepot": "wp i18n make-pot . languages/sb-breadcrumbs-block.pot --exclude=node_modules,vendor,build"

See also https://developer.wordpress.org/cli/commands/i18n/make-json/
to find out if we can use the i18n make-json subcommand instead.
....We only need to enqueue the strings that are needed in the editor.

and https://getwithgutenberg.com/2019/04/how-to-internationalize-your-block/

Original documentation which talks about md5... but which I don't understand...

https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/

@bobbingwide bobbingwide added enhancement Gutenberg Required for the WordPress block editor - Gutenberg labels Jul 27, 2020
@bobbingwide bobbingwide self-assigned this Jul 27, 2020
@bobbingwide
Copy link
Owner Author

bobbingwide commented Jul 28, 2020

package.json would include a line like this

"makepot": "wp i18n make-pot . languages/sb-breadcrumbs-block.pot --exclude=node_modules,vendor,build"

Further investigation indicated that the .js files that we need to scan are those in the build directory, not the src ( source ).
So we need to change the exclude= parameter.

"makepot": "wp i18n make-pot . languages/sb-breadcrumbs-block.pot --exclude=node_modules,vendor,src"

The semi-automatic process for localization into en_GB and bb_BB locales is:

Command Explanation
npm start or npm run build to build the block: target file build/index.js
npm run makepot to build the main .pot file in the languages folder
l10n my localization routine to build .po and .mo files for en_GB and bb_BB in languages folder
npm run makejson to build the .json file(s) to match the build.index.js script; one for each locale.

For the sb-breadcrumbs-block plugin the target JSON files are:

  • sb-breadcrumbs-block-bb_BB-dfbff627e6c248bcb3b61d7d06da9ca9.json
  • sb-breadcrumbs-block-en_GB-dfbff627e6c248bcb3b61d7d06da9ca9.json

where md5 for build/index.js is dfbff627e6c248bcb3b61d7d06da9ca9

Using this process means I don't need to rename the .json files in order for WordPress to find them.
The code that's needed to localise the JavaScript file that's enqueued in the block editor is:

/*
 * Localise the script by loading the required strings for the build/index.js file
 * from the locale specific .json file in the languages folder
 */
	$ok = wp_set_script_translations( 'sb-breadcrumbs-block-block-editor', 'sb-breadcrumbs-block' , $dir .'/languages'  );

where

  • 'sb-breadcrumbs-block-block-editor' is the handle used to register the script.
  • 'sb-breadcrumbs-block' is the text domain
  • $dir.'/languages' - is the fully qualified path the the languages folder

@bobbingwide
Copy link
Owner Author

bobbingwide commented Aug 7, 2020

Change the do_makeoik() function to not bother to rebuild the .pot file that was generated using the wp-cli method.

For the sb-children-block plugin I changed the logic to skip calling do_makeoik().
In the future the logic should be changed to test for the presence of a node_modules folder which suggests the presence of Javascript that needs to be translated.

Update 2020/09/04
The logic will need to take into account plugins where the blocks are not built from the src folder.
The function is maybe_do_makeoik

node_modules src Process to use
n n makeoik
n y makeoik -
y n makeoik - but JavaScipt blocks are not internationalised
y y npm run makepot & npm run l10n & npm run makejson

Additional functions are needed to copy the plugin's .pot file to and from oik-i18n/working depending on which process is used to build the file.

Note: In the plugin's package.json file npm run makepot needs to be configured to ignore the src folder and use the build folder for the source of .js files to be searched for strings.

@bobbingwide
Copy link
Owner Author

I believe this can now be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Gutenberg Required for the WordPress block editor - Gutenberg
Projects
None yet
Development

No branches or pull requests

1 participant