【发布时间】:2020-12-06 00:18:36
【问题描述】:
我刚刚建造了一个古腾堡街区,我想将它翻译成不同的语言。我正在用葡萄牙语使用我的 WordPress,并且我已经进行了葡萄牙语翻译。我按照文档https://developer.wordpress.org/block-editor/developers/internationalization/ 中提供的所有步骤进行操作,但仍然无效。
我的插件叫做spoiler-alert。
我在/spoiler-alert/languages 文件夹中创建了一个名为spoileralert.pot 的.pot 文件。然后,我生成了 md5 文件,甚至使用脚本处理程序创建了一个包含所有字符串的新文件。我的语言文件夹结构如下所示:
languages
- spoileralert.pot
- spoileralert-pt_BR.po
- spoileralert-pt_BR-<HASH-CODE-1>.json
- spoileralert-pt_BR-<HASH-CODE-2>.json
- spoileralert-pt_BR-<HASH-CODE-3>.json
- spoileralert-pt_BR-spoileralert.json
这是我的 PHP 文件 spoiler-alert.php:
function spoiler_alert_spoiler_alert_block_init() {
$dir = dirname( __FILE__ );
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "spoiler-alert/spoiler-alert" block first.'
);
}
$index_js = 'build/index.js';
$script_asset = require( $script_asset_path );
wp_register_script(
'spoiler-alert-spoiler-alert-block-editor',
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version']
);
$editor_css = 'build/index.css';
wp_register_style(
'spoiler-alert-spoiler-alert-block-editor',
plugins_url( $editor_css, __FILE__ ),
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'build/style-index.css';
wp_register_style(
'spoiler-alert-spoiler-alert-block',
plugins_url( $style_css, __FILE__ ),
array(),
filemtime( "$dir/$style_css" )
);
register_block_type( 'spoiler-alert/spoiler-alert', array(
'editor_script' => 'spoiler-alert-spoiler-alert-block-editor',
'editor_style' => 'spoiler-alert-spoiler-alert-block-editor',
'style' => 'spoiler-alert-spoiler-alert-block',
) );
wp_set_script_translations( 'spoileralert', 'spoiler-alert', plugin_dir_path( __FILE__ ) . '/languages' );
}
add_action( 'init', 'spoiler_alert_spoiler_alert_block_init' );
在我的.js 文件中,我使用以下方式导入了翻译包:import { __ } from '@wordpress/i18n';
我使用的翻译如下:title: __( 'Spoiler Alert', 'spoiler-alert' ),
如何让翻译正确显示?
【问题讨论】:
标签: wordpress wordpress-gutenberg