Last updated on October 18, 2023

WPML translate Customizer settings

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

Translate Customizer settings with WPML.

To translate Customizer settings in WPML, you can use the wpml_translate_string function provided by the WPML plugin. This function allows you to translate any string, including Customizer settings, using the WPML translation interface.

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

function wpsnippets_translate_customizer_setting( $value ) {
    // Check if WPML is active
    if ( function_exists( 'wpml_translate_string' ) ) {
        // Translate the Customizer setting using WPML
        $translated_value = wpml_translate_string( 'Customizer', $value );

        // Return the translated value
        return $translated_value;
    }

    // If WPML is not active, return the original value
    return $value;
}

In this example, the wpsnippets_translate_customizer_setting function takes a value as input and checks if WPML is active. If WPML is active, it uses the wpml_translate_string function to translate the value. If WPML is not active, it simply returns the original value.

You can use this function to translate any Customizer setting by passing the setting value as an argument to the wpsnippets_translate_customizer_setting function. The function will then return the translated value if WPML is active, or the original value if WPML is not active.

This code snippet can be useful in multilingual websites where you want to provide translated versions of your Customizer settings. By using the wpml_translate_string function, you can easily translate the settings and display the appropriate values based on the current language.

Examples

Example 1: Translating Customizer Settings using WPML

This example demonstrates how to translate Customizer settings using the WPML plugin in WordPress. The code snippet below shows how to add a translation string for a customizer setting and retrieve the translated value.

// Add translation string for customizer setting
function wpsnippets_add_customizer_translation_string() {
    $setting_id = 'my_custom_setting';
    $setting_value = get_theme_mod($setting_id);
    if (function_exists('icl_register_string')) {
        icl_register_string('Theme Customizer', $setting_id, $setting_value);
    }
}
add_action('customize_save_after', 'wpsnippets_add_customizer_translation_string');

// Retrieve translated value for customizer setting
function wpsnippets_get_translated_customizer_setting($setting_id) {
    if (function_exists('icl_t')) {
        return icl_t('Theme Customizer', $setting_id, get_theme_mod($setting_id));
    }
    return get_theme_mod($setting_id);
}

Explanation:

  • The wpsnippets_add_customizer_translation_string function is hooked to the customize_save_after action and registers a translation string for a customizer setting using the icl_register_string function from WPML.
  • The wpsnippets_get_translated_customizer_setting function retrieves the translated value for a customizer setting using the icl_t function from WPML. If WPML is not active, it falls back to the default value using get_theme_mod.

Example 2: Translating Customizer Controls using WPML

This example demonstrates how to translate Customizer controls using the WPML plugin in WordPress. The code snippet below shows how to add translation strings for customizer controls and retrieve the translated labels.

// Add translation strings for customizer controls
function wpsnippets_add_customizer_control_translation_strings() {
    $controls = array(
        'my_custom_control_1' => 'Custom Control 1',
        'my_custom_control_2' => 'Custom Control 2',
    );
    if (function_exists('icl_register_string')) {
        foreach ($controls as $control_id => $control_label) {
            icl_register_string('Theme Customizer', $control_id, $control_label);
        }
    }
}
add_action('customize_controls_enqueue_scripts', 'wpsnippets_add_customizer_control_translation_strings');

// Retrieve translated labels for customizer controls
function wpsnippets_get_translated_customizer_control_label($control_id) {
    if (function_exists('icl_t')) {
        return icl_t('Theme Customizer', $control_id, $control_id);
    }
    return $control_id;
}

Explanation:

  • The wpsnippets_add_customizer_control_translation_strings function is hooked to the customize_controls_enqueue_scripts action and registers translation strings for customizer controls using the icl_register_string function from WPML.
  • The wpsnippets_get_translated_customizer_control_label function retrieves the translated label for a customizer control using the icl_t function from WPML. If WPML is not active, it falls back to the control ID.

Example 3: Translating Customizer Sections using WPML

This example demonstrates how to translate Customizer sections using the WPML plugin in WordPress. The code snippet below shows how to add translation strings for customizer sections and retrieve the translated titles.

// Add translation strings for customizer sections
function wpsnippets_add_customizer_section_translation_strings() {
    $sections = array(
        'my_custom_section_1' => 'Custom Section 1',
        'my_custom_section_2' => 'Custom Section 2',
    );
    if (function_exists('icl_register_string')) {
        foreach ($sections as $section_id => $section_title) {
            icl_register_string('Theme Customizer', $section_id, $section_title);
        }
    }
}
add_action('customize_controls_enqueue_scripts', 'wpsnippets_add_customizer_section_translation_strings');

// Retrieve translated titles for customizer sections
function wpsnippets_get_translated_customizer_section_title($section_id) {
    if (function_exists('icl_t')) {
        return icl_t('Theme Customizer', $section_id, $section_id);
    }
    return $section_id;
}

Explanation:

  • The wpsnippets_add_customizer_section_translation_strings function is hooked to the customize_controls_enqueue_scripts action and registers translation strings for customizer sections using the icl_register_string function from WPML.
  • The wpsnippets_get_translated_customizer_section_title function retrieves the translated title for a customizer section using the icl_t function from WPML. If WPML is not active, it falls back to the section ID.
Last updated on October 18, 2023. Originally posted on December 29, 2023.

Leave a Reply

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