Displaying Widgets By Language

The majority of the themes created by GavickPro are fully-compatible with WPML, the awesome multilingual WordPress plugin that simplifies the translation process, making it easy to build multilingual sites and run them without all the fiddly bits that you might think such a complex task could cause. Check out the extensive feature list for this plugin here if you’re interested in reaching a wider audience. At the moment we’re working hard on making our newer themes like Events and Steakhouse compatible too.

Displaying Widgets By Language

On our forum we find that our users often have questions about translating widgets with WPML. Default WordPress text widgets may be translated easily via the "WPML -> String Translation" view, but with our more advanced and flexible widgets, such as News Show Pro or Image Show, you may actually want to display a different version of the widget for each language. I’ll cover the basic steps needed to do this in the following section.

Assigning widgets by language.

First of all we’ll need another, very simple WordPress plugin called Widget Logic. Widget Logic is a plugin that allows you to control where a widget is displayed using PHP code. You can use it to specify where a widget should appear and when, including specifying whether it should display according to the language selected.

Start by downloading and activating the plugin as you would any other; nothing special needs to be done here! Now, we can start adding our widgets. As an example, we’ll use our News Show Pro widget on a dual-language site; just stick the widget in any available widget area, such as the sidebar and configure it with your preferred options.

Now comes the important bit. At the bottom of the widget options there should be an additional field called, funnily enough, "widget logic"; this is where we’ll need to add our code to limit the widget to appearing only under a specific language:

News Show pro and widget logic

The logic that we must add to this field uses WPML’s language code constant – ICL_LANGUAGE_CODE (WPML reference) For example, if we want to limit the widget to only appearing on the English version of the site, we should add:

ICL_LANGUAGE_CODE == 'en'

…To the field. This condition checks to see which language has been selected via WPML, then checks whether it matches the language specified in the code; if it does, then the widget is displayed.

Now we can add a second instance of the News Show Pro widget to the same location, and again configure it to suit our needs – we might choose a different category specific to the second language, or amend the data source to pull specific alternate-language articles for display etc…Once done, we can assign a different language code in the widget logic field; in this case we’ll set the logic to only display the widget on Polish language pages. :

ICL_LANGUAGE_CODE == 'pl'

If you have more than two languages on your site, you will need to add an NSP instance and configure it for each language as per the above, unless you wish to use the same widget for multiple languages.

Displaying Widgets By Language 4.675 (93.33%) 9 votes
Share
This article was first published November 4th, 2014