When using @wordpress/create-block
to scaffhold a plugin for a block, the generated bundle is automatically registered via the block.json
metadata :
{
...
"name": "my-block",
"textdomain": "my-block",
"editorScript": "file:./index.js",
...
}
No need to call wp_register_script
myself. This is great since it automatically handles dependencies via the index.asset.php
file generated in the build folder.
Following the procedure mentionned in the doc, I then create a JET translation file. Here is the procedure :
- Creating POT file with
wp i18n make-pot . languages/my-block.pot
- Creating PO file with
cp languages/test.pot languages/my-block-FR_BE.po
- Filling msgstr strings in
my-block-FR_BE.po
- Adding line
"Language: fr_BE\n"
tomy-block-FR_BE.po
- Creating JSON file with
wp i18n make-json languages/my-block-FR_BE.po --no-purge
The JSON generated is appended with a md5 hash: my-block-fr_BE-cae574befd871d4f740fd8b719bac1db.json.
Now I have to call wp_set_script_translations
in my init method :
function my_block_init() {
register_block_type( __DIR__ . '/build' );
wp_set_script_translations( 'my-block-script', 'my-block', plugin_dir_path( __FILE__ ) . 'languages/' );
}
This does not work.
In order to make it work, I have to register the script and enqueue it, loosing the ability to have dependencies automatically injected :
function my_block_init() {
wp_register_script(
'my-block-script',
plugins_url('/build/index.js', __FILE__),
array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-react-refresh-runtime')
);
wp_enqueue_script('my-block-script');
register_block_type( __DIR__ . '/build' );
wp_set_script_translations('my-block-script', 'my-block', plugin_dir_path( __FILE__ ) . 'languages/');
}
I also need to rename my JSON file to include the script handle instead of the automatically generated md5 hash. The "Load Translation File" section in the doc gives me the impression that this shouldn't be necessary when keeping the generated name, though I'm not sure of what I'm supposed to do here :
WordPress will check for a file in that path with the format ${domain}-${locale}-${handle}.json as the source of translations. Alternatively, instead of the registered handle you can use the md5 hash of the relative path of the file, ${domain}-${locale} in the form of ${domain}-${locale}-${md5}.json.
Is there a way to register JET translations for a script that is automatically registered via the block metadata ? And how can I use the generated name for the JSON file when registering my translation ?
Edit
Here's the folder structure :
app/plugins/my-block/
├── build
│ ├── block.json
│ └── index.asset.php
│ └── index.css
│ └── index.js
│ └── style-index.css
├── languages
│ ├── my-block.pot
│ └── my-block-fr_BE.po
│ └── my-block-fr_BE-my-block-script.json
├── src
│ ├── block.json
│ └── index.js
│ └── ...
└── my-block.php
And here's the content of the my-block.php
file (plugin root) :
<?php
/**
* Plugin Name: My Block
* Requires at least: 5.9
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: my-block
*/
function my_block_init() {
register_block_type( __DIR__ . '/build' );
wp_set_script_translations( 'my-block-script', 'my-block', plugin_dir_path( __FILE__ ) . 'languages/' );
}
add_action( 'init', 'my_block_init' );
block.json
the same way the script and styles were, is that not the case? Are you sure thatplugin_dir_path( __FILE__ ) . 'languages/'
is the actual folder? If you're not doing this in the main plugin folder then__FILE__
will have the incorrect value andplugin_dir_path
will return an incorrect result,plugin_dir_path
is expecting the main file of the plugin, not the current file the code is in.wp_set_script_translations
is in the main plugin php file at the root of the plugin.add_action
calls you used, and make sure they're in the order they are in your codebase too. It's possible the order things are called in is causing issues, eitherway seeing the code will help eliminate that question. ( note that the packages you're using to generate this have changed over time so simply referring to what the package does as the answer isn't enough )