0

I'm setting up a language switcher for my website but have an issue with the actual switcher which always brings back to the homepage (index.php). :-s very annoying

I a complete newbie in php and have been following this tutorial to do so on youtube: https://www.youtube.com/watch?v=cgvDMUrQ3vA

I am trying to implement it on a multi pages website. The actual language swap works like a charm and seems to keep the set language throughout the pages but the mechanism for the switch doesn't because it uses a static link to load index.php + new language attribute, bringing the visiter inevitably back to the homepage instead of reloading the current page.

<?php echo $lang['lang-selector-href']  /* set the <a> tag src="index+lang='x'" */ ?>
<?php echo $lang['lang-selector-switch'] /* display language to be set */  ?>
</a> 

i'm using 2 similar files (en.php and fr.php) containing an array to store the translated content

en.php :

<?php
$lang = array(
   "lang-selector-href" => "<a href='index.php?lang=fr'>",
   "lang-selector-switch" => "Français (French)",
...
...

);
?>

fr.php :

<?php
$lang = array(
  "lang-selector-href" => "<a href='index.php?lang=en'>",
  "lang-selector-switch" => "English (anglais)",
...
...

);
?>

Question: How could i could I change the link to:

<a href='current-page.php?lang=fr'> Français (French) </a>

or:

<a href='current-page.php?lang=en'> English </a>

according to the set language?

here is my config.php file

<?php
session_start();

// if no language selected go for english
if (!isset($_SESSION['lang']))
    $_SESSION['lang'] = "en";


else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
    if ($_GET['lang'] == "en")
        $_SESSION['lang'] = "en";
    else if ($_GET['lang'] == "fr")
        $_SESSION['lang'] = "fr";
}

// load content
require_once "content/languages/" . $_SESSION['lang'] . ".php";

?>

Sorry if it sounds very basic, i'm sure it is but couldn't find a simple answer that would work... Thank you!

1 Answers1

0

Make Dropdown list in html which options have language value like below as,

    <select onchange="callSomeFunc()">
       <option value="fr">French</option>
       <option value="en">English</option>
    </select>

On change of dropdown list you need to call function which will set selected language value in to php $_SESSION.

brombeer
  • 8,716
  • 5
  • 21
  • 27
vitts
  • 127
  • 7