Support

Account

Forum Replies Created

  • Haha some serious ACF rubber duck debugging here but I hope it might help others if admins keep the thread.

    Add a new loading point in child theme’s functions.php that points to parent theme’s acf-json folder (get_template_directory()):

    function child_theme_acf_json_load_point( $paths ) {
    	// Remove the original path (optional).
    	unset($paths[0]);
    
    	// Append the new path and return it.
    	$paths[] = get_template_directory() . '/acf-json';
    
    	return $paths;
    }
    add_filter( 'acf/settings/load_json', 'child_theme_acf_json_load_point' );
  • Ah, of course. I have to use get_template_directory() instead of get_stylesheet_directory(). Now I can see the blocks registered but the acf fields are not loaded. Do I have to add something to make the child theme use the parent themes acf-json folder?

  • Third attempt on trying to post a reply to you John (two attempts yesterday with 1-2 hours in between). After submit the posts just vanish, don’t know what’s happening.

    Anyway, thank you for replying! Here’s the url https://support.advancedcustomfields.com/forums/topic/block-json-with-translated-title-and-description-i18n-l10n-2/

    My latest reply has since then appeared but there’s also a duplicate posted because I think it re-posted on page refresh once the lock lifted.

  • Update! I’ve stumbled across this article saying that it can’t be done like any of the methods above for the time being. Instead you’d have to add the strings in an extra step and registering the blocks with register_block_type_from_metadata() instead of register_block_type().

    However the strings in your block.json remains not translated. Unfortunately we have to add these manually to register_block_type_from_metadata(). This might change in the future, but as for the time of writing this (WordPress 5.8+) we need this one additional step.

    My previous function looked like this:

    function register_acf_blocks() {
    	foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    		register_block_type( $path . '/block.json' );
    	}
    }

    I changed register_block_type to register_block_type_from_metadata() and then tried getting and using the strings from the block.json files, but without success (I guess the same problem as in my first post. You can’t pass a variable into a gettext function).

    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    	foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    		$str = file_get_contents( $path . '/block.json' );
    		$json = json_decode($str, true);
    		$title = $json['title'];
    		$desc = $json['description'];
    
    		register_block_type_from_metadata( $path . '/block.json', [
    			'title' => _x( $title, 'block title', 'mytextdomain' ),
    			'description' => _x( $desc, 'block description', 'mytextdomain' ),
    		] );
    	}
    }

    So then I guess we’re left with the manual and double declaration way, as the article above states.

    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    	$path = get_stylesheet_directory() . '/includes/blocks/';
    
    	register_block_type_from_metadata( $path . 'my-block-1/block.json', [
    		'title' => _x( 'Title text of block #1', 'block title', 'theme-textdomain' ),
    		'description' => _x( 'This is the description of block #1', 'block description', 'theme-textdomain' ),
    	] );
    
    	register_block_type_from_metadata( $path . 'my-block-2/block.json', [
    		'title' => _x( 'Title text of block #2', 'block title', 'theme-textdomain' ),
    		'description' => _x( 'This is the description of block #2', 'block description', 'theme-textdomain' ),
    	] );
    
    	register_block_type_from_metadata( $path . 'my-block-3/block.json', [
    		'title' => _x( 'Title text of block #3', 'block title', 'theme-textdomain' ),
    		'description' => _x( 'This is the description of block #3', 'block description', 'theme-textdomain' ),
    	] );
    
    }

    VoilĂ ! It works!

    A good thing about this is that I can use the theme’s textdomain (using a custom textdomain for your blocks would need a separate load_theme_textdomain() function in functions.php and creating .pot/.po files), allowing me to only have one .po file for all theme related strings and also no need for anything wp-cli i18n, npm etc. The downside is that you’d have to register each block manually and also handle double title and description texts (not sure if you can omit them from block.json).

    To make it more modular I can add register_block_type_from_metadata() in a separate php file (like setup.inc.php) in each block folder and then use the second approach above, looping through all folders and include the php file instead. That way you wouldn’t need to edit function.php for adding/removing blocks.

    functions.php:

    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    	global $path;
    
    	foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    		include $path . '/setup.inc.php';
    	}
    
    }

    In each of my block folders I add a new file setup.inc.php:

    <?php
    register_block_type_from_metadata( $path . '/block.json', [
        'title' => _x( 'Title of this block', 'block title', 'theme-textdomain' ),
        'description' => _x( 'This is the description of this block', 'block description', 'theme-textdomain' ),
    ] );

    Now, I’m no developer, more of an UI/UX guy. So this can probably be written even better. But I think this approach should be reviewed by the ACF devs and maybe added to the docs as an alternative to register_block_type() if this is the only way to handle translations of block.json strings. Since there is a vast amount of non-english installs of WP and by using ACF we actively want to build custom blocks with PHP (= .po) rather than JS. 🙂

  • I too have had this problem multiple times lately. I get: “ERROR: Duplicate reply detected; it looks as though you’ve already said that!”. How do I know when or if I can view my post/reply again? Very frustrating.

    Maybe the settings can be tweaked a bit? And might I suggest a preview mode, that would spare a lot of edits.

  • Update! I’ve stumbled across this article saying that it can’t be done like any of the methods above for the time being. Instead you’d have to add the strings in an extra step and registering the blocks with register_block_type_from_metadata() instead of register_block_type().

    However the strings in your block.json remains not translated. Unfortunately we have to add these manually to register_block_type_from_metadata(). This might change in the future, but as for the time of writing this (WordPress 5.8+) we need this one additional step.

    My previous function looked like this:

    function register_acf_blocks() {
    	foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    		register_block_type( $path . '/block.json' );
    	}
    }

    Then I tried getting and using the strings from the block.json files, but without success (I guess the same problem as in my first post. You can’t pass a variable into a gettext function).

    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    	foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    		$str = file_get_contents( $path . '/block.json' );
    		$json = json_decode($str, true);
    		$title = $json['title'];
    		$desc = $json['description'];
    
    		register_block_type_from_metadata( $path . '/block.json', [
    			'title' => _x( $title, 'block title', 'mytextdomain' ),
    			'description' => _x( $desc, 'block description', 'mytextdomain' ),
    		] );
    	}
    }

    So then I guess we’re left with the manual and double declaration way, as the article above states.

    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    	$path = get_stylesheet_directory() . '/includes/blocks/';
    
    	register_block_type_from_metadata( $path . 'my-block-1/block.json', [
    		'title' => _x( 'Title text of block #1', 'block title', 'theme-textdomain' ),
    		'description' => _x( 'This is the description of block #1', 'block description', 'theme-textdomain' ),
    	] );
    
    	register_block_type_from_metadata( $path . 'my-block-2/block.json', [
    		'title' => _x( 'Title text of block #2', 'block title', 'theme-textdomain' ),
    		'description' => _x( 'This is the description of block #2', 'block description', 'theme-textdomain' ),
    	] );
    
    	register_block_type_from_metadata( $path . 'my-block-3/block.json', [
    		'title' => _x( 'Title text of block #3', 'block title', 'theme-textdomain' ),
    		'description' => _x( 'This is the description of block #3', 'block description', 'theme-textdomain' ),
    	] );
    
    }

    VoilĂ ! It works!

    A good thing about this is that I can use the theme’s textdomain (using a custom textdomain for your blocks would need a separate load_theme_textdomain() function in functions.php and creating .pot/.po files), allowing me to only have one .po file for all theme related strings and also no need for anything wp-cli i18n, npm etc. The downside is that you’d have to register each block manually and also handle double title and description texts (not sure if you can omit them from block.json).

    To make it more modular I can add register_block_type_from_metadata() in a separate php file (like setup.inc.php) in each block folder and then use the second approach above, looping through all folders and include the php file instead. That way you wouldn’t need to edit function.php for adding/removing blocks.

    functions.php:

    add_action( 'init', 'register_acf_blocks', 5 );
    function register_acf_blocks() {
    	global $path;
    
    	foreach ( glob( get_stylesheet_directory() . '/includes/blocks/*' ) as $path ) {
    		include $path . '/setup.inc.php';
    	}
    
    }

    In each of my block folders I add a new file setup.inc.php:

    <?php
    register_block_type_from_metadata( $path . '/block.json', [
        'title' => _x( 'Title of this block', 'block title', 'theme-textdomain' ),
        'description' => _x( 'This is the description of this block', 'block description', 'theme-textdomain' ),
    ] );

    Now, I’m no developer, more of an UI/UX guy. So this can probably be written even better. But I think this approach should be reviewed by the ACF devs and maybe added to the docs as an alternative to register_block_type() if this is the only way to handle translations of block.json strings. Since there is a vast amount of non-english installs of WP and by using ACF we actively want to build custom blocks with PHP (= .po) rather than JS. 🙂

  • No not yet. I have also tried using wp cli i18n to create .pot and json files (see below) and trying to register via:

    add_action( 'wp_enqueue_scripts', 'my_blocks_script_translations' );
    function my_blocks_script_translations() {
    	if ( function_exists( 'wp_set_script_translations' ) ) {
    		wp_set_script_translations( 'my-blocks', 'my-textdomain', get_template_directory() . '/includes/blocks/languages/' );
    	}
    }

    But no success yet.

    Problem 1: I can’t generate a json file when the string reference block.json is present in the .po file #: my-blocks/testblock/block.json. Like this:

    #: my-blocks/testblock/block.json
    #: my-blocks/google-maps/block.json
    msgctxt "block keyword"
    msgid "container"
    msgstr "behĂĄllare"

    Trying the same procedure but with the strings added in a .js file works, like #: my-blocks/testblock/script.js

    Problem 2: Even when using an online tool (such as wpeform.io/tools/make-po-to-json/) to convert my .po file to json I can’t get wp_set_script_translations() to work.

    Anyway, this is what I’ve tried (without success), maybe someone can figure things out which I can’t.

    functions.php: same snippet as above

    package.json:

    {
    	"name": "my-blocks",
    	"version": "1.0.0",
    	"description": "Builds pot and json files for translation of ACF blocks",
    	"author": "Me",
    	"scripts": {
    		"start": "wp-scripts start",
    		"build": "wp-scripts build",
    		"packages-update": "wp-scripts packages-update",
    		"make-pot": "wp i18n make-pot . languages/my-blocks.pot --exclude=node_modules,src --ignore-domain --headers='{\"Last-Translator\":\"lepardman <[email protected]>\",\"Language-Team\":\"The team <[email protected]>\"}'",
    		"make-json": "wp i18n make-json languages/ --no-purge --pretty-print"
    	},
    	"dependencies": {
    		"@wordpress/i18n": "^4.27.0"
    	},
    	"devDependencies": {
    		"@wordpress/scripts": "^25.4.0"
    	}
    }

    I first run wp i18n make-pot to get a pot file (this works, all strings are added). Then I use PoEdit to create a po file with translations (or copy+paste pot and change file name to .po and translate). And lastly I run wp i18n make-json but it creates no files (output: “Success: Created 0 files”). See problem #1.

  • Found a duplicate https://support.advancedcustomfields.com/forums/topic/block-settings-in-admin-sidebar-with-acf-blocks/ (sorry ’bout that). Admin can remove (but maybe the topic titles might help others?).

Viewing 10 posts - 1 through 10 (of 10 total)