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

Adding SDK POT file generation for Gutenberg blocks. #30303

Closed
wants to merge 4 commits into from

Conversation

zinigor
Copy link
Member

@zinigor zinigor commented Jan 22, 2019

Fixes #30048

Changes proposed in this Pull Request

  • Adds the babel-plugin-makepot plugin to the extensions folder for Gutenberg blocks. This is needed to be able to translate blocks in third party code.

Testing instructions

  • Run the SDK using this command: node bin/sdk-cli.js gutenberg client/gutenberg/extensions/presets/jetpack/ --output-dir /some/folder/you/want/blocks/to/be/put
  • See that alongside editor.js and all other files for Gutenberg blocks you have extensions.pot file generated, and it contains strings from the JSX files.

Now that I look at this PR, I can see that there might be a better approach, one that does not add the makepot plugin to everything, but relies on an environment variable (or other switch) that will be set when SDK runs. I'd appreciate any ideas here.

@zinigor zinigor added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] In Progress i18n labels Jan 22, 2019
@matticbot
Copy link
Contributor

@zinigor zinigor requested review from ockham, akirk and jeherve January 22, 2019 13:07
babel.config.js Outdated Show resolved Hide resolved
@zinigor
Copy link
Member Author

zinigor commented Jan 23, 2019

Thanks for the review, @simison ! I have changed the variable name to use camelCase, as well as made the plugin only be used on SDK runs to avoid generating useless POT files when building normally.

@zinigor zinigor added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Jan 23, 2019
babel.config.js Outdated Show resolved Hide resolved
];

// The output directory is set when SDK runs, so we only set the POT generator override in that case.
if ( outputDir ) {
Copy link
Member

@simison simison Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with the comment, I would still prefer the code to tell this story better by renaming the variable something less generic. Not a biggie tho if you decide to leave it like this!

Consider someone who doesn't know anything about SDK reading this line.

Maybe hasSdkOutputDir ?

@simison simison requested a review from a team January 24, 2019 12:10
@simison
Copy link
Member

simison commented Jan 24, 2019

Just noting that I'm observing strings from both beta and production blocks ending up to the same file — suppose that's the goal so that we can get blocks earlier for translating than they're shipped.

Full list of prod + beta blocks:

"production": [
"contact-form",
"map",
"markdown",
"publicize",
"related-posts",
"shortlinks",
"simple-payments",
"subscriptions",
"tiled-gallery"
],
"beta": [
"mailchimp",
"giphy",
"vr"
]

@simison
Copy link
Member

simison commented Jan 24, 2019

The babel plugin works great! I get the pot file when building Jetpack preset with --output-dir CLI argument.

One thing that doesn't work right now is when running gutenberg command without --output-dir (it's optional arg):

npm run sdk gutenberg ./client/gutenberg/extensions/presets/jetpack/

That would place files in git-ignored folder ./client/gutenberg/extensions/presets/jetpack/build

I get:

ENOENT: no such file or directory, open 'undefined/extensions.pot'

You can build any block in extensions directry this way, not just presets:

npm run sdk gutenberg ./client/gutenberg/extensions/hello-dolly/

Suppose current setup where pot files is built only with --output-dir would serve our goal of just getting the pot-file to Jetpack plugin directory (in which case the command we indeed use is:

npm run sdk gutenberg ./client/gutenberg/extensions/presets/jetpack/ --output-dir=~/jetpack/_inc/blocks

...but since the SDK and gutenberg build command are kinda generic, the behaviour where pot file gets generated sometimes and sometimes not, can be surprising. I'd suggest we generate it always when dealing with gutenberg command. Another option could be generate it only when dealing with presets folder but that might be confusing as well.

That would require setting the CALYPSO_SDK_OUTPUT_DIR from bin/sdk/gutenberg.js, since that's where output gets finally defined like so:

path: outputDir || path.join( inputDir, 'build' ),

I would also consider having filename something else than "extensions" — maybe just "translations" or the foldername "jetpack" for preset or block name for the rest?

Another aspect is that there is also notifications SDK command which also has output-dir argument: since the CALYPSO_SDK_OUTPUT_DIR sounds pretty generic and is set in bin/sdk-cli.js file, I'd expect it to handle notifications (and any other) build command as well. Since the plugin is gated to extensions folder, it's not the case. I think it's okay if we make this gutenberg extensions specific for now and make it clear by moving CALYPSO_SDK_OUTPUT_DIR to bin/sdk/gutenberg.js — what do you think?

I was also thinking if we should have pot-generation turned on only as opt-in via separate CLI argument, instead of generate on each run. It doesn't seem to slow the build at all and it's nice not to think of CLI arguments, so current way seems good!

@@ -13,6 +13,9 @@ const path = require( 'path' );
const yargsModule = require( 'yargs' );
const webpack = require( 'webpack' );

// This environment variable will be used by the Babel plugin that makes POT files, see babel.config.js.
process.env.CALYPSO_SDK_OUTPUT_DIR = yargsModule.argv.outputDir;
Copy link
Member

@simison simison Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To express #30303 (comment) in a bit more compact way, how about we move this to bin/sdk/gutenberg.js and change it to:

const output = outputDir || path.join( inputDir, 'build' );
process.env.CALYPSO_SDK_OUTPUT_DIR = output;

];

// The output directory is set when SDK runs, so we only set the POT generator override in that case.
if ( outputDir ) {
Copy link
Member

@simison simison Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another (maybe easier to read since code stays in one place?) way to structure this would be by using lodash.compact:

plugins: compact( [
	outputDir && [
		'@wordpress/babel-plugin-makepot',
		{
        ...
        }
	],
	[
		'@wordpress/import-jsx-pragma',
		{
			scopeVariable: 'createElement',
			source: '@wordpress/element',
			isDefault: false,
		},
	],
	[
		'@babel/transform-react-jsx',
		{
			pragma: 'createElement',
		},
	],
] )

You can see similar pattern here:

plugins: _.compact( [
! codeSplit && new webpack.optimize.LimitChunkCountPlugin( { maxChunks: 1 } ),

...but that's just up to code-stylistic choices. :-)

@akirk
Copy link
Member

akirk commented Jan 24, 2019

It would be nice if we were able to create editor.pot and editor-beta.pot respectively but I am having trouble modifying the Babel config (and thus the ouput filename for the makepot plugin) based on the "entry" in Webpack.

You can find my efforts here: https://github.com/Automattic/wp-calypso/compare/try/sdk-gutenberg-pot-generation-per-webpack-entry

It's close but I think there is a fundamental problem with both specifying a configFile as an option for babel-loader and an overrides (or plugins since we're only operating on the path subset anyway) at the same time.

Maybe some webpack+babel wizards can help us here?

@simison
Copy link
Member

simison commented Jan 24, 2019

It would be nice if we were able to create editor.pot and editor-beta.pot respectively

Why would it be nice? :-)

Maybe some webpack+babel wizards can help us here?

Sure, I can have a look tomorrow (utc+3)

@jeherve
Copy link
Member

jeherve commented Jan 25, 2019

It would be nice if we were able to create editor.pot and editor-beta.pot respectively

I think we should aim to ship all translations to WordPress.or as soon as possible, even if the feature isn't live for everyone yet; that gives translators some time to translate everything so that when the feature actually goes live, it is already translated. As such, I am okay with having only one .pot file that contains all the strings.

@simison
Copy link
Member

simison commented Jan 25, 2019

Let's drop the requirement for separate pot files for now and concentrate shipping this with one file? We can always revisit in follow up PRs if it seems like we need those files separately.

@akirk
Copy link
Member

akirk commented Jan 25, 2019

Yes, I'm fine with doing just one POT file, my angle was just not to have the SDK having to make the decision which translations to include in what file.

@ockham
Copy link
Contributor

ockham commented Jan 29, 2019

Plot twist:

Related: WordPress/gutenberg#12559

Gutenberg will soon not use babel-plugin-makepot internally.

jeherve added a commit to Automattic/jetpack that referenced this pull request Feb 1, 2019
…1225)

<!--- Provide a general summary of your changes in the Title above -->

#### Changes proposed in this Pull Request:
* Adapts JSON language files to be usable in WordPress client-side i18n.
* Rebuilds JSON language files.
* Adds a mechanism to integrate POT files that result from [block building](Automattic/wp-calypso#30303) into `jetpack-strings.php`. The whole PHP file generation pipeline now consists of one i18n-calypso run, one msgcat run, and one msgexec run that executes a bash script for every translation.
* Paves the way for legacy i18n support and makes initial steps to move towards wp.i18n.

#### Testing instructions:
Currently blocks don't have any strings translated yet in the JSON files because the strings themselves are not present in the code. After merging this code we should be able to re-run language builds and get translated strings in blocks.
* Make sure the existing i18n works properly, including existing wp-admin Settings translations.

#### Proposed changelog entry for your changes:
* Adapted existing Jetpack translations to be used in WordPress i18n mechanisms.

Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
jeherve added a commit to Automattic/jetpack that referenced this pull request Feb 1, 2019
…1225)

<!--- Provide a general summary of your changes in the Title above -->

* Adapts JSON language files to be usable in WordPress client-side i18n.
* Rebuilds JSON language files.
* Adds a mechanism to integrate POT files that result from [block building](Automattic/wp-calypso#30303) into `jetpack-strings.php`. The whole PHP file generation pipeline now consists of one i18n-calypso run, one msgcat run, and one msgexec run that executes a bash script for every translation.
* Paves the way for legacy i18n support and makes initial steps to move towards wp.i18n.

Currently blocks don't have any strings translated yet in the JSON files because the strings themselves are not present in the code. After merging this code we should be able to re-run language builds and get translated strings in blocks.
* Make sure the existing i18n works properly, including existing wp-admin Settings translations.

* Adapted existing Jetpack translations to be used in WordPress i18n mechanisms.

Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
@stale
Copy link

stale bot commented Oct 26, 2019

This issue has been marked as stale and will be closed in seven days. This happened because:

  • It has been inactive in the past 9 months.
  • It isn't a project or a milestone, and hasn’t been labeled `[Pri] Blocker`, `[Pri] High`, `[Status] Keep Open`, or `OSS Citizen`.

You can keep the issue open by adding a comment. If you do, please provide additional context and explain why you’d like it to remain open. You can also close the issue yourself — if you do, please add a brief explanation.

@stale stale bot added the [Status] Stale label Oct 26, 2019
@zinigor
Copy link
Member Author

zinigor commented Oct 30, 2019

Let's close this, since Gutenberg blocks get i18n from WordPress.org repository.

@zinigor zinigor closed this Oct 30, 2019
@zinigor zinigor deleted the add/sdk-gutenberg-pot-generation branch October 30, 2019 20:46
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i18n [Status] Stale [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDK / Gutenblocks: include .pot file with built blocks
6 participants