0

I want to have three languages in a website - two buttons for the non-selected language, and just a text for the used language - plus two buttons that shows different data on a div (tables from a database, not shown here).

Everything is "button type='submit'...". I read that I "just need to" add a "input type='hidden'...", but I get a repeated 'lang' parameter on the URL, because of the clickable buttons for the languages not selected.

What's the right way to do this, please?

Here's the code, with the floating language bar on the upper right corner:

<?php session_start();
// connects to the databse
$erroConn = include(".../connector.php");
// converts $_GETs to $php_vars
if (isset($_GET['lang'])) { // language
    $lang = $_GET['lang'];
    leDic($lang); // reads the apropriate language file
} else {
    $lang = NULL;
}
if (isset($_GET['table'])) { // table
    $table = $_GET['table'];
} else {
    $table = NULL;
}
?>

<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <title>Example</title>
    <style>
        .langs { /* makes the language bar float */
            background-color: #90A090;
            position:absolute;
            right:10;
            top:10;
            font-size: 0.75em; /* 10px/16px = 0.625em */
        }
    </style>
</head>
<body>

    <form id="form1" name="form1" method="get" action="">
    <?php
    echo "<h1>".txt('titulo')."</h1>"; // draws the appropriate phrase from the language file
// if don't have a table selected yet
    if (is_null($table)) {
// shows both buttons
        echo "<button type='submit' name='table' value='A'>table A</button><BR>";
        echo "<button type='submit' name='table' value='B'>table B</button><BR>";
    } else {
// if already have a table selected, shows only the others as buttons
        switch($table) {
            case 'A':
                echo "table A<BR>";
                echo "<button type='submit' name='table' value='B'>table B</button><BR>";
            break;
            case 'B':
                echo "<button type='submit' name='table' value='A'>table A</button><BR>";
                echo "table B<BR>";
            break;
        }
    }
// constructs the language bar inside the div 'langs'
    $dir1 = glob('./coisas_txt*'); # all languages available
    echo "<div id='langs' class='langs'>";
    foreach ($dir1 as $fname) {
        $sigla = mb_substr($fname,-2,NULL,'UTF-8');
// draws a darker button for the selected language
        if ($sigla == $lang) {
            echo "<button type='submit' name='lang' value='$sigla'><strong>$sigla</strong></button>";
        } else {
            echo "<button type='submit' name='lang' value='$sigla'>$sigla</button>";
        }
    }
    echo "</div>";
    echo "<div id='tabela'>";
    if (!is_null($table)) {
// shows the data, if any
        switch($table) {
            case 'A':
                echo 'tabela A';
            break;
            case 'B':
                echo 'tabela B';
            break;
        }
    }
    echo "</div>";
    ?>
    </form>
</body>
</html>

Thank you!

Rodrigo
  • 4,706
  • 6
  • 51
  • 94
  • You can use `$_GET['lang']` the once and store the language preference into the users session, from here on you can then refer to the language value in the session, currently your script is designed so every url must have the language on it otherwise it nulls. – Scuzzy Aug 20 '15 at 23:32
  • Yes, I was thinking about have the language too in the url, but I can go without this. Thank you! – Rodrigo Aug 20 '15 at 23:39

2 Answers2

1

Here's a crude example of basic language selection and session storage

<?php // lang.php

  session_start();

  $validLanguages = array('en','jp','ru');

  // If found on URL, set the session
  if(isset($_GET['lang']) and !empty($_GET['lang']) and in_array($_GET['lang'],$validLanguages))
  {
    $lang = $_SESSION['lang'] = $_GET['lang'];
  }
  // Read from session
  elseif(isset($_SESSION['lang']))
  {
    $lang = $_SESSION['lang'];
  }
  // Default
  else
  {
    $lang = 'en';
  }

?>

<form method="get">
  <input type="submit" name="lang" value="en" <?php if($lang == 'en'){ ?>disabled<?php }?>>
  <input type="submit" name="lang" value="jp" <?php if($lang == 'jp'){ ?>disabled<?php }?>>
  <input type="submit" name="lang" value="ru" <?php if($lang == 'ru'){ ?>disabled<?php }?>>
  <input type="submit" name="lang" value="es" <?php if($lang == 'es'){ ?>disabled<?php }?>>
</form>

<a href="lang.php">Some Page link</a> | <a href="lang.php">Another Page link</a>
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • Thank you for the example. I'm trying it. – Rodrigo Aug 20 '15 at 23:48
  • 1
    Mind you, something like language is probably more suitable for cookie storage with a longer expiration. another example http://stackoverflow.com/questions/7791126/how-to-create-a-simple-php-cookie-language-toggle – Scuzzy Aug 20 '15 at 23:48
  • Even with sessions, when I click a language button, the value of $_GET from the selected table is disappearing, even when I have a "input type='hidden' name='table' value='a_table'". – Rodrigo Aug 21 '15 at 00:02
  • 1
    Can I ask why must you have it on the URL line? Once it goes into the session or a cookie its always accessible from your code. Or is this for search engine indexation? For all your OTHER non language selection forms you can have `` in it if you must. – Scuzzy Aug 21 '15 at 00:03
  • 1
    I'll try next with two separate forms on the same page, and see if that works. I think this will answer your question: "It isn't a good idea to use cookies to determine the language. Separate URLs are a better bet, people can link directly to the content in the same language as they are using on the page they put the link, and search engines see two separate pages." http://stackoverflow.com/questions/7791126/how-to-create-a-simple-php-cookie-language-toggle#comment9507869_7791126 After all, I think I was just following REST rules to put all the non-secret variables on the url. – Rodrigo Aug 21 '15 at 00:38
  • Then I would be looking into rewrite rules so you can have `www.domain.com/en/page` and have nice clean URL's – Scuzzy Aug 21 '15 at 00:40
  • Yes, this looks better, but seems like a pain to update. – Rodrigo Aug 21 '15 at 00:41
0

Well, all I needed was two separate forms, with the buttons from one form as hidden inputs on the other, and vice-versa.

Here is the code, in case anyone need it:

<?php // index.php
session_start();
$errorConn = include(".../dbfile.php");
// If found on URL, set the session
if(isset($_GET['lang']) and !empty($_GET['lang'])) { //and in_array($_GET['lang'],$validLanguages)
    $lang = $_SESSION['lang'] = $_GET['lang'];
} elseif(isset($_SESSION['lang'])) { // Read from session
    $lang = $_SESSION['lang'];
} else { // Default
    $lang = $_SESSION['lang'] = 'BR';
}
leDic($lang);
if(isset($_GET['table']) and !empty($_GET['table'])) { //and in_array($_GET['lang'],$validLanguages)
    $table = $_SESSION['table'] = $_GET['table'];
} elseif(isset($_SESSION['table'])) { // Read from session
    $table = $_SESSION['table'];
} else { // Default
    $table = $_SESSION['table'] = -1;
}
?>

<html lang='BR'>
<head>
    <meta charset='UTF-8'>
    <title>Example</title>
    <style>
        .langs {
            background-color: #90A090;
            position:absolute;
            right:10;
            top:10;
            font-size: 0.75em; /* 10px/16px = 0.625em */
        }
    </style>
</head>
<body>
    <form id="form1" name="form1" method="get" action="">
    <?php
    echo "<h1>".txt('titulo')."</h1>";
    echo "<input type='hidden' name='lang' value='$lang' />";
    echo "<button type='submit' name='table' value='A'>table A</button><BR>";
    echo "<button type='submit' name='table' value='B'>table B</button><BR>";
    echo "<div id='tabela'>";
    if (!is_null($table)) {
        switch($table) {
            case 'A':
                echo 'tabela A';
            break;
            case 'B':
                echo 'tabela B';
            break;
        }
    }
    echo "</div>";
    ?>
    </form>

    <form id="form2" name="form2" method="get" action="">
    <?php
    $dir1 = glob('./coisas_txt*'); # all languages available
    echo "<input type='hidden' name='table' value='$table' />";
    echo "<div id='langs' class='langs'>";
    foreach ($dir1 as $fname) {
        $sigla = mb_substr($fname,-2,NULL,'UTF-8');
        if ($sigla == $lang) {
            echo "<button type='submit' name='lang' value='$sigla'><strong>$sigla</strong></button>";
        } else {
            echo "<button type='submit' name='lang' value='$sigla'>$sigla</button>";
        }
    }
    echo "</div>";
    ?>
    </form>
</body>
</html>
Rodrigo
  • 4,706
  • 6
  • 51
  • 94