Site logo

[Resolved] having trouble with multilingual version of the site

Home Forums Support [Resolved] having trouble with multilingual version of the site

Home Forums Support having trouble with multilingual version of the site

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2406790
    nvc

    Hello everyone,
    I am having some issues with making my site multilingual.
    When I’ve first started it I made for all of the English speaking countries separate pages like that:
    www. frontpage. com/usa
    www. frontpage. com/uk
    www. frontpage. com/canada
    www. frontpage. com/new-zealand
    www. frontpage. com/x-default
    And I’ve added with the Hook Element to the wp_head the proper hreflang tag. Everything is working just fine and Google ranks the different pages for the specific country.
    But now I want to add Spanish and German language and add language switcher with flags. I’ve installed some plugin but it changes my old hreflang tags and when I do hreflang check they are not working.
    I am not able to do the other languages like I did with the English language because the menus need to be changed.
    Do you have any easy solution to that?
    Thank you

    #2407058
    Ying
    Staff
    Customer Support

    Hi there,

    GP doesn’t have any functions in this field, so Unfortuanly there’s no easy solutions from GP.

    I would recommend reaching out to the plugin’s support to see if they have any insights.

    #2407769
    nvc

    Thanks for your answer Ying.
    A question came to my mind when I was thinking how can I create multilingual site without plugin.
    In GP there is an option I can set personal header and footer for particular pages. So i can create these Elements in other language for example. The question is if there is an option I can remove the menu from these pages and add custom menu only for these pages?
    If yes I will be able to create full page with other language and add the proper hreflang tags. I guess that will work in my case.
    What do you think?
    Regards

    #2407936
    David
    Staff
    Customer Support

    Hi there,

    if you create different language menus in Appearance > Menus.
    You can then use a PHP Snippet to check the current URI for the subfolder eg. /spain and then swap the menu

    
    function db_switch_menus( $args ) {
    
        // Get the current URI for $locale
        $locale = $_SERVER['REQUEST_URI'];
        
        // apply logic to primary nav 
        if( 'primary' === $args['theme_location'] ) {        
    
            // check $locale and swap menu ID
            if ( strpos( $locale, '/spain') !== false ) { 
                $args['menu'] = '55'; 
            }
    
            if ( strpos( $locale, '/germany') !== false ) { 
                $args['menu'] = '77'; 
            }
    
        }
        return $args;
    }
    add_filter( 'wp_nav_menu_args', 'db_switch_menus' );

    Each if statement eg.

    if ( strpos( $locale, '/spain') !== false ) { 
                $args['menu'] = '55'; 
            }
    

    Checks the URI for the appropriate sub folder eg. /spain and if true it swaps the Menu ID to 55
    So you just need to match the subfolder name with the appropriate menu ID.

    #2441428
    nvc

    Thanks David for your answer but my coding skills are really bad. Would you please explain to me with more details. Thank you.
    I already found a solution how to build multilingual using Parent and Child pages. The only issue left is the menu. Is there a way I can create a menu with Elements and include/exclude it to different pages/posts?

    #2442489
    David
    Staff
    Customer Support

    OK, there is no way in Elements to swap the menus that get displayed in the primary / secondary navs

    Here i rewrite the code:

    
    function db_switch_menus( $args ) {
    
        // Get the current URI for $locale
        $locale = $_SERVER['REQUEST_URI'];
        
        // apply logic to primary nav 
        if( 'primary' === $args['theme_location'] ) {        
    
            // check $locale and swap menu ID
            if ( strpos( $locale, '/spain') !== false ) { 
                $args['menu'] = 'spain'; 
            }
    
            if ( strpos( $locale, '/germany') !== false ) { 
                $args['menu'] = 'germany'; 
            }
    
        }
        return $args;
    }
    add_filter( 'wp_nav_menu_args', 'db_switch_menus' );

    If in your Appearance > Menus you create 3 x menus for example:

    1. English language ( and the default menu ) you can name it whatever.
    2. Spanish language and name the menu: spain
    3. German language and name the menu: germany

    Adding the above code will swap the menu to spain if the user is in the /spain directory of the site.
    And same for german.

    If thats not working let me know.

    #2443131
    nvc

    Thanks David,
    I will make a backup first and then I will try with you code. I guess that I need to add that code in the functions.php file, right?
    Regards

    #2444251
    David
    Staff
    Customer Support

    The code is PHP.
    This article explains how to add the code:

    https://docs.generatepress.com/article/adding-php/

    TLDR: Are you using a Child Theme?
    IF yes, then you add the PHP to your Child Themes functions.php
    IF no, then use the Code Snippets plugin ( link the above doc ) and add it to a new Snippet.

    #2557814
    nvc

    David, i did it : ) Now i have different menus for my English and for my Spanish version.
    I’ve done everything as you said and it works perfectly. Works sooo smooth : )
    Awesome support as always. You’re the best guys!
    Thank you!
    Regards

    #2558476
    David
    Staff
    Customer Support

    Glad to be of help

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.