2

I am trying to follow this article https://developer.wordpress.org/block-editor/how-to-guides/internationalization/ in order to translate my block.

I created my block using https://developer.wordpress.org/block-editor/getting-started/create-block/ script.

Then in my gutenpride.php, I added this code.

function myguten_set_script_translations()
{
    wp_register_script('myguten-script', plugins_url('/temp.js', __FILE__), ['wp-blocks', 'wp-i18n', 'wp-element'], filemtime(__DIR__ . '/temp.js'));

    wp_set_script_translations('myguten-script', 'myguten', plugin_dir_path(__FILE__) . 'languages');

    wp_localize_script('myguten-script', 'myguten', [
        'plugin_url' => plugins_url('/', __FILE__),
    ]);

    wp_enqueue_script('myguten-script');
}
add_action('init', 'myguten_set_script_translations');

As you can see, I am registering a temp.js file and giving it all the required dependencies.

The temp.js file works and is running.

As instructed by the translation article I added two files in my language folder

1. myguten-co.po
2. myguten.pot

Then I created the JSON file for myguten-co.po and changed the site language to Esperanto.

But when I see the block or add the new block yet I do not get the translated label.

Is there anything I am missing?

1 Answer 1

1

I see that you do not use register_block_type in PHP, which is now recommended way to register block. I am assuming you register block in the front-end ( I am not familiar with create-block ), and instead that I inspect create-block, would you share your front end code where you also have translation functions? It could be for a various reasons that this does not function, one of more frequent being using incorrect text domain. Also, for better workflow I recommend using wp-cli and wp i18n make-json.

Edit

Use wp i18n make-json to create Jed-formatted JSON files. This is the reason why your code did not work. You only created .pot and .po files, but you have to supply .json files, since javascript loads them. See examples at bottom of this page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.