星期日 , 8 11 月 2026

AI agent 如何翻译网站(标签+动态内容)

先让AI把网站进行i18n化,就是添加翻译函数,可以加载到poedit里

然后主题就具备翻译的条件,让AI根据模板里重新调整生成的函数生成.pot文件。

然后让AI进行翻译,将英语的.po文件翻译成中文的.po 文件或者其他语言的, 编写.po文件并写入内容,生成.mo 文件

翻译的提示词:

I need you to translate a WordPress .po translation file from English to [TARGET LANGUAGE].

IMPORTANT RULES:
1. Only translate the text inside msgstr "" (leave it empty currently)
2. Do NOT translate the text inside msgid "" (this is the source text)
3. Keep ALL placeholders like %s, %d, %1$s exactly as they are
4. Keep ALL HTML tags like <strong>, <a>, <br> exactly as they are
5. Keep ALL special characters like \n (newline), \" (quotes) exactly as they are
6. Keep the exact same structure and formatting
7. Do not translate code references like #: functions.php:123
8. Maintain the same number of quotes and line breaks

Here is the .po file content to translate:

[PASTE YOUR .PO FILE CONTENT HERE]

然后如果要翻译block内容的话,从英语开始翻译的话,要添加下面一些helper函数,在functions.php里面或者blocks里面的主php文件

/**
 * 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;
}
×
AI agent 如何翻译网站(标签+动态内容)

AI agent 如何翻译网站(标签+动态内容)

先让AI把网站进行i18n化,就是添加翻...

查看更多

WP Multilingual Website SEO Authority and Issue Fixes

When the websit …

发表回复