If your widgets include translatable strings, you’ll be able to edit the translation with WPML, but translations are not guaranteed to display on the site.
WPML uses the standard WordPress filters and translates content that you filter.
All the standard WordPress widgets apply filters by default. When you create your own widgets, you need to make sure that the same happens too.
For instance, to apply filters to a widget title, use this:
$wtitle = apply_filters('widget_title', $wtitle);
WPML connects to the apply_filters call and will make this string translatable via the String Translation page.
Alternatively, you can use WPML’s string translation functions manually. You can register strings for translation and then look up their translation when displaying.
To register strings, use the icl_register_string call:
icl_register_string($context, $name, $value)
- $context – the name of the plugin, in a human readable format
- $name – the name of the string which helps the user (or translator) understand what’s being translated.
- $value – the string that needs to be translated.
Then, when you display the texts, use icl_t to display with translations:
icl_t($context, $name, $value)
The two methods described before are best for dynamically generated texts. These texts are editable by users via the WordPress Admin panel.
If you have static texts, which are hard-coded in the PHP, you can just use GetText calls, like:
<?php __('Hello world','cool_theme'); ?>
WPML hooks to the GetText calls and makes these texts translatable via the String Translation interface. Please note that this method only works for texts that cannot be edited and are fixed in the theme.