开发者

Keeping track of language in a session variable

开发者 https://www.devze.com 2023-02-20 18:51 出处:网络
What my current code does is, while checking the DB if the versions (FR and EN) are either True or False, display the proper content and if both exist to display a link so that users can switch langua

What my current code does is, while checking the DB if the versions (FR and EN) are either True or False, display the proper content and if both exist to display a link so that users can switch languages. If only one language exists, the content is shown in that language and there is no link displayed.

the 2 functions in javascript are like this, here`s the FR one:

function makeVisibleFR()
{
    document.getElementById('bbqc_contentFR').style.display="inline"; 
    document.getElementById('bbqc_contentEN').style.display='none';
    document.getElementById('vFrancais').style.display='none';
    document.getElementById('vAnglais').style.display="inline";
}

What i`d like to add to this is the option of memorizing the user's choice and displaying the following pages with the same language version.

I imagine i'd need to create a $_SESSION['language'] variable and store in it either "FR" or "EN" but i`m not sure how to go about implementing that within my current code.

<?php
                if($versionFR == true)
                { 
                    if($versionEN == true)
                    {
                ?>
                    <a href="javascript:makeVisibleEN()" id="vAnglais">Version Anglaise</a> 
                    <div id="bbqc_contentFR">
                        <h2><?php echo $titleFR; ?></h2>
                        <?php echo $contentFR; ?>
                    </div>

                    <a href="javascript:makeVisibleFR()" id="vFrancais" style="display:none">Version Française </a>
                    <div style="display:none" id="bbqc_contentEN">
                        <h2><?php echo $titleEN; ?></h2>
                        <?php echo $contentEN; ?>
                    </div>
                <?php 
                    }
                    else
                    {
                ?>
                    <div id="bbqc_contentFR">
                        <h2><?php echo $titleFR; ?></h2>
                        <?php echo $contentFR; ?>
                    </div>
                <?php 
                    }
                }
                else
                {
                    if($versionEN == true)
                    {
                ?>
                    <div id="bbqc_contentEN">
                        <h2><?php echo $titleEN; ?></h2>
                        <?php 开发者_C百科echo $contentEN; ?>
                    </div>
                <?php 
                    }
                    else
                    {
                ?>
                    <h2>Erreur, il n`y a aucun texte</h2>
                <?php 
                    }
                }
                ?>


Here's a simplistic example:

// assuming that databaseHas() queries available languages
session_start();

$langs = array('ENG', 'FR');
$showlang = '';
if (databaseHas($_SESSION['lang']))
{
    $showlang = $_SESSION['lang'];
}
else
{
    foreach ($langs as $l)
    {
        if (databaseHas($l))
        {
            $showlang = $l;
            break;
        }
    }
}

if ($showlang == '')
{
    die('No languages found!');
}

echo databaseContent($showlang);

// print links to alternate languages
foreach ($langs as $l)
{
    if (databaseHas($l) && $l != $showlang)
    {
        // print link to this language
    }
}


put session_start() at the top of your file then:

if($versionFR == true) $_SESSION['lang'] = 'FR';
else $_SESSION['lang'] = 'ENG';

//later on (could be in a whole other page with session_start() on top)
if($_SESSION['lang'] == 'FR'){/*display FR stuff*/}
elseif($_SESSION['lang'] == 'ENG'){/*display ENG stuff*/}

Something like that should work well for ya ^_^


How would i go about adding that if 1) both versions exist 2) lang is ENG 3) english part is display:none by default

1) You'd just add the clause: if($_SESSION['lang'] == 'FR' && $_SESSION['lang'] == 'ENG') 2) Not sure what you mean here, Neal explained it well from what I can see 3) If ENG is display:none by default, you'd want to fire off a javascript function to toggle it back on.

But let's take a step back here, consider this: Make two language files that define each piece of content. So for your english.php you might have variables such as $GREETING = 'Hello'; $YES = 'YES'; and then in your french.php you'd define these variables as $GREETING = 'Bonjour'; $YES = 'WEE'; (I'm not even sure if wee means yes, but you get the idea!). So now you can choose to include the appropriate language file based on the user's language, and you make it easy to add another language down the road. Be flexible!

0

精彩评论

暂无评论...
验证码 换一张
取 消