• Resolved ImageGuido

    (@guido07111975)


    Hi,

    Currently rebuilding my block to support Block API version 3 and have a question about making the block translatable.

    In edit.js I import support:

    import { __ } from '@wordpress/i18n';

    The dependency is added to my index.asset file:

    array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-server-side-render'), 'version' => '123456789');

    I add a translatable string like:

    label={ __( 'Attributes', 'text-domain' ) }

    Should I do more, or does WP generate the proper language pack files now?
    I normally include a POT file generated with Poedit (free version), but it does not index the edit.js file. So I’m in doubt.

    Guido

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Imagethreadi

    (@threadi)

    Yes, that looks correct so far.

    If your plugin is in the WordPress repository, it will take care of the translation and generate all the necessary language files. In this case, you don’t need to provide any language files, just upload the generated block files.

    If, on the other hand, you want to generate the language files yourself, you must proceed as follows:

    Prerequisite: you need the WP CLI executable on the command line.

    1. Create the .pot files by executing the following command in the main directory of your plugin on the command line: wp i18n make-pot . languages/your-plugin-slug.pot
    2. Then make the translations as usual using PoEdit and upload the finished .po and .mo files to your plugin’s languages directory.
    3. Run this command: wp i18n make-json languages – this will generate the json files containing the translations for the blocks.
    4. Finally, run this command: wp i18n make-php languages – this will generate the optimized PHP files for your PHP files.

    Then you need to integrate the language files:

    add_action( 'init', function() {
    load_plugin_textdomain( 'your-plugin-slug', false, dirname( plugin_basename( __FILE__) ) . '/languages' );
    });

    See also: https://developer.wordpress.org/cli/commands/i18n/make-json/

    Thread Starter ImageGuido

    (@guido07111975)

    Great, the plugin is listed on org, so I’m very pleased to hear I don’t have to follow all those extra steps.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.