Localising a Block

There are 2 main issues in your code:

  1. As stated in my answer, the script handle format is <block name with slashes replaced with hypens>-editor-script, so because your block name is my-block/local, then the script handle is my-block-local-editor-script and not my-block-local-edit-script.

  2. The correct action name is wp_enqueue_scripts (note the “s”) and not wp_enqueue_script. However, wp_enqueue_scripts would not work because that’s for the front-end (non-admin side). For admin use like the post editing screen at wp-admin/post.php, you can use either admin_enqueue_scripts or a hook specific to the block/Gutenberg editor like enqueue_block_editor_assets.

  3. Your JSON translation file should also be using dfbff627e6c248bcb3b61d7d06da9ca9 which is the value of md5( 'build/index.js' ) (no ./). So the file name should instead be my-block-es_ES-dfbff627e6c248bcb3b61d7d06da9ca9.json 🙂

    That value/hash is also what you’d get when you run this via WP-CLI: wp i18n make-json my-block-<locale>.po ./ --no-purge after doing a cd languages in your plugin directory.

How to fix the issues 1 and 2:

  1. Change the wp_set_script_translations() line (it’s line 20 in your code) to:

        'my-block-local-editor-script',           // script handle
        'my-block',                               // text domain
        plugin_dir_path( __FILE__ ) . 'languages' // path to your translation files

    Or you can use generate_block_asset_handle() to generate the script handle:

    $script_handle = generate_block_asset_handle( 'my-block/local', 'editorScript' );
    wp_set_script_translations( $script_handle, 'my-block', plugin_dir_path( __FILE__ ) . 'languages' );
  2. Change the add_action( 'wp_enqueue_script', 'script_translations' ); (line 22 in your code) to:

    add_action( 'enqueue_block_editor_assets', 'script_translations' );
    // These also work:
    //add_action( 'admin_enqueue_scripts', 'script_translations' );
    //add_action( 'init', 'script_translations' );

    Note about using init: Calling register_block_type() will automatically and immediately register the block editor script — see register_block_script_handle(), so init can be used to set the script translations as long as it’s called after calling register_block_type().