我尝试使用以下代码更改文本
function ctxtlearn_gettext( $ctxttranslation, $ctxttext ) {
$ctxttrans = array_values(changeTxt_setting(\'trans\',true));
$ctxtdirty = false;
$ctxtstrings_map = array();
$ctxttext_words = explode( \' \', $ctxttext );
foreach($ctxttrans as $ctxtmytrans) {
$ctxtstrings_map[$ctxtmytrans[\'key\']] = $ctxtmytrans[\'val\'];
}
foreach ( $ctxttext_words as $ctxtk => $ctxtword ) {
$ctxtw = trim( $ctxtword, \'.,:!?-_()\' );
if ( isset( $ctxtstrings_map[ $ctxtw ] ) ) {
$ctxttext_words[ $ctxtk ] = str_replace( $ctxtw, $ctxtstrings_map[ $ctxtw ], $ctxtword );
$ctxtdirty = true;
}
}
if ( $ctxtdirty ) {
return implode( \' \', $ctxttext_words );
}
return $ctxttranslation;
}
add_filter( \'gettext\', \'ctxtlearn_gettext\', 10, 2 );
如果我给出以下输入,它就会工作
Post => My Book
Page => My Vertical
Comments => My Feedback
=>之前的单词是旧字符串,之后是新字符串。如果我将其作为数组传递,它就会工作。但是如果我提供以下输入
First Name => My Name
Username => User ID
在配置文件页面中,用户名文本会更改,但名字不会更改。但如果我分裂并提供它的变化。
我可以通过提供整个字符串来做到这一点。。?
最合适的回答,由SO网友:Varun Sridharan 整理而成
我自己找到了答案。。
以下代码正在运行
add_filter( \'gettext\', \'wps_translate_words_array\' );
add_filter( \'ngettext\', \'wps_translate_words_array\' );
function wps_translate_words_array( $translated ) {
$words = array(
// \'word to translate\' = > \'translation\'
\'Posts\' => \'Article\',
\'Post\' => \'Articles\',
\'Pages\' => \'Stuffing\',
\'Media\' => \'Upload Images\',
\'Links\' => \'Blog Roll\',
);
$translated = str_ireplace( array_keys($words), $words, $translated );
return $translated;
}