How to make your widget WPML sensitive

How to make your widget WPML sensitive

The market-leading plugin for making WordPress built sites multilanguage websites has a nice feature, called "String Translation". This feature allows you to have WPML sensitive fields in f.e. widgets , available for translation. This article shows  how can you make your plugin/widget WPML sensitive.

Smart Image Link WidgetExample

On the front page of this website there is a sample, for which it was required to have the caption texts translated. The plugin used for this is not the standard text plugin, which is already WPML sensitive. For this we used our own plugin "Smart Image Link Widget".

As you see the "image" has a text "We build the… Ext JS.". This text the "ecerpts" field value from the corresponding widget. In the image below you can see how that fits to the widget.

Making the widget WPML sensitive

To make our widget WPML sensitive we have modify the source code for the widget to register and update the ICL register settings of WPML.

The widget update function

Here we register the values that we like to translate recognizable for the WPML plugin. First we test if the function "icl_register_string" exists. This doesn’t hurt anything when your plugin is used on a site without WPML. The format of the parameters is:

  • Label (Title Text)
  • Widget title (widget title – ‘ . $this->id)
  • Instance value ($instance[‘title’]

In the WPML String Translation in the WordPress it will look like this:

WPML String Translation

As you can see there are 3 widget areas that are using this plugin/widget. They can be identified by their widget id. And you see also the excerpt text that can be translated to any of your languages.

The widget widget function

Now we have the make the output also WPML aware. For this we are using the "icl_t" function from the WPML plugin.

  1. We use a variable ($icl_t) for the function test of "icl_t" (a bit of code saving)
  2. Below this line is the actual placement of the translated value. As you can see, it checks if the function ($icl_t) exists.

    If it does, the "icl_t" function is called. The parameters are repeated as found in the "update" function.

Almost there

That is all there is to it. But we have still something to do. If the plugin you have modified was already in use, before the update, then you have to open the widget again in the WordPress dashboard and save it again. Otherwise nothing will be found by WPML, for it will save the values of the "update" in the WPML registry.

image

The last thing that remains is that we have to scan the strings on new values.

Go  to the WPML settings, option "String Translation" and look for the Original language of strings.

Press the "Save options and rescan strings" button. Now you should be able to translate your widget values.

Links

WPML  – the WordPress multilingual plugin

Johan van de Merwe
Dedicated to professional software development since 1985. Has worked since 1992 as IT manager in several international operating companies. Since 2007 CEO and Sencha Ext JS web application developer at Enovision GmbH.

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.