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

Extract translatable strings from block.json files #163

Closed
1 task done
swissspidy opened this issue Apr 25, 2019 · 7 comments · Fixed by #210
Closed
1 task done

Extract translatable strings from block.json files #163

swissspidy opened this issue Apr 25, 2019 · 7 comments · Fixed by #210
Assignees

Comments

@swissspidy
Copy link
Member

Feature Request

Describe your use case and the problem you are facing

For supporting the next iteration of Gutenberg block development, we need to extract strings from block.json files.

Describe the solution you'd like

wp i18n make-pot should parse block.json files and extract all strings it finds with the additional context.

For more information, check out:

@swissspidy
Copy link
Member Author

Here's the final RFC: https://github.com/WordPress/gutenberg/blob/76aa7154913d54985ffbb57b695c9d7e86492124/docs/rfc/block-registration.md

@swissspidy swissspidy self-assigned this Jul 15, 2019
@gziolo
Copy link
Contributor

gziolo commented Jan 3, 2020

I have just closed WordPress/gutenberg#16088 where we all agreed that i18n-command should extract translatable strings from block.json file.

Re-sharing comment from @swissspidy:

I can potentially help with implementation as I built large parts of that tool and am familiar with it. Then, I suppose @ocean90 could help update it on dotorg.

The challenge would be only how WP-CLI locates all block.json files

Just like WP-CLI scans all JS and PHP files for translation functions, it would then also scan all block.json files that are deemed valid. Don't see a huge difficulty there.

I explored building Babel plugin which takes care of converting block.json into JavaScript code but we decided that we shouldn't go in that direction. Here are some snippets of code which should help with the implementation:

const translatableProperties = [
 	'title',
 	'description',
 	'keywords',
 	'styles',
 ];
function wrapTranslatableProperty( node, name, textDomain, types ) {
 	if ( node.type === 'StringLiteral' && node.value ) {
 		node = types.callExpression(
 			types.identifier( '__x' ),
 			[
 				node,
 				types.stringLiteral( `block ${ name }` ),
 				textDomain !== 'default' && types.stringLiteral( textDomain ),
 			].filter( Boolean )
 		);
 	} else if ( node.type === 'ArrayExpression' ) {
 		node.elements = node.elements.map(
 			( elementNode ) => wrapTranslatableProperty(
 				elementNode,
 				name,
 				textDomain,
 				types
 			)
 		);
 	} else if ( node.type === 'ObjectExpression' ) {
 		node.properties.forEach( ( propertyNode ) => {
 			if ( propertyNode.key.name === 'label' ) {
 				propertyNode.value = wrapTranslatableProperty(
 					propertyNode.value,
 					name,
 					textDomain,
 					types
 				);
 			}
 		} );
 	}
 	return node;
 }

The gist of it is:

  • string (title and description) and string[] (keywords) are translatable when whitelisted
  • for Object[] only label properties are translatable for each object listed (this is only for styles at the moment)

@swissspidy
Copy link
Member Author

@gziolo Thanks for the update! How often do you think the list of translatable properties would change? It might be safer to have title, description, keywords, and styles.label* whitelisted in WP-CLI.

* What about styleVariations?

@gziolo
Copy link
Contributor

gziolo commented Jan 8, 2020

@gziolo Thanks for the update! How often do you think the list of translatable properties would change? It might be safer to have title, description, keywords, and styles.label* whitelisted in WP-CLI.

  • What about styleVariations?

styleVariations is an alias for styles that is the legacy name and a bit ambiguous.

The whitelist approach is perfectly fine. I don't expect to change it frequently. Only if we decide to add new fields but I don't see any reasons for that as of today.

@swissspidy
Copy link
Member Author

Thanks for the quick response!

Sounds like a relatively easy thing to implement then :-)

@gziolo
Copy link
Contributor

gziolo commented Apr 8, 2020

@swissspidy – it seems to be one of the last blockers for making block.json a thing. Can you share some pointers about how this could be implemented? Maybe some prior art (PR or commit)?

@swissspidy
Copy link
Member Author

@gziolo Heya, I created #210 as a starting point for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants