• kardoo

    (@kardoo)


    Hello, I would like to find a way to change my website’s language based on the cookie that I have made it save.
    I have a custom plugin that saves the language cookies like this:

    function aws_set_language_cookie() {
    
        //To save the language in a cookie
        
        $current_language = get_locale();
        
        if ($current_language == "es_ES") {
            $result = setcookie('lang', $current_language, time() + (30 * DAY_IN_SECONDS), '/');
    
        } else {
            $result = setcookie('lang', $current_language, time() + (30 * DAY_IN_SECONDS), '/');
        }
    
        //To read the cookie and change the language based on the cookie
        
        if (isset($_COOKIE['lang']) && $_COOKIE['lang'] == "es_ES") {
            //need code to change website language to spanish here
        }
    }

    How do I change the language to Spanish inside the last if statement?

    The plugin I use to change my website’s language is called TranslatePress

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • a2hostingrj

    (@a2hostingrj)

    Wouldn’t the first conditions change the language to Spanish?

    What would be the value of the cookie?

    Moderator bcworkz

    (@bcworkz)

    You can change language with the “locale” filter.

    add_action('locale', function(){
       return 'es_ES';
    });

    This does need to be done before the translation system attempts to fetch any language files.

    Hi,

    I had the same need, I fixed the problem with the redirects

    switch ($lang)
    {
    case ‘EN’:
    wp_redirect( “https://mysite.com/en/” );
    die;
    break;
    case ‘FR’:
    wp_redirect( “https://mysite.com” );
    die;
    break;
    default:
    wp_redirect( “https://mysite.com” );
    die;
    break;
    }

    hope that helps.

    • This reply was modified 4 years, 11 months ago by soufianecool.
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How do I change language’ is closed to new replies.