WPML
SignaturePHP
apply_filters( 'wpml_translate_single_string', string $original_value, string $domain, string $name, string $language_code )

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_valuestringRequired

    The string’s original value
  • $domainstringRequired

    The string’s registered domain
  • $namestringRequired

    The string’s registered name
  • $language_codestringOptional

    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 (opens in a new tab)

PHP
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...