Last updated on October 18, 2023

WPML translate plugin settings

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

Configure WPML translation settings for your needs.

The WPML plugin is a popular choice for translating WordPress websites into multiple languages. To translate the plugin settings, you can use the icl_translate function provided by WPML. This function allows you to translate any string or value using the WPML translation system.

Here’s an example of how you can use the icl_translate function to translate a plugin setting:

$setting_value = get_option( 'my_plugin_setting' );
$translated_value = icl_translate( 'my-plugin', 'my_plugin_setting', $setting_value );

In the example above, we retrieve the value of a plugin setting using the get_option function. Then, we pass the value to the icl_translate function along with the context (‘my-plugin’) and the name of the setting (‘mypluginsetting’). The function returns the translated value, which can be stored in the $translated_value variable.

This code snippet can be useful when you want to provide multilingual support for your plugin settings. By using the icl_translate function, you ensure that the settings are displayed in the correct language for each user.

Note: In the example above, ‘my-plugin’ is the context used to group the translation strings. It should be unique to your plugin to avoid conflicts with other translations.

Examples

Example 1: Translating Plugin Settings with WPML

This use case demonstrates how to translate plugin settings using the WPML translate plugin. The code example shows how to add translation support for a custom plugin setting.

function wpsnippets_translate_plugin_settings() {
    $setting_value = get_option( 'my_plugin_setting' );
    $translated_value = apply_filters( 'wpml_translate_single_string', $setting_value, 'my-plugin', 'my-plugin-setting', get_locale() );
    update_option( 'my_plugin_setting', $translated_value );
}
add_action( 'admin_init', 'wpsnippets_translate_plugin_settings' );

In this code example, we retrieve the value of a custom plugin setting using get_option(). We then use the wpml_translate_single_string filter to translate the setting value. The translated value is then saved back to the database using update_option().

Example 2: Translating Plugin Settings with WPML and Polylang

This use case demonstrates how to translate plugin settings using both WPML and Polylang plugins. The code example shows how to add translation support for a custom plugin setting that works with both translation plugins.

function wpsnippets_translate_plugin_settings() {
    $setting_value = get_option( 'my_plugin_setting' );

    if ( function_exists( 'pll_current_language' ) ) {
        $current_language = pll_current_language();
    } else {
        $current_language = apply_filters( 'wpml_current_language', NULL );
    }

    if ( $current_language ) {
        $translated_value = apply_filters( 'wpml_translate_single_string', $setting_value, 'my-plugin', 'my-plugin-setting', $current_language );
        update_option( 'my_plugin_setting', $translated_value );
    }
}
add_action( 'admin_init', 'wpsnippets_translate_plugin_settings' );

In this code example, we retrieve the value of a custom plugin setting using get_option(). We then check if the Polylang plugin is active using function_exists(). If Polylang is active, we use pll_current_language() to get the current language. If Polylang is not active, we use wpml_current_language filter to get the current language from WPML. We then use the wpml_translate_single_string filter to translate the setting value based on the current language. The translated value is then saved back to the database using update_option().

Example 3: Translating Plugin Settings with WPML and qTranslate-X

This use case demonstrates how to translate plugin settings using both WPML and qTranslate-X plugins. The code example shows how to add translation support for a custom plugin setting that works with both translation plugins.

function wpsnippets_translate_plugin_settings() {
    $setting_value = get_option( 'my_plugin_setting' );

    if ( function_exists( 'qtranxf_getLanguage' ) ) {
        $current_language = qtranxf_getLanguage();
    } else {
        $current_language = apply_filters( 'wpml_current_language', NULL );
    }

    if ( $current_language ) {
        $translated_value = apply_filters( 'wpml_translate_single_string', $setting_value, 'my-plugin', 'my-plugin-setting', $current_language );
        update_option( 'my_plugin_setting', $translated_value );
    }
}
add_action( 'admin_init', 'wpsnippets_translate_plugin_settings' );

In this code example, we retrieve the value of a custom plugin setting using get_option(). We then check if the qTranslate-X plugin is active using function_exists(). If qTranslate-X is active, we use qtranxf_getLanguage() to get the current language. If qTranslate-X is not active, we use wpml_current_language filter to get the current language from WPML. We then use the wpml_translate_single_string filter to translate the setting value based on the current language. The translated value is then saved back to the database using update_option().

Last updated on October 18, 2023. Originally posted on January 11, 2024.

Leave a Reply

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