Site icon Hip-Hop Website Design and Development

The best way to appropriately load a special model of fundamental menu based mostly on the consumer language in WordPress? Is it a superb answer?

I’ve the next drawback growing a WordPress theme and I’m right here to ask you if this answer may very well be a superb answer or if exist some higher solution to do.

I’ve a customized WP theme and into this theme there’s a fundamental menu. The web site is multilanguage (italian and english) so I’ve to load the Italian fundamental menu (comprises the menu merchandise in Italian language) if the customer is italian and the principle menu should been loaded within the English model if the consumer will not be italian.

So I’ve discovered this answer:

Within the WP backend I create 2 totally different menus: Menu 1 and Menu 2 (the primary one having voices in Italian language and the second in English language)

Then inside my theme (into the header.php file) I declare one thing prefer it:

  1. First I detect the consumer language tacking it from $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]

  2. Then, I take advantage of this worth to point out Menu 1 if the $lang worth is it* or *Menu 2 if the *$lang worth is en

One thing like this:

<?php 
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    //echo "Language is $lang";  IT SEEMS TO WORK: MY LANGUAGE IS CORRECTLY RECOGNIZED AS it

    if($lang == it){      // If the consumer is Italian load Italian menu

        wp_nav_menu(array(
        'menu' => 'Menu 1', 
        'theme_location' => 'main', 
        'depth' => 2,
        'container' => 'div', 
        /*'container_class' => 'navbar navbar-default',*/
        'menu_class' => 'nav navbar-nav', 
        'fallback_cb' => 'wp_bootstrap_navwalker::fallback', 
        'walker' => new wp_bootstrap_navwalker())
    );
     }
     else{            // If the consumer will not be italian load English menu
        wp_nav_menu(array(
        'menu' => 'Menu 2', 
        'theme_location' => 'main', 
        'depth' => 2,
        'container' => 'div', 
        /*'container_class' => 'navbar navbar-default',*/
        'menu_class' => 'nav navbar-nav', 
        'fallback_cb' => 'wp_bootstrap_navwalker::fallback', 
        'walker' => new wp_bootstrap_navwalker())
    );
     }
?>

I feel that on this approach should appropriately work (I’ve to attempt) however, is it a superb answer or may I do higher in another approach?