Last updated on October 18, 2023

WPML language switcher page not translating

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

Troubleshoot page translation issues with WPML.

The WPML plugin is a popular choice for creating multilingual websites with WordPress. One common issue that users may encounter is when the language switcher page is not translating properly. This can happen due to various reasons, such as incorrect configuration or conflicts with other plugins or themes.

To troubleshoot and resolve this issue, you can follow these steps:

  1. Check WPML Configuration: Ensure that you have properly configured the WPML plugin. Go to the WPML settings page (WPML -> Languages) and review the settings for language switcher options. Make sure you have selected the appropriate language switcher type and that the necessary languages are enabled.

  2. Verify Translation Status: Confirm that the content you want to translate has been properly marked for translation. You can do this by editing the page or post in the WordPress admin area and checking the language settings. Ensure that the content has been translated for the desired languages.

  3. Check for Conflicts: Deactivate other plugins and switch to a default WordPress theme (e.g., Twenty Twenty-One) to rule out any conflicts. If the language switcher starts working after deactivating a specific plugin or theme, you can narrow down the cause and contact the plugin/theme developer for further assistance.

  4. Debugging: Enable WPML’s debug mode to gather more information about the issue. Add the following code snippet to your theme’s functions.php file:

define('ICL_DEBUG_MODE', true);

This will enable debug mode for WPML and display any relevant error messages on the front-end. Remember to remove this code snippet once you have resolved the issue.

  1. Cache Plugin Compatibility: If you are using a caching plugin, such as WP Rocket or W3 Total Cache, try clearing the cache or excluding the language switcher page from caching. Some caching plugins may interfere with the translation process.

If the above steps do not resolve the issue, you may need to reach out to WPML support for further assistance. They can provide more specific guidance based on your setup and configuration.

Remember to always keep your WordPress, theme, and plugins up to date to ensure compatibility and minimize potential issues.

I hope this helps! Let me know if you have any further questions.

Note: The code snippet provided in step 4 is used to enable WPML’s debug mode. It defines the constant ICL_DEBUG_MODE with a value of true, which activates the debug mode. This will display any relevant error messages on the front-end, helping you identify the cause of the translation issue.

Examples

Example #1: Adding Language Switcher to a Custom Menu

This example demonstrates how to add a language switcher to a custom menu in WordPress using WPML. The code snippet below adds a language switcher to a custom menu by using the wp_nav_menu_items filter hook.

function wpsnippets_add_language_switcher_to_menu( $items, $args ) {
    if ( function_exists( 'icl_get_languages' ) ) {
        $languages = icl_get_languages( 'skip_missing=0' );
        if ( ! empty( $languages ) ) {
            $switcher = '<ul class="language-switcher">';
            foreach ( $languages as $language ) {
                $switcher .= '<li>';
                if ( $language['active'] ) {
                    $switcher .= '<span class="active">' . $language['native_name'] . '</span>';
                } else {
                    $switcher .= '<a href="' . esc_url( $language['url'] ) . '">' . $language['native_name'] . '</a>';
                }
                $switcher .= '</li>';
            }
            $switcher .= '</ul>';
            $items .= $switcher;
        }
    }
    return $items;
}
add_filter( 'wp_nav_menu_items', 'wpsnippets_add_language_switcher_to_menu', 10, 2 );

The code adds a language switcher to a custom menu by using the wp_nav_menu_items filter hook. It checks if the icl_get_languages function exists (provided by WPML) and retrieves the available languages. It then generates the HTML markup for the language switcher and appends it to the menu items.

Example #2: Displaying Language Switcher in a Widget

This example demonstrates how to display a language switcher in a widget area using WPML. The code snippet below creates a custom widget that outputs the language switcher HTML.

class WPSnippets_Language_Switcher_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'wpsnippets_language_switcher_widget',
            __( 'Language Switcher', 'text-domain' ),
            array( 'description' => __( 'Displays a language switcher.', 'text-domain' ) )
        );
    }

    public function widget( $args, $instance ) {
        if ( function_exists( 'icl_get_languages' ) ) {
            $languages = icl_get_languages( 'skip_missing=0' );
            if ( ! empty( $languages ) ) {
                echo '<ul class="language-switcher">';
                foreach ( $languages as $language ) {
                    echo '<li>';
                    if ( $language['active'] ) {
                        echo '<span class="active">' . $language['native_name'] . '</span>';
                    } else {
                        echo '<a href="' . esc_url( $language['url'] ) . '">' . $language['native_name'] . '</a>';
                    }
                    echo '</li>';
                }
                echo '</ul>';
            }
        }
    }
}
function wpsnippets_register_language_switcher_widget() {
    register_widget( 'WPSnippets_Language_Switcher_Widget' );
}
add_action( 'widgets_init', 'wpsnippets_register_language_switcher_widget' );

The code creates a custom widget called “Language Switcher” that displays the language switcher HTML. It checks if the icl_get_languages function exists (provided by WPML) and retrieves the available languages. It then generates the HTML markup for the language switcher and outputs it in the widget.

Example #3: Adding Language Switcher to a Theme Template

This example demonstrates how to add a language switcher to a theme template using WPML. The code snippet below adds a language switcher to a theme template by directly calling the icl_get_languages function and generating the HTML markup.

if ( function_exists( 'icl_get_languages' ) ) {
    $languages = icl_get_languages( 'skip_missing=0' );
    if ( ! empty( $languages ) ) {
        echo '<ul class="language-switcher">';
        foreach ( $languages as $language ) {
            echo '<li>';
            if ( $language['active'] ) {
                echo '<span class="active">' . $language['native_name'] . '</span>';
            } else {
                echo '<a href="' . esc_url( $language['url'] ) . '">' . $language['native_name'] . '</a>';
            }
            echo '</li>';
        }
        echo '</ul>';
    }
}

The code adds a language switcher to a theme template by directly calling the icl_get_languages function (provided by WPML) and generating the HTML markup. It checks if the icl_get_languages function exists and retrieves the available languages. It then generates the HTML markup for the language switcher and outputs it in the template.

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

Leave a Reply

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