3

I'm creating select list. But I don't know how to make window language change depending on selected language, and I need help by opening these translations from $lang.es and $lang.en files. Here is one for example:

<?php
/* 
------------------
Language: English
------------------
*/
$lang = array();

$lang['page_title'] = 'Client Service System';
$lang['username'] = 'Username';
$lang['password'] = 'Password';
$lang['language'] = 'Language';
$lang['es'] = 'Espaniol';
$lang['en'] = 'English';
$lang['forgot'] = 'Forgot password';
$lang['submit'] = 'login';

?>

And here is my html php script where the dropdown list is:

<tr>
    <td align="right" nowrap><?php echo $lang['language'];?>:</td>
    <td align="left" nowrap>
        <select type="language" id="my-select" name="language" class="text" onchange="javascript:languageChange();">
            <option value="es"><?php echo $lang['es']; ?></option>
            <option value="en"><?php echo $lang['en']; ?></option>
        </select>
    </td>
</tr>    <script type="text/javascript">
        var select = document.forms[0].language;
        select.onchange = function(){
           var kalba =  select.options[select.selectedIndex].value; // to get Value
           var text =  select.options[select.selectedIndex].text; // to get Text
        }; </script>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TorresAlGrande
  • 213
  • 1
  • 14

1 Answers1

0

There are many ways to handle localization in PHP. As noted on another question, you could use PHP's gettext. You could also use constants, store in JSON format, store in a database or in an array, like you are doing.

To make your script work, first you need to redirect to the PHP script setting the language to be loaded. Then you make your script conditionally open one of the files.

Here is a simple implementation, with a class to handle the loading and retrieving of language files.

Main PHP script

<?php
require 'Localization.Class.php';

$localization = new Localization();
$localization->addLanguage("en");
$localization->addLanguage("es");
$lang = $localization->getLanguageStrings($_GET);
$selectedLanguage = $localization->getSelectedLanguage();
?>

<tr>
    <td align="right" nowrap><?php echo $lang['language'];?>:</td>
    <td align="left" nowrap>
        <form>
            <select type="language" id="my-select" name="language" class="text" onchange="javascript:languageChange();">
                <option value="es"<?php echo ($selectedLanguage == "es") ? " selected" : ""; ?>><?php echo $lang['es']; ?></option>
                <option value="en"<?php echo ($selectedLanguage == "en") ? " selected" : ""; ?>><?php echo $lang['en']; ?></option>
            </select>
        </form>
    </td>
</tr>    

<script type="text/javascript">
var select = document.forms[0].language;
select.onchange = function(){
   var selectedLanguage = select.options[select.selectedIndex].value;
   window.location.href = '?lang=' + selectedLanguage;
}; 
</script>

Localization.Class.php

<?php
class Localization
{
    private $defaultLanguage = "en";
    private $chosenLanguage = null;
    private $languages = array();

    public function getLanguageStrings($get) {
        $this->identifyLanguage($get);
        $languageArray = $this->getLanguageFileContents();
        return $languageArray;
    }

    public function addLanguage($lang) {
        $this->languages[] = $lang;
    }

    public function getSelectedLanguage() {
        return $this->chosenLanguage;
    }

    private function identifyLanguage($get) {
        if (isset($get['lang']) and $get['lang'] !== "") {
            if (!in_array($get['lang'], $this->languages)) {
                // this is an important check, as this string will be used
                // as part of the filename to be included
                throw new Exception(__METHOD__ . ": The specified language ($get[lang]) was not found.");
            }
            $this->chosenLanguage = $get['lang'];
        } else {
            $this->chosenLanguage = $this->defaultLanguage;
        }
    }

    private function getLanguageFileContents() {
        $filename = "lang.{$this->chosenLanguage}.php";
        if (!file_exists($filename)) {
            throw new Exception(__METHOD__ . ": The language file \"$filename\" was not found.");
        }
        include $filename;
        return $lang;
    }
}
?>
Community
  • 1
  • 1
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62