Skip to content

Extract translatable strings from block.json files #163

Closed

Description

@swissspidy

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:

Activity

  1. swissspidy commented on Jul 9, 2019

    @swissspidy
    MemberAuthor
  2. self-assigned this
    on Jul 15, 2019
  3. gziolo commented on Jan 3, 2020

    @gziolo
    Contributor

    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)
  4. swissspidy commented on Jan 8, 2020

    @swissspidy
    MemberAuthor

    @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?

  5. gziolo commented on Jan 8, 2020

    @gziolo
    Contributor

    @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.

  6. swissspidy commented on Jan 8, 2020

    @swissspidy
    MemberAuthor

    Thanks for the quick response!

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

  7. gziolo commented on Apr 8, 2020

    @gziolo
    Contributor

    @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)?

  8. swissspidy commented on Apr 17, 2020

    @swissspidy
    MemberAuthor

    @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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions