block 多语言翻译辅助函数

  1. 让每个自定义 Block 具备 i18n 能力

新增通用助手 framework/my-custom-block/my-custom-block.php:

  • wpdemo_block_translate_defaults($attributes, DIR) — 递归遍历 attributes,凡是与 block.json 英文默认值完全相同的字符串,替换为
    当前语言的 __() 翻译;编辑器里手动输入的内容绝不会被改动;路径/URL 自动跳过
  • 8 个 block 的 render.php(src + build 各一份,共16个文件)全部接入该助手,block.json 本来就声明了 textdomain: wpdemo
/**
 * Translate block attribute values that still equal their English default.
 *
 * These blocks declare their visible default strings (titles, captions, repeater
 * items…) in block.json. WordPress fills those defaults server-side before the
 * render callback runs, so content pages that never edited the attributes would
 * always render English regardless of the active language. This helper walks the
 * attributes and swaps every value that is byte-identical to its declared default
 * for its translation in the current locale. Anything an editor typed themselves
 * is left untouched.
 *
 * @param array  $attributes Block attributes (defaults already merged in by WP).
 * @param string $block_dir  Directory holding the block.json (usually __DIR__).
 * @return array Attributes with defaults translated where applicable.
 */
function wpdemo_block_translate_defaults( $attributes, $block_dir ) {
	if ( ! is_array( $attributes ) || ! function_exists( '__' ) ) {
		return $attributes;
	}

	static $defaults_cache = array();

	$block_json = $block_dir . '/block.json';

	if ( ! isset( $defaults_cache[ $block_json ] ) ) {
		$defaults = array();

		if ( is_readable( $block_json ) ) {
			$json = json_decode( (string) file_get_contents( $block_json ), true );

			if ( is_array( $json ) && ! empty( $json['attributes'] ) && is_array( $json['attributes'] ) ) {
				$defaults = $json['attributes'];
			}
		}

		$defaults_cache[ $block_json ] = $defaults;
	}

	foreach ( $defaults_cache[ $block_json ] as $key => $def ) {
		if ( ! isset( $attributes[ $key ] ) || ! is_array( $def ) || ! array_key_exists( 'default', $def ) ) {
			continue;
		}

		$attributes[ $key ] = wpdemo_block_translate_default_value( $attributes[ $key ], $def['default'] );
	}

	return $attributes;
}

/**
 * Recursively translate strings that still equal their declared default.
 *
 * Handles scalar attributes as well as nested repeater structures (arrays of
 * arrays). Strings that look like paths/URLs (contain a slash) are never touched.
 *
 * @param mixed $value   Current attribute value.
 * @param mixed $default Declared default from block.json.
 * @return mixed
 */
function wpdemo_block_translate_default_value( $value, $default ) {
	if ( is_array( $value ) && is_array( $default ) ) {
		foreach ( $value as $k => $v ) {
			$value[ $k ] = wpdemo_block_translate_default_value( $v, array_key_exists( $k, $default ) ? $default[ $k ] : null );
		}

		return $value;
	}

	if (
		is_string( $value ) && is_string( $default )
		&& $value === $default
		&& '' !== trim( $value )
		&& false === strpos( $default, '/' )
		&& preg_match( '/\p{L}/u', $default )
	) {
		return __( $default, 'wpdemo' );
	}

	return $value;
}
×
block 多语言翻译辅助函数

block 多语言翻译辅助函数

让每个自定义 Block 具备 i18n...