Last updated on October 18, 2023

WPML translate Polylang settings

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

Translate Polylang settings to WPML.

The code snippet provided below demonstrates how to translate Polylang settings using WPML in WordPress. This can be useful when you have a multilingual website and want to translate the settings of the Polylang plugin using WPML.

/**
 * Translate Polylang settings using WPML.
 */
function wpsnippets_translate_polylang_settings() {
    if ( function_exists( 'icl_register_string' ) ) {
        // Register the strings to be translated
        icl_register_string( 'polylang', 'polylang_setting_1', 'Polylang Setting 1' );
        icl_register_string( 'polylang', 'polylang_setting_2', 'Polylang Setting 2' );

        // Translate the strings
        $translated_setting_1 = icl_t( 'polylang', 'polylang_setting_1', get_option( 'polylang_setting_1' ) );
        $translated_setting_2 = icl_t( 'polylang', 'polylang_setting_2', get_option( 'polylang_setting_2' ) );

        // Use the translated settings
        echo $translated_setting_1;
        echo $translated_setting_2;
    }
}

In this code snippet, we first check if the WPML plugin is active by using the function_exists() function. Then, we register the strings that we want to translate using the icl_register_string() function. The first parameter is the context, which can be any unique identifier for the group of strings. The second parameter is the name of the string to be translated, and the third parameter is the default value of the string.

After registering the strings, we use the icl_t() function to translate the strings. The first parameter is the context, the second parameter is the name of the string, and the third parameter is the default value of the string. The translated values are stored in variables.

Finally, we can use the translated settings as per our requirements. In the example code, we echo the translated settings, but you can use them in any way you need.

Note: This code assumes that you have WPML and Polylang plugins installed and activated on your WordPress website.

Examples

Example 1: Translating Polylang settings using WPML

This example demonstrates how to use WPML to translate Polylang settings. The code snippet below shows how to add a translation string for a Polylang setting using the wpsnippets_wpml_register_string() function.

wpsnippets_wpml_register_string( 'polylang', 'setting_key', 'Setting Label', 'Setting Description' );

The wpsnippets_wpml_register_string() function registers a string for translation with WPML. The first parameter is the context, which is set to ‘polylang’ in this example. The second parameter is the setting key, which should match the key used in the Polylang settings array. The third parameter is the label of the setting, and the fourth parameter is the description of the setting.

Example 2: Translating Polylang settings using WPML with a custom domain

This example demonstrates how to use WPML to translate Polylang settings with a custom domain. The code snippet below shows how to add a translation string for a Polylang setting using the wpsnippets_wpml_register_string() function with a custom domain.

wpsnippets_wpml_register_string( 'polylang', 'setting_key', 'Setting Label', 'Setting Description', 'my-plugin' );

The fifth parameter of the wpsnippets_wpml_register_string() function is the domain, which is set to ‘my-plugin’ in this example. This allows WPML to group the translation strings under a specific domain, making it easier to manage translations for different plugins or themes.

Example 3: Translating Polylang settings using WPML with a specific language

This example demonstrates how to use WPML to translate Polylang settings for a specific language. The code snippet below shows how to add a translation string for a Polylang setting using the wpsnippets_wpml_register_string() function with a specific language.

wpsnippets_wpml_register_string( 'polylang', 'setting_key', 'Setting Label', 'Setting Description', '', 'fr' );

The sixth parameter of the wpsnippets_wpml_register_string() function is the language, which is set to ‘fr’ in this example. This allows WPML to associate the translation string with a specific language, ensuring that the correct translation is displayed based on the user’s language preference.

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

Leave a Reply