apply_filters( 'wpml_translate_single_string', string $original_value, string $domain, string $name, string $language_code )
- Type
- filter
- Category
- Retrieving Localized Content
- WPML Version
- 3.2
Description
*Retrieves an individual (as opposed to a string that is part of a package**) text string translation. The filter looks for a string with matching $domain and $name. If it finds it, it looks for a translation in the current language or the language you specify. If a translation exists, it will return it. Otherwise, it will return the original string.
*To register a string for translation please see: wpml_register_single_string
**For retrieving a string translation that is part of a package please see: wpml_translate_string
Note: This hook requires the WPML String Translation module
Arguments
- $original_value
- (string) (Required) The string’s original value
- $domain
- (string) (Required) The string’s registered domain
- $name
- (string) (Required) The string’s registered name
- $language_code
- (string) (Optional) Return the translation in this language. Default is NULL which returns the current language
Example usage
We pick up the same example of the widget we used for the“wpml_register_single_string” action hook. You may want to go there to refresh your memory. We use the “wpml_translate_single_string” filter in the widget’s output function
Example
function widget($args, $instance){
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
$custom_input = apply_filters('wpml_translate_single_string', $instance['custom_input'], 'Widgets', 'Custom Widget - input field' );
$custom_textarea = apply_filters('wpml_translate_single_string', $instance['custom_textarea'], 'Widgets', 'Custom Widget - textarea field' );
# Before the widget
echo $before_widget;
# The title
if ( $title )
echo $before_title . $title . $after_title;
# Output...
Related hooks
wpml_media_url, wpml_media_id, force_suppress_filters, wpml_get_translated_slug, wpml_home_url, wpml_translate_string, wpml_elements_without_translations, wpml_permalink, wpml_unfiltered_admin_string, wpml_element_link