Last updated on October 18, 2023

WPML string translation troubleshooting

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Troubleshoot WPML string translation problems.

The code snippet below can be useful for troubleshooting issues related to WPML string translation in WordPress. It allows you to check if a string is translatable and if it has been translated in the current language.

/**
 * Check if a string is translatable and if it has been translated in the current language.
 *
 * @param string $string The string to check.
 * @param string $context The context of the string (optional).
 * @return bool True if the string is translatable and has been translated, false otherwise.
 */
function wpsnippets_is_string_translated( $string, $context = '' ) {
    global $sitepress;

    if ( ! isset( $sitepress ) ) {
        return false;
    }

    $translations = $sitepress->get_translations_of_original( $string, $context );

    if ( empty( $translations ) ) {
        return false;
    }

    $current_language = $sitepress->get_current_language();

    return isset( $translations[ $current_language ] );
}

To use this code snippet, you can call the wpsnippets_is_string_translated() function and pass the string you want to check as the first parameter. Optionally, you can also provide the context of the string as the second parameter.

The function will return true if the string is translatable and has been translated in the current language, and false otherwise.

This code snippet can be useful when troubleshooting issues with WPML string translation, such as checking if a specific string is being translated correctly or if it has been translated at all in the current language.

Examples

Example 1: Checking if WPML String Translation is active

This example demonstrates how to check if the WPML String Translation plugin is active on a WordPress site.

if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
    // WPML String Translation is active
} else {
    // WPML String Translation is not active
}

The code checks if the constant ICL_SITEPRESS_VERSION is defined, which indicates that the WPML String Translation plugin is active. If the constant is defined, the plugin is active, otherwise it is not.

Example 2: Getting the translation of a string

This example shows how to get the translation of a string using the WPML String Translation plugin.

$translated_string = apply_filters( 'wpml_translate_single_string', $string, $domain, $language_code );

The code uses the wpml_translate_single_string filter to get the translation of a string. The $string parameter is the original string, $domain is the text domain of the string, and $language_code is the language code of the desired translation. The translated string is returned in the $translated_string variable.

Example 3: Troubleshooting missing translations

This example demonstrates how to troubleshoot missing translations when using the WPML String Translation plugin.

add_filter( 'wpml_translation_priority', 'wpsnippets_adjust_translation_priority', 10, 2 );

function wpsnippets_adjust_translation_priority( $priority, $string ) {
    if ( empty( $string ) ) {
        $priority = 1;
    }
    return $priority;
}

The code adds a filter to adjust the translation priority when a string is empty. If a translation is missing for a string, the priority is set to 1, which gives it the highest priority. This can help troubleshoot and identify missing translations in the WPML String Translation plugin.

Last updated on October 18, 2023. Originally posted on December 21, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *