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

Fix blocks missing translations in simple sites #33190

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix version mismatch
2 changes: 1 addition & 1 deletion projects/packages/jetpack-mu-wpcom/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"autotagger": true,
"branch-alias": {
"dev-trunk": "4.10.x-dev"
"dev-trunk": "4.11.x-dev"
},
"textdomain": "jetpack-mu-wpcom",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/jetpack-mu-wpcom/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-mu-wpcom",
"version": "4.10.0",
"version": "4.11.0-alpha",
"description": "Enhances your site with features powered by WordPress.com",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/jetpack-mu-wpcom/#readme",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Jetpack_Mu_Wpcom {

const PACKAGE_VERSION = '4.10.0';
const PACKAGE_VERSION = '4.11.0-alpha';
const PKG_DIR = __DIR__ . '/../';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix version mismatch
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '1.57.0';
const PACKAGE_VERSION = '1.57.1-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-blocks-localization
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Fix blocks title and description not localized
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/extensions/blocks/ai-chat/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import './editor.scss';
import './components/feedback/style.scss';

registerJetpackBlockFromMetadata( metadata, {
// The API version needs to be explicitly specified in this instance for styles to be loaded.
apiVersion: metadata.apiVersion,
Copy link
Contributor Author

@monsieur-z monsieur-z Sep 19, 2023

Choose a reason for hiding this comment

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

Metadata are added to settings in registerJetpackBlockFromMetadata anyway.

edit,
save,
} );
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"apiVersion": 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the changes brought to registerJetpackBlockFromMetadata , the implementation of the edit function (I believe) requires the block to use the first version of the API.

"name": "jetpack/business-hours",
"title": "Business Hours",
"description": "Display opening hours for your business.",
"keywords": ["opening hours", "closing time", "schedule", "working day"],
"keywords": [ "opening hours", "closing time", "schedule", "working day" ],
"version": "12.5.0",
"textdomain": "jetpack",
"category": "grow",
Expand All @@ -22,7 +22,7 @@
"fontSize": true,
"lineHeight": true
},
"align": ["wide", "full"]
"align": [ "wide", "full" ]
},
"attributes": {
"days": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {
} from '@automattic/jetpack-shared-extension-utils';
import { registerBlockType } from '@wordpress/blocks';
import { addFilter } from '@wordpress/hooks';
import { _x } from '@wordpress/i18n';

const JETPACK_PREFIX = 'jetpack/';
const LOCALIZED_BLOCK_PROPERTIES = [ 'title', 'description', 'keywords' ];
const DEFAULT_TEXTDOMAIN = 'jetpack';

/**
* Registers a gutenberg block if the availability requirements are met.
Expand Down Expand Up @@ -59,6 +62,24 @@ export default function registerJetpackBlock( name, settings, childBlocks = [],
return result;
}

// Localize properties passed as parameter when applicable.
// Note: the documentation states that `registerBlockType` wraps localized properties in `_x`
// when it is passed the metadata object as the first param. I haven't found this to be true
// with the latest version of `@wordpress/blocks` (12.18.0) at the time of writing this.
// https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#javascript
const localizeBlockProperties = ( metadata, props ) => {
const result = { ...props };

for ( const prop in props ) {
if ( LOCALIZED_BLOCK_PROPERTIES.includes( prop ) ) {
// eslint-disable-next-line
result[ prop ] = _x( props[ prop ], null, metadata.textdomain || DEFAULT_TEXTDOMAIN );
Copy link
Contributor

@anomiex anomiex Sep 21, 2023

Choose a reason for hiding this comment

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

(most of this is already known to the people involved via Slack discussions, but to close the loop I'll repeat it here)

I think this only accidentally works if your translation files still have the translations from

title: __( 'Business Hours', 'jetpack' ),
description: __( 'Display opening hours for your business.', 'jetpack' ),
that were removed in #32698.

WordPress intends for the translation to happen on the PHP side during block registration (here), then the already-translated texts are sent to JS via a generated call to wp.blocks.unstable__bootstrapServerSideBlockDefinitions (here, here, here, or here).

That PHP code path is only reached when using register_block_type_from_metadata(), or passing a path to the block.json to register_block_type(). We instead pass a WP_Block_Type object. So we'd either need to change our code to pass the block.json path, or duplicate the relevant logic (plus any other needed logic for any other core block.json registeration features) when creating our WP_Block_Type.

}
}

return result;
};

/**
* Wrapper around registerJetpackBlock to register a block by specifying its metadata.
*
Expand All @@ -69,20 +90,25 @@ export default function registerJetpackBlock( name, settings, childBlocks = [],
* @returns {object|boolean} Either false if the block is not available, or the results of `registerBlockType`
*/
export function registerJetpackBlockFromMetadata( metadata, settings, childBlocks, prefix ) {
const clientSettings = {
const mergedSettings = {
...metadata,
...settings,
};
const processedSettings = {
...localizeBlockProperties( metadata, mergedSettings ),
icon: getBlockIconProp( metadata ),
};

const { variations } = metadata;

if ( Array.isArray( variations ) && variations.length > 0 ) {
clientSettings.variations = variations.map( variation => {
processedSettings.variations = variations.map( variation => {
return {
...variation,
...localizeBlockProperties( metadata, variation ),
icon: getBlockIconProp( variation ),
};
} );
}

return registerJetpackBlock( metadata.name, clientSettings, childBlocks, prefix );
return registerJetpackBlock( metadata.name, processedSettings, childBlocks, prefix );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
]
},
"config": {
"autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ1_6_20"
"autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ1_6_21_alpha"
}
}
4 changes: 2 additions & 2 deletions projects/plugins/mu-wpcom-plugin/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Plugin Name: WordPress.com Features
* Description: Test plugin for the jetpack-mu-wpcom package
* Version: 1.6.20
* Version: 1.6.21-alpha
* Author: Automattic
* License: GPLv2 or later
* Text Domain: jetpack-mu-wpcom-plugin
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-mu-wpcom-plugin",
"version": "1.6.20",
"version": "1.6.21-alpha",
"description": "Test plugin for the jetpack-mu-wpcom package",
"homepage": "https://jetpack.com",
"bugs": {
Expand Down
Loading