Skip to content
Draft
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
5 changes: 5 additions & 0 deletions tools/build-scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ async function build() {
env: { ...process.env, NODE_ENV: 'production' },
} );

console.log( '\n📦 Generating widget metadata strings...' );
await exec( 'node', [
path.join( __dirname, 'packages/generate-widget-strings.mjs' ),
] );

console.log( '\n📦 Building blocks manifests...' );
const blocksDirs = [
{
Expand Down
1 change: 1 addition & 0 deletions tools/build-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"clean:packages": "node ./clean.mjs --packages",
"dev": "node ./dev.mjs",
"build-vendors": "node ./packages/build-vendors.mjs",
"generate-widget-strings": "node ./packages/generate-widget-strings.mjs",
"generate-worker-placeholders": "node ./packages/generate-worker-placeholders.mjs",
"resolve-trace-source-maps": "node ./packages/resolve-trace-source-maps.cjs",
"check-type-declarations": "node ./packages/check-build-type-declaration-files.cjs && node ./packages/check-esm-package-types.mjs",
Expand Down
156 changes: 156 additions & 0 deletions tools/build-scripts/packages/generate-widget-strings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env node

import { mkdir, readFile, readdir, rm, writeFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname( fileURLToPath( import.meta.url ) );
const ROOT_DIR = path.resolve( __dirname, '../../..' );
const WIDGETS_DIR = path.join( ROOT_DIR, 'widgets' );
const SCHEMA_PATH = path.join(
ROOT_DIR,
'lib',
'experimental',
'dashboard-widgets',
'widget-i18n.json'
);
const OUTPUT_PATH = path.join(
ROOT_DIR,
'build',
'widgets',
'widget-strings.php'
);

const FILE_HEADER = `<?php
/**
* Widget metadata strings - Auto-generated by the plugin build.
*
* Not loaded at runtime: this file exists solely so gettext extraction
* tooling sees the translatable strings declared in widget.json files,
* until \`wp i18n make-pot\` learns to extract them natively. The gettext
* contexts come from the same widget-i18n.json schema the runtime
* translation uses. Do not edit this file manually.
*
* @package gutenberg
*/

`;

/**
* Collect the translatable ( value, context ) pairs of a widget by walking
* its metadata in lockstep with the i18n schema, mirroring the runtime
* `translate_settings_using_i18n_schema()` traversal.
*
* @param {*} schema Schema node: a context string, an array template, or a
* keyed map.
* @param {*} value Metadata node aligned with the schema node.
* @return {Array<{ value: string, context: string }>} Translatable entries.
*/
function collectEntries( schema, value ) {
if ( typeof schema === 'string' ) {
return typeof value === 'string' && value !== ''
? [ { value, context: schema } ]
: [];
}

if ( Array.isArray( schema ) ) {
return Array.isArray( value )
? value.flatMap( ( item ) => collectEntries( schema[ 0 ], item ) )
: [];
}

if (
schema &&
typeof schema === 'object' &&
value &&
typeof value === 'object'
) {
return Object.keys( schema ).flatMap( ( key ) =>
collectEntries( schema[ key ], value[ key ] )
);
}

return [];
}

/**
* Format a value as a single-quoted PHP string literal.
*
* @param {string} value String to escape.
* @return {string} PHP string literal.
*/
function toPhpStringLiteral( value ) {
return `'${ value.replace( /\\/g, '\\\\' ).replace( /'/g, "\\'" ) }'`;
}

/**
* Emit `build/widgets/widget-strings.php`: one `_x()` call per translatable
* `widget.json` value, for gettext extraction tooling. Widgets without a
* `textdomain` are skipped, matching the runtime translation no-op.
*/
async function generateWidgetStrings() {
const schema = JSON.parse( await readFile( SCHEMA_PATH, 'utf8' ) );

let widgetDirs = [];
try {
widgetDirs = ( await readdir( WIDGETS_DIR, { withFileTypes: true } ) )
.filter( ( entry ) => entry.isDirectory() )
.map( ( entry ) => entry.name )
.sort();
} catch {
// No widgets directory, nothing to extract.
}

const seen = new Set();
const calls = [];

for ( const dirName of widgetDirs ) {
let metadata;
try {
metadata = JSON.parse(
await readFile(
path.join( WIDGETS_DIR, dirName, 'widget.json' ),
'utf8'
)
);
} catch {
continue;
}

if ( ! metadata?.textdomain ) {
continue;
}

for ( const { value, context } of collectEntries( schema, metadata ) ) {
const key = JSON.stringify( [
metadata.textdomain,
context,
value,
] );
if ( seen.has( key ) ) {
continue;
}
seen.add( key );

calls.push(
`_x( ${ toPhpStringLiteral( value ) }, ${ toPhpStringLiteral(
context
) }, ${ toPhpStringLiteral( metadata.textdomain ) } );`
);
}
}

if ( calls.length === 0 ) {
// Drop a leftover file so stale strings never reach the POT.
await rm( OUTPUT_PATH, { force: true } );
return;
}

await mkdir( path.dirname( OUTPUT_PATH ), { recursive: true } );
await writeFile( OUTPUT_PATH, FILE_HEADER + calls.join( '\n' ) + '\n' );
console.log(
` ✔ Generated build/widgets/widget-strings.php (${ calls.length } strings)`
);
}

await generateWidgetStrings();
2 changes: 1 addition & 1 deletion widgets/activity/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
],
"category": "dashboard",
"presentation": "content-bleed",
"textdomain": "default"
"textdomain": "gutenberg"
}
2 changes: 1 addition & 1 deletion widgets/events/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
"relevance": "high"
}
],
"textdomain": "default"
"textdomain": "gutenberg"
}
2 changes: 1 addition & 1 deletion widgets/hello-dolly/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"openInNewTab": true
}
],
"textdomain": "default"
"textdomain": "gutenberg"
}
2 changes: 1 addition & 1 deletion widgets/hello-world/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
],
"category": "demo",
"presentation": "full-bleed",
"textdomain": "default"
"textdomain": "gutenberg"
}
2 changes: 1 addition & 1 deletion widgets/news/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Displays the latest news from WordPress.org and Planet WordPress.",
"category": "dashboard",
"presentation": "content-bleed",
"textdomain": "default",
"textdomain": "gutenberg",
"actions": [
{
"id": "news-visit",
Expand Down
2 changes: 1 addition & 1 deletion widgets/quick-draft/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"href": "edit.php?post_status=draft"
}
],
"textdomain": "default"
"textdomain": "gutenberg"
}
2 changes: 1 addition & 1 deletion widgets/site-health/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"icon": "core/shield",
"category": "site",
"presentation": "framed",
"textdomain": "default",
"textdomain": "gutenberg",
"actions": [
{
"id": "site-health-details",
Expand Down
2 changes: 1 addition & 1 deletion widgets/site-preview/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"icon": "dashboard-widgets/site-logo",
"category": "dashboard",
"presentation": "full-bleed",
"textdomain": "default"
"textdomain": "gutenberg"
}
2 changes: 1 addition & 1 deletion widgets/welcome/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"icon": "dashboard-widgets/wordpress",
"category": "dashboard",
"presentation": "full-bleed",
"textdomain": "default"
"textdomain": "gutenberg"
}
Loading