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

Earlier, I used a drop-down list with options that use the post method to change the language on the web page, which is saved in a separate file. Now I'm trying to create something similar, but I need help. Now I'm trying to make a web page in only 2 languages, and when viewing a web page in one language, it will be possible to switch to another. Essentially giving the viewer the opportunity to change the session language into English or Spanish only with the display of the opposite language as a hyperlink on all pages. My language file essentially looks like this:

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

PHP-, , , , , . html-, :

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

, , . , , :

<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>

, , script :

<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>

, , . , , :

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

, . , , , , , Espanol, , , , , . , PHP- javascript, "" "", . , , , "form" "posting-method" :

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

, , . , , . , . , , .

+4
2

php if, else .

$_GET , , lang URL- :

Edit

session_start(); php, .

<?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';
    }
}

, , , href , .

html

<?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>';
}

Java.

, URL- ? lang =, , :

<?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"];
}

, , .

+2
<?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>
0

Source: https://habr.com/ru/post/1652127/


All Articles