WordPress提供_x()
函数,就像__()
, 但它增加了$context
用于区分相同字符串的参数。
在下面的示例中,在页脚中生成了一些输出。可翻译字符串为Message
在这两种情况下都使用,但在第一种情况下,Message
给出了上下文for use in footer text
.
接下来,我们使用gettext_with_context
和gettext
分别筛选以独立地翻译每个字符串。
// Generate output for our example.
add_action( \'wp_footer\', \'wpse_example_strings\' );
function wpse_example_strings() {
// Note that concatenating strings is not translation friendly. It\'s done here for simplicity.
echo \'Message string with context: \' . _x( \'Message\', \'for use in footer text\', \'text_domain\' ) . \'<br>\';
echo \'Message string without context: \' . __( \'Message\', \'text_domain\' ) . \'<br>\';
}
/**
* Translate text with context.
*
* https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext_with_context
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string
*/
add_filter( \'gettext_with_context\', \'wpse_gettext_with_context\', 10, 4 );
function wpse_gettext_with_context( $translation, $text, $context, $domain ) {
if ( \'text_domain\' === $domain ) {
if ( \'Message\' === $text && \'for use in footer text\' === $context ) {
$translation = \'Message Us\';
}
}
return $translation;
}
/**
* Translate text without context.
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*
* @return string
*/
add_filter( \'gettext\', \'um_rename_messagetxt\', 10, 3);
function um_rename_messagetxt( $translation, $text, $domain ) {
if ( \'text_domain\' === $domain ) {
if ( \'Message\' === $text ) {
$translation = \'Contact Us\';
}
}
return $translation;
}
编辑:终极会员消息按钮定制我仔细研究了这一点,不幸的是插件作者并没有让更改按钮文本变得非常容易。
这里有一个解决方案,它将用我们自己的分叉版本替换默认的最终成员消息按钮的短代码。请注意,我们可以回收ultimatemember_message_button
名称我建议用这段代码制作一个插件:
<?php
/*
Plugin Name: Ulimate Member Custom Message Button Shortcode
Plugin URI:
Description:
Version: 0.0.1
Author:
Author URI:
License: GPL2/Creative Commons
*/
/**
* Remove the default UM Message shortcode and wire up our forked version.
*/
add_action( \'init\', \'wpse_ultimatemember_message_button_custom\' );
function wpse_ultimatemember_message_button_custom() {
global $um_messaging;
remove_shortcode( \'ultimatemember_message_button\', [ $um_messaging->shortcode, \'ultimatemember_message_button\' ] );
add_shortcode( \'ultimatemember_message_button\', \'wpse_ultimatemember_message_button\' );
}
/**
* Customized version of ultimatemember_message_button shortcode, which allows
* for the button\'s label to be specified using the \'label\' parameter.
*/
function wpse_ultimatemember_message_button( $args = array() ) {
global $ultimatemember, $um_messaging;
$defaults = array(
\'user_id\' => 0,
\'label\' => __( \'Message\',\'um-messaging\' ),
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
$current_url = $ultimatemember->permalinks->get_current_url();
if( um_get_core_page(\'user\') ){
do_action("um_messaging_button_in_profile", $current_url, $user_id );
}
if ( !is_user_logged_in() ) {
$redirect = um_get_core_page(\'login\');
$redirect = add_query_arg(\'redirect_to\', $current_url, $redirect );
$btn = \'<a href="\' . $redirect . \'" class="um-login-to-msg-btn um-message-btn um-button" data-message_to="\'.$user_id.\'">\'. $label .\'</a>\';
return $btn;
} else if ( $user_id != get_current_user_id() ) {
if ( $um_messaging->api->can_message( $user_id ) ) {
$btn = \'<a href="#" class="um-message-btn um-button" data-message_to="\'.$user_id.\'"><span>\'. $label .\'</span></a>\';
return $btn;
}
}
}
更新模板以调用
ultimatemember_message_button
使用我们的新
label
参数如下:
<div class="msgshort">
<?php echo do_shortcode(\'[ultimatemember_message_button user_id=2 label="Contact US"]\'); ?>
</div>
这并不是最好的解决方案,但我们的选择受到插件实现方式的限制。我建议为
label
要添加的参数。