Answers:
如果您查看/wp-admin/edit-form-advanced.php
,则会找到元框:
add_meta_box('submitdiv', __('Publish'), 'post_submit_meta_box', $post_type, 'side', 'core');
注意__('Publish')
–函数__()
会指向translate()
您获取过滤器的位置'gettext'
。
有两种方法可以解决您的问题:1.在单个专用函数中处理字符串(请确保匹配正确的textdomain!);或2.使用更通用的方法。
@Rarst 刚刚发布了版本1,因此我将添加版本2。:)
<?php
/*
Plugin Name: Retranslate
Description: Adds translations.
Version: 0.1
Author: Thomas Scholz
Author URI: http://toscho.de
License: GPL v2
*/
class Toscho_Retrans {
// store the options
protected $params;
/**
* Set up basic information
*
* @param array $options
* @return void
*/
public function __construct( array $options )
{
$defaults = array (
'domain' => 'default'
, 'context' => 'backend'
, 'replacements' => array ()
, 'post_type' => array ( 'post' )
);
$this->params = array_merge( $defaults, $options );
// When to add the filter
$hook = 'backend' == $this->params['context']
? 'admin_head' : 'template_redirect';
add_action( $hook, array ( $this, 'register_filter' ) );
}
/**
* Conatiner for add_filter()
* @return void
*/
public function register_filter()
{
add_filter( 'gettext', array ( $this, 'translate' ), 10, 3 );
}
/**
* The real working code.
*
* @param string $translated
* @param string $original
* @param string $domain
* @return string
*/
public function translate( $translated, $original, $domain )
{
// exit early
if ( 'backend' == $this->params['context'] )
{
global $post_type;
if ( ! empty ( $post_type )
&& ! in_array( $post_type, $this->params['post_type'] ) )
{
return $translated;
}
}
if ( $this->params['domain'] !== $domain )
{
return $translated;
}
// Finally replace
return strtr( $original, $this->params['replacements'] );
}
}
// Sample code
// Replace 'Publish' with 'Save' and 'Preview' with 'Lurk' on pages and posts
$Toscho_Retrans = new Toscho_Retrans(
array (
'replacements' => array (
'Publish' => 'Save'
, 'Preview' => 'Lurk'
)
, 'post_type' => array ( 'page', 'post' )
)
);
您无需将代码用作插件。在主题的functions.php中包含它就足够了。
要删除原始的“保存”按钮(不确定“草稿”按钮是什么),请将以下代码添加到您的functions.php / a插件中:
add_action( 'admin_print_footer_scripts', 'remove_save_button' );
function remove_save_button()
{
?>
<script>
jQuery(document).ready(function($){$('#save-post').remove();});
</script><?php
}
是的,这很丑。
post_submit_meta_box()
仅转储其内容即可。完全没有过滤器。Javascript或ob_start()
可以解决这个问题。两者都不是很优雅。
hakre建议使用翻译过滤器的代码如下所示:
add_filter( 'gettext', 'change_publish_button', 10, 2 );
function change_publish_button( $translation, $text ) {
if ( $text == 'Publish' )
return 'Save';
return $translation;
}
为了保持一致性,我假设您希望在整个Wordpress中使用“保存”而不是“发布”,而不是仅在按钮文本上使用。
在@Rarst提供的解决方案的基础上,我扩展了代码,以便将“发布”一词的许多(全部?)其他实例转换为“保存”的适当变体。
add_filter("gettext", "translate_publish_post_status", 10, 2);
add_filter("ngettext", "translate_publish_post_status_number", 10, 5);
add_filter("ngettext_with_context", "translate_publish_post_status_number", 10, 6);
function translate_publish_post_status($translation, $text) {
switch($text) {
case "Publish": return "Save";
case "Published on: <b>%1\$s</b>": return "Saved on: <b>%1\$s</b>";
case "Publish <b>immediately</b>": return "Approve <b>immediately</b>";
case "Publish on: <b>%1\$s</b>": return "Approve on: <b>%1\$s</b>";
case "Privately Published": return "Privately Saved";
case "Published": return "Saved";
case "Save & Publish": return "Save"; //"Double-save"? :)
default: return $translation;
}
}
function translate_publish_post_status_number($translation, $single, $plural, $number, $domain) {
if($translation == "Published <span class=\"count\">(%s)</span>") {
return "Saved <span class=\"count\">(%s)</span>";
}
return $translation;
}
要删除“草稿”按钮,@ toscho给出的答案是一个很好的解决方案。
%1$s
为%1\$s
。或者,关闭通知/警告将隐藏这些错误。