Language switcher

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 24, 2014   (RSS)

By mattbcd - January 21, 2014

I've been trying to create a simple language switcher (Eng & Ger).  I've created duplicate fields for each piece of content, ending in _en or _de  Then created links for each ending in ?lan=eng and ?lan=de

A test version can be seen at:

http://www.premiergolfacademy.com/aboutTest.php

Everything works up to a point, but when you click the links, which sets a cookie, you only get the correct language when you click again, or refresh. The PHP which select the correct content from the cookie setting is:

<?php if(@$_COOKIE['langCookie'] == "de"): ?>
    <?php echo htmlencode($aboutRecord['title_de']) ?>
    <?php else: ?>
    <?php echo htmlencode($aboutRecord['title_en']) ?>
    <?php endif ?>

and the PHP which sets the cookie is:

<?php

function setLanguage ()
{
    if(isset($_GET['lan']))
    {
        if($_GET['lan'] == 'de')
        {
            setcookie('langCookie','de',time()+60*60*24*365);
        }
        else
        {
            setcookie('langCookie','en',time()+60*60*24*365);
        }
    }
    else
    {
        setcookie('langCookie','en',time()+60*60*24*365);
    }
}

function checkLangCookie ()
{
    if(isset($_COOKIE['langCookie']))
    {
        if($_COOKIE['langCookie'] == 'de')
        {
            setcookie('langCookie','de',time()+60*60*24*365);
        }
        else
        {
            setcookie('langCookie','en',time()+60*60*24*365);
        }
    }
    else
    {
        setcookie('langCookie','de',time()+60*60*24*365);
    }
}
checkLangCookie();
setLanguage();

?>

I'm sure this part is the problem - would anyone be able to help?

By mattbcd - January 24, 2014

Hi Greg

It worked a treat. Thank you sooo much! Also, thanks for not laughing at my appalling code - as a relative newbie one can be too embarrassed to ask for help. Particularly when the answer is staring you in the face. So you are officially 'good people'. Thank you.

Matt