Last updated on October 18, 2023

WPML translation string not working

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

Fix issues with WPML translation strings.

The code snippet provided below demonstrates how to use the WPML plugin to translate strings in WordPress. This can be useful when you have a multilingual website and need to display different content based on the user’s language preference.

// Example translation string
$translation_string = __( 'Hello, world!', 'text-domain' );

// Translate the string using WPML
$translated_string = apply_filters( 'wpml_translate_single_string', $translation_string, 'text-domain', 'Translation context', 'Language code' );

// Output the translated string
echo $translated_string;

In the code snippet above, we first define the translation string using the __() function. The first parameter is the string to be translated, and the second parameter is the text domain, which helps WPML identify the correct translation file.

Next, we use the apply_filters() function to call the wpml_translate_single_string filter hook. This hook allows WPML to translate the string based on the provided parameters. The first parameter is the original translation string, followed by the text domain, translation context, and language code.

Finally, we output the translated string using echo.

Remember to replace 'Hello, world!' with your actual translation string and 'text-domain' with the appropriate text domain for your theme or plugin.

This code snippet can be useful when you want to display translated content on your website based on the user’s language preference. WPML provides a convenient way to handle translations in WordPress, ensuring a seamless multilingual experience for your visitors.

Examples

Example 1: Using icl_translate() to Translate a String with WPML

This example demonstrates how to use the icl_translate() function provided by WPML to translate a string in WordPress.

$translated_string = icl_translate( 'text-domain', 'Hello, world!', 'fr' );

In this code example, the icl_translate() function is used to translate the string “Hello, world!” from the text domain “text-domain” to French. The translated string is stored in the variable $translated_string.

Example 2: Using __() Function with WPML for String Translation

This example showcases the usage of the __() function, which is a WordPress core function, along with WPML to translate a string.

$translated_string = __( 'Hello, world!', 'text-domain' );

In this code example, the __() function is used to translate the string “Hello, world!” from the text domain “text-domain”. WPML automatically handles the translation based on the active language.

Example 3: Using wpml_translate_single_string() to Translate a String

This example demonstrates how to use the wpml_translate_single_string() function provided by WPML to translate a string in WordPress.

$translated_string = wpml_translate_single_string( 'text-domain', 'Hello, world!', 'fr' );

In this code example, the wpml_translate_single_string() function is used to translate the string “Hello, world!” from the text domain “text-domain” to French. The translated string is stored in the variable $translated_string.

Last updated on October 18, 2023. Originally posted on November 28, 2023.

Leave a Reply