文本在WordPress 5.0版WordPress中introduced JavaScript i18n支持,这基本上意味着您可以使用__()
, _n()
, 等,以检索特定字符串/文本的翻译,如下所示:// Equivalent example in PHP: <?php var_dump( __( \'My string\', \'text-domain\' ) ); ?>
console.log( wp.i18n.__( \'My string\', \'text-domain\' ) );
// Equivalent example in PHP: <?php var_dump( sprintf( _n( \'%s Comment\', \'%s Comments\', 9, \'text-domain\' ), 9 ) ); ?>
console.log( wp.i18n.sprintf( wp.i18n._n( \'%s Comment\', \'%s Comments\', 9, \'text-domain\' ), 9 ) );
正如您所看到的,与PHP不同,在JavaScript中,我们通过wp.i18n
这是翻译库。然而,并非所有功能都可用;例如_e()
功能如中所示<?php _e( \'Text\' ); ?>
&mdash;没有wp.i18n._e()
和wp.i18n._e( \'Text\' )
将导致错误。现在,对于该文本&mdash;When you’re ready, submit your work for review, and an Editor will be able to approve it for you.
&mdash;您要自定义的,可以在中的块编辑器脚本中找到它wp-includes/js/dist/editor.js
在名为PostPublishPanelPrepublish
:
prePublishBodyText = Object(external_this_wp_i18n_["__"])(\'When you’re ready, submit...\');
还有那个external_this_wp_i18n_
指向wp.i18n
; 因此external_this_wp_i18n_["__"])
相当于wp.i18n.__
.Now here\'s how you can change the text via JavaScript...
使用wp.i18n.setLocaleData():wp.i18n.setLocaleData({
\'String to translate #1\': [
\'Translated string - singular\',
\'Translated string - plural\'
],
\'String to translate #2\': [
\'Translated string\'
// No plural.
]
// Omit the text domain (or set it to \'\') if overriding the default WordPress translations.
}, \'text-domain\');
例如,在您的案例中:wp.i18n.setLocaleData({
\'When you’re ready, submit your work for review, and an Editor will be able to approve it for you.\': [
\'Publish when you are ready..\'
]
});
And here\'s how you could add the script to the post/page edit screen...
或任何管理页面/屏幕wp-i18n
脚本正在排队。add_action( \'admin_print_footer_scripts\', function(){
// Make sure the `wp-i18n` has been "done".
if ( wp_script_is( \'wp-i18n\' ) ) :
?>
<script>
// Note: Make sure that `wp.i18n` has already been defined by the time you call `wp.i18n.setLocaleData()`.
wp.i18n.setLocaleData({
\'When you’re ready, submit your work for review, and an Editor will be able to approve it for you.\': [
\'Publish when you are ready..\'
]
});
</script>
<?php
endif;
}, 11 );
当然,您可以使用其他类似的WordPress挂钩,或者将JavaScript代码放在.js
(外部JavaScript)文件,并通过指定wp-i18n
(记住,它是一个破折号,而不是一个点)作为依赖项。显示“其中一个用户将能够为您批准:(用户显示名称列表)。文本
add_action( \'admin_print_footer_scripts\', function(){
// Make sure the `wp-i18n` has been "done".
if ( wp_script_is( \'wp-i18n\' ) ) :
$wp_roles = wp_roles();
$role_names = [];
// Get the roles which have the publish_posts capability.
foreach ( $wp_roles->role_objects as $name => $role ) {
if ( $role && $role->has_cap( \'publish_posts\' ) ) {
$role_names[] = $name;
}
}
// Get the users which have the role(s) in the `$role_names`.
$users = get_users([
\'role__in\' => $role_names,
]);
$user_names = [];
foreach ( $users as $user ) {
$user_names[] = $user->display_name;
}
$data = [
\'When you’re ready, submit your work for review, and an Editor will be able to approve it for you.\' => [
\'One of these users will be able to approve it for you: \' . implode( \', \', $user_names )
],
];
?>
<script>
// Note: Make sure that `wp.i18n` has already been defined by the time you call `wp.i18n.setLocaleData()`.
wp.i18n.setLocaleData(<?php echo json_encode( $data ); ?>);
</script>
<?php
endif;
}, 11 );
如何测试上述代码段:将片段添加到主题的functions.php
文件
以任何具有“贡献者”角色的用户身份登录WordPress后端。
创建新帖子并单击“发布”按钮。
你应该看到this.