我一直在研究一個WordPress小部件,以在選擇的側邊欄中顯示代碼文本和縮略圖。爲了讓多語言WPML插件抓取文本字符串進行翻譯,我不得不添加一些代碼來正確註冊這些文本字符串。現在有兩個問題,WPML似乎無法解決這個問題,或者無論如何都沒有時間。一,字符串註冊兩次WPML語言字符串和兩個,翻譯後的字符串不存儲在數據庫中。這裏是我要顯示插件的代碼:由WPML插件加載兩次Widget數據和未存儲在數據庫中的翻譯
<?php
/*
Plugin Name: Accompany Text Widget
Plugin URI: http://imagewize.com/
Description: Accompany Post Widget grabs a Accompany post and the associated thumbnail to display on your sidebar based on egetway.com 's development
Author: Jasper Frumau
Version: 2
Author URI: http://jasperfrumau.com/
*/
class AccompanyPostWidget extends WP_Widget
{
function AccompanyPostWidget()
{
$widget_ops = array('classname' => 'AccompanyPostWidget', 'description' => 'Displays a Accompany post with thumbnail');
$this->WP_Widget('AccompanyPostWidget', 'Accompany Text Widget', $widget_ops);
$plugin_dir = basename(dirname(__FILE__));
load_plugin_textdomain('accompany-text', false, $plugin_dir);
}
function form($instance)
{
$instance = wp_parse_args((array) $instance, array('number' => '','text' => '', 'imagesPath' => '','cssClass' => ''));
$number = $instance['number'];
$text = $instance['text'];
$widgetNo="widgetname".mt_rand(); //.substr(md5('widgetname'), 0, 4);
icl_register_string('Accompany Text',$widgetNo, $text);
$imagesPath= $instance['imagesPath'];
$cssClass= $instance['cssClass'];
?>
<p><label for="<?php echo $this->get_field_id('number'); ?>">Number : <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo esc_attr($number); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('text'); ?>">Text :
<textarea class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo esc_attr(icl_t('Accompany Text', $widgetNo, $text));?></textarea>
</label></p>
<p><label for="<?php echo $this->get_field_id('imagesPath'); ?>">images path : <input class="widefat" id="<?php echo $this->get_field_id('imagesPath'); ?>" name="<?php echo $this->get_field_name('imagesPath'); ?>" type="text" value="<?php echo esc_attr($imagesPath); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('cssClass'); ?>">Css Class : <input class="widefat" id="<?php echo $this->get_field_id('cssClass'); ?>" name="<?php echo $this->get_field_name('cssClass'); ?>" type="text" value="<?php echo esc_attr($cssClass); ?>" /></label></p>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['number'] = $new_instance['number'];
$instance['text'] = $new_instance['text'];
$instance['imagesPath'] = $new_instance['imagesPath'];
$instance['cssClass'] = $new_instance['cssClass'];
return $instance;
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
echo $before_widget;
$number = empty($instance['number']) ? ' ' : apply_filters('widget_number', $instance['number']);
$text = empty($instance['text']) ? ' ' : apply_filters('widget_text', $instance['text']);
$imagesPath = empty($instance['imagesPath']) ? ' ' : apply_filters('widget_imagesPath', $instance['imagesPath']);
$cssClass = empty($instance['cssClass']) ? ' ' : apply_filters('widget_cssClass', $instance['cssClass']);
// WIDGET CODE GOES HERE
echo '<div class="box '.$cssClass.'">
<div class="round">
<div class="con">';
if (!empty($number)){ echo ' <span class="num">'.$number.'</span>';}
echo $text;
if (!empty($imagesPath)){ echo '<div class="iconBox"><img src="'.$imagesPath.'" class="icon" /> </div>';}
echo ' </div>
</div>
</div>';
echo $after_widget;
}
}
add_action('widgets_init', create_function('', 'return register_widget("AccompanyPostWidget");'));?>
任何人都可以看到爲什麼WPML字符串會爲每個插件存儲兩遍?還有兩點,爲什麼字符串沒有存儲在數據庫中的任何想法。我想第二個問題仍然是WPML的答案,但我確實希望有人能夠幫助我解決第一個問題。
更新:
去掉一個錯誤:Notice: attribute_escape is <strong>deprecated</strong> since version 2.8! Use esc_attr() instead.
在做這個錯誤: Notice: Undefined variable: number in /home/domain.com/wp-content/plugins/accompany-Text/accompany-Text.php on line 36
更新2: 號碼已被定義爲變量現在也是。 WPML仍然從這些小部件獲取文本兩次作爲要翻譯的字符串。三個小部件和六個字符串需要翻譯。
我幾天前收到來自論壇的更新 - 3月28日。由於我不得不等待這個,所以我不得不專注於其他一些項目。我會盡快檢查這些代碼。 – rhand 2012-04-09 08:45:48
答案由WPML提供。需要更改一些代碼。請參閱https://gist.github.com/3109606 – rhand 2012-07-14 05:59:13