Carlos Rodriguez
Carlos Rodriguez

Reputation: 97

Need help creating the php and/or javascript using a hyperlink to change the language on a webpage

I have previously used a dropdown selection box with options using a post method in order to change the language in a webpage that is saved on a separate file. I am now trying to create something similar but need help. Now I am trying to make the webpage for only 2 languages and when viewing the webpage on one language the option to switch to the other will appear. Essentially giving the viewer the option to change the session language to either English or Spanish only with showing the opposite language as a hyperlink on all pages. My language file is essentially as follows:

<?php    
     $lang = array(
           'EN' => array(
                         'ABOUT' => 'About', 
                         'HOME' => 'Home'
                        ),
           'SP' => array(
                         'ABOUT' => 'Acerca',
                         'HOME' => 'Casa'
                        )
                  )
?>

This PHP code that I have shown here is more extensive but this is how I set things up writing these lines of code on another file to be able to change the language. On my main page I have a short section of code before the html document is declared and that is as follows:

<?php 
      require("lang.php");
      session_start();
      $_SESSION['LANG'] = "EN";
      if(@$_POST['lang-chooser']){
         $_SESSION['LANG'] = $_POST['lang-chooser'];
      }
?>

I am trying to make the portion of the page where I have the hyperlink to be located in the header or body of the document. The code I have currently for the option to choose a language is as follows:

<form method="post" action="" id="lang-form">
      <select id="lang-chooser" class="lang-chooser" name="lang-chooser" onchange="window.lang(this);">
         <option value="EN"<?php if($_SESSION['LANG'] == "EN") {?> selected="selected"<?php }?>>English</option>
         <option value="SP"<?php if($_SESSION['LANG'] == "SP") {?> selected="selected"<?php }?>>Spanish</option>
      </select>
</form>

Underneath my footer but still in the body portion I also have a little amount of script as follows:

<script type="text/javascript">
  function lang(element){
      var lang_name = element.value;
      var e_form = document.getElementById("lang-form");
      e_form.submit();
      console.log(element);
      }
  window.lang = lang;
</script>

With all of these portions of code I was successfully able to change the language using the dropdown selection box while staying on the current page. The code I would use to have changeable text would be as follows:

<?php echo ($lang[$_SESSION['LANG']]['ABOUT']); ?>

Now I wish to have the option to change the session language on any page again but without the dropdown selection box. I wish to have it so that when the page is in English which it automatically should be when accessing the site there will be a hyperlink named Espanol which allows the viewer to change to Spanish and once the page is in Spanish the hyperlink will change to saying English which allows the viewer to change to English. From looking online I am lead to believe that I will still need the intro PHP code and javascript but will no longer need the "form" or "method" portion to change the session language. I believe all that I need now in replacement of the "form" and "posting-method" is as follows:

<a href="index.php?LANG=SP"> <?php echo($lang[$_SESSION['LANG']]['SPANISH']); ?> </a>

I believe that my code is still lacking and this is why I still cant get it to work. Essentially I will need the hyperlink to change text according to the session and to also be used to change the session language from either Spanish or English. I am a little stumped here and would very much appreciate any kind of help. Thanks for taking the time to read this question.

Upvotes: 4

Views: 154

Answers (2)

independent.guru
independent.guru

Reputation: 580

You could replace your form with php if, else and get functions.

By using $_GET at the head of the page you can check if lang is set in the URL and set a session based on the result:

Edit

This section will replace everything after session_start(); in the second piece of php code you placed in the question.

<?php

if(!isset($_SESSION['LANG'])){
    $_SESSION['LANG']='EN';
    header('location: '.$_SERVER["REQUEST_URI"]);
}

if(isset($_GET['lang'])){
    if($_GET['lang']=='sp'){
        $_SESSION['LANG']='SP';
    }else{
        $_SESSION['LANG']='EN';
    }
}

After you can check if the session is set then call out a href link to whichever language you want to change to.

This section will replace the html form

<?php
if(isset($_SESSION['LANG'])){
    if($_SESSION['LANG']=='EN'){
        echo '<a href="?lang=sp">Espanol</a>';
    }else{
        echo '<a href="?lang=en">English</a>';
    }
}else{
        echo '<a href="?lang=sp">Espanol</a>';
}

You don't need java to achieve this.

EDIT

If you want the URL not to show the ?lang= you can include another session and a header in the first section such as:

<?php
if(isset($_GET['lang'])){
    if($_GET['lang']=='sp'){
        $_SESSION['LANG']='SP';
        header('location: '.$_SESSION['URI']);
    }else{
        $_SESSION['LANG']='EN';
        header('location: '.$_SESSION['URI']);
    }
}else{
    $_SESSION['URI']=$_SERVER["REQUEST_URI"];
}

This will instantly redirect the user back to the page they were on, they shouldn't notice the refresh.

Upvotes: 2

MindGamer
MindGamer

Reputation: 136

<?php 
   $allowed_langs = array('EN' => 'English', 'SP' => 'Espanol'); 
   $site_lang = isset($_SESSION['LANG'])?$_SESSION['LANG']:'EN';

  //Here you can set language according to link

 if(isset($_GET['lang'] && in_array($_GET['lang'], $allowed_langs)){
   $_SESSION['LANG'] = $_GET['lang'];
   $site_lang = $_GET['lang'];

 //Then you can refresh the page if you want to load new file or start 
  // including your language file after this language set.
 }

//include your lang file
include_once( 'langs/' . $site_lang . '/text.php' );
?>
          <ul>
 <?php  
foreach($allowed_langs as $langshort => $langlong){
 $new_query_string = build_query_string($param, 'lang', $langshort);
 $new_link = strtok($_SERVER["REQUEST_URI"],'?') . "?" . $new_query_string;
 $class = ($_SESSION['LANG']==$langshort)?'selected':'';
 ?>
<li class="<?= $class ?>"><a href="<?= $new_link ?>"><?= $langlong ?></a></li>

<?php  } ?>
  </ul>

Upvotes: 0

Related Questions