Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A third party developer made a text widget with title, body and thumbnail. This accompany widget plugin has not been internationalized and therefore the strings do not appear in WPML's string translation management. I need them to be loaded. So I am trying to register the plugings domain as indicated here, but the code I added:

function accompany_text_init() {
    $plugin_dir = basename(dirname(__FILE__));
    load_plugin_textdomain( 'accompany-text', false, $plugin_dir );
}
add_action('init', 'accommpany_text_init');

does not load. It seems as the widget is not registered. Here is the full code. What am I doing wrong? Is the call position off? A plugin coding newbie here and a junior PHP-er so would appreciate some help.

share|improve this question

closed as off-topic by Mark Kaplun, cybmeta, Bainternet Dec 12 at 10:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Could you please clean up the code of the pastebin? It's actually unreadable. –  kaiser Feb 15 '12 at 10:16

3 Answers 3

up vote 0 down vote accepted

After some great help - even though a bit late - from the guys at WPML, especially @Brooks I managed to ge the code fixed. The icl_register_strings() function was in the wrong place so WPML I shifted it to the update() Method from where one can sanitize the instance variables before they are used by the widget. The icl_t() function was put in the widget() Method. The widget() which is used to display content within the widget when it appears in the sidebar on your blog. They also changed one of the $name argument of icl_register_string($context, $name, $value) to 'widget body – ' . $this->id so we can now have unique values stored for that value. Code can be seen here: https://github.com/jasperf/Accompany-Widget

share|improve this answer

You will need to add the following

$plugin_dir = basename(dirname(__FILE__)); 
load_plugin_textdomain( 'accompany-text', false, $plugin_dir );

inside where the widget class is initialised. In your example, inside the AccompanyPostWidget function.

That said, there doesn't appear to be any text strings that are translatable in the widget class you've linked to. For instance, to translate the word number (as in Toscho's answer)

<?php _e( 'Number', 'accompany-text' ); ?>

As a more complete example, the following loads the text domain and makes the Widget description translatable:

 function AccompanyPostWidget(){
    $plugin_dir = basename(dirname(__FILE__)); 
    load_plugin_textdomain( 'accompany-text', false, $plugin_dir );
    $widget_ops = array('classname' => 'AccompanyPostWidget', 'description' => __( 'Displays a Accompany post with thumbnail','accompany-text');
    $this->WP_Widget('AccompanyPostWidget', 'Accompany Text Widget', $widget_ops);
  }

Side remark

The following would 'normally' be sufficient, but isn't loaded in time to translate the widget's title and description:

function accompany_text_init() {
    $plugin_dir = basename(dirname(__FILE__));
    load_plugin_textdomain( 'accompany-text', false, $plugin_dir );
}
add_action('init', 'accompany_text_init');
share|improve this answer
    
Yeah, I just realized that I have not added any gettext calls. Silly me. Working on that now. –  rhand Feb 15 '12 at 10:25
    
Updated the pastie, but I only get an error when I added the domain initiation to the widget class. 'Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'accommpany_text_init' was given in /wp-includes/plugin.php on line 405' –  rhand Feb 15 '12 at 10:38
    
You need to remove the function and the add_action that was in the question from your code and just add those two lines from my answer inside AccompanyPostWidget –  Stephen Harris Feb 15 '12 at 10:44
    
Done that. Updated pastie. Code is added to function AccompanyPostWidget() and I have added one __(): ?php printf(__($this->get_field_id('text'))); ?> Nothing loaded yet but no errors either –  rhand Feb 15 '12 at 10:56
    
That's because you are trying to translate the field's ID rather than it's label text. See Toscho's answer, but I shall edit my answer. –  Stephen Harris Feb 15 '12 at 11:06

You have to use translation functions.

Example:

_e( 'Number', 'accompany-text' );
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.